1 //===- llvm/unittest/DebugInfo/DWARFDebugInfoTest.cpp ---------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "DwarfGenerator.h"
10 #include "DwarfUtils.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/BinaryFormat/Dwarf.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
19 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
21 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
22 #include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCSectionELF.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/Object/ObjectFile.h"
27 #include "llvm/ObjectYAML/DWARFEmitter.h"
28 #include "llvm/Support/Error.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/TargetRegistry.h"
31 #include "llvm/Support/TargetSelect.h"
32 #include "llvm/Testing/Support/Error.h"
33 #include "gtest/gtest.h"
37 using namespace dwarf
;
38 using namespace utils
;
42 template <uint16_t Version
, class AddrType
, class RefAddrType
>
44 Triple Triple
= getDefaultTargetTripleForAddrSize(sizeof(AddrType
));
45 if (!isConfigurationSupported(Triple
))
48 // Test that we can decode all DW_FORM values correctly.
49 const AddrType AddrValue
= (AddrType
)0x0123456789abcdefULL
;
50 const uint8_t BlockData
[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
51 const uint32_t BlockSize
= sizeof(BlockData
);
52 const RefAddrType RefAddr
= 0x12345678;
53 const uint8_t Data1
= 0x01U
;
54 const uint16_t Data2
= 0x2345U
;
55 const uint32_t Data4
= 0x6789abcdU
;
56 const uint64_t Data8
= 0x0011223344556677ULL
;
57 const uint64_t Data8_2
= 0xAABBCCDDEEFF0011ULL
;
58 const uint8_t Data16
[16] = {1, 2, 3, 4, 5, 6, 7, 8,
59 9, 10, 11, 12, 13, 14, 15, 16};
60 const int64_t SData
= INT64_MIN
;
61 const int64_t ICSData
= INT64_MAX
; // DW_FORM_implicit_const SData
62 const uint64_t UData
[] = {UINT64_MAX
- 1, UINT64_MAX
- 2, UINT64_MAX
- 3,
63 UINT64_MAX
- 4, UINT64_MAX
- 5, UINT64_MAX
- 6,
64 UINT64_MAX
- 7, UINT64_MAX
- 8, UINT64_MAX
- 9};
65 #define UDATA_1 18446744073709551614ULL
66 const uint32_t Dwarf32Values
[] = {1, 2, 3, 4, 5, 6, 7, 8};
67 const char *StringValue
= "Hello";
68 const char *StrpValue
= "World";
69 const char *StrxValue
= "Indexed";
70 const char *Strx1Value
= "Indexed1";
71 const char *Strx2Value
= "Indexed2";
72 const char *Strx3Value
= "Indexed3";
73 const char *Strx4Value
= "Indexed4";
75 auto ExpectedDG
= dwarfgen::Generator::create(Triple
, Version
);
76 ASSERT_THAT_EXPECTED(ExpectedDG
, Succeeded());
77 dwarfgen::Generator
*DG
= ExpectedDG
.get().get();
78 dwarfgen::CompileUnit
&CU
= DG
->addCompileUnit();
79 dwarfgen::DIE CUDie
= CU
.getUnitDIE();
82 CUDie
.addStrOffsetsBaseAttribute();
84 uint16_t Attr
= DW_AT_lo_user
;
86 //----------------------------------------------------------------------
88 //----------------------------------------------------------------------
89 const auto Attr_DW_FORM_addr
= static_cast<dwarf::Attribute
>(Attr
++);
90 CUDie
.addAttribute(Attr_DW_FORM_addr
, DW_FORM_addr
, AddrValue
);
92 //----------------------------------------------------------------------
94 //----------------------------------------------------------------------
95 const auto Attr_DW_FORM_block
= static_cast<dwarf::Attribute
>(Attr
++);
96 CUDie
.addAttribute(Attr_DW_FORM_block
, DW_FORM_block
, BlockData
, BlockSize
);
98 const auto Attr_DW_FORM_block1
= static_cast<dwarf::Attribute
>(Attr
++);
99 CUDie
.addAttribute(Attr_DW_FORM_block1
, DW_FORM_block1
, BlockData
, BlockSize
);
101 const auto Attr_DW_FORM_block2
= static_cast<dwarf::Attribute
>(Attr
++);
102 CUDie
.addAttribute(Attr_DW_FORM_block2
, DW_FORM_block2
, BlockData
, BlockSize
);
104 const auto Attr_DW_FORM_block4
= static_cast<dwarf::Attribute
>(Attr
++);
105 CUDie
.addAttribute(Attr_DW_FORM_block4
, DW_FORM_block4
, BlockData
, BlockSize
);
107 // We handle data16 as a block form.
108 const auto Attr_DW_FORM_data16
= static_cast<dwarf::Attribute
>(Attr
++);
110 CUDie
.addAttribute(Attr_DW_FORM_data16
, DW_FORM_data16
, Data16
, 16);
112 //----------------------------------------------------------------------
114 //----------------------------------------------------------------------
115 const auto Attr_DW_FORM_data1
= static_cast<dwarf::Attribute
>(Attr
++);
116 CUDie
.addAttribute(Attr_DW_FORM_data1
, DW_FORM_data1
, Data1
);
118 const auto Attr_DW_FORM_data2
= static_cast<dwarf::Attribute
>(Attr
++);
119 CUDie
.addAttribute(Attr_DW_FORM_data2
, DW_FORM_data2
, Data2
);
121 const auto Attr_DW_FORM_data4
= static_cast<dwarf::Attribute
>(Attr
++);
122 CUDie
.addAttribute(Attr_DW_FORM_data4
, DW_FORM_data4
, Data4
);
124 const auto Attr_DW_FORM_data8
= static_cast<dwarf::Attribute
>(Attr
++);
125 CUDie
.addAttribute(Attr_DW_FORM_data8
, DW_FORM_data8
, Data8
);
127 //----------------------------------------------------------------------
129 //----------------------------------------------------------------------
130 const auto Attr_DW_FORM_string
= static_cast<dwarf::Attribute
>(Attr
++);
131 CUDie
.addAttribute(Attr_DW_FORM_string
, DW_FORM_string
, StringValue
);
133 const auto Attr_DW_FORM_strx
= static_cast<dwarf::Attribute
>(Attr
++);
134 const auto Attr_DW_FORM_strx1
= static_cast<dwarf::Attribute
>(Attr
++);
135 const auto Attr_DW_FORM_strx2
= static_cast<dwarf::Attribute
>(Attr
++);
136 const auto Attr_DW_FORM_strx3
= static_cast<dwarf::Attribute
>(Attr
++);
137 const auto Attr_DW_FORM_strx4
= static_cast<dwarf::Attribute
>(Attr
++);
139 CUDie
.addAttribute(Attr_DW_FORM_strx
, DW_FORM_strx
, StrxValue
);
140 CUDie
.addAttribute(Attr_DW_FORM_strx1
, DW_FORM_strx1
, Strx1Value
);
141 CUDie
.addAttribute(Attr_DW_FORM_strx2
, DW_FORM_strx2
, Strx2Value
);
142 CUDie
.addAttribute(Attr_DW_FORM_strx3
, DW_FORM_strx3
, Strx3Value
);
143 CUDie
.addAttribute(Attr_DW_FORM_strx4
, DW_FORM_strx4
, Strx4Value
);
146 const auto Attr_DW_FORM_strp
= static_cast<dwarf::Attribute
>(Attr
++);
147 CUDie
.addAttribute(Attr_DW_FORM_strp
, DW_FORM_strp
, StrpValue
);
149 //----------------------------------------------------------------------
150 // Test reference forms
151 //----------------------------------------------------------------------
152 const auto Attr_DW_FORM_ref_addr
= static_cast<dwarf::Attribute
>(Attr
++);
153 CUDie
.addAttribute(Attr_DW_FORM_ref_addr
, DW_FORM_ref_addr
, RefAddr
);
155 const auto Attr_DW_FORM_ref1
= static_cast<dwarf::Attribute
>(Attr
++);
156 CUDie
.addAttribute(Attr_DW_FORM_ref1
, DW_FORM_ref1
, Data1
);
158 const auto Attr_DW_FORM_ref2
= static_cast<dwarf::Attribute
>(Attr
++);
159 CUDie
.addAttribute(Attr_DW_FORM_ref2
, DW_FORM_ref2
, Data2
);
161 const auto Attr_DW_FORM_ref4
= static_cast<dwarf::Attribute
>(Attr
++);
162 CUDie
.addAttribute(Attr_DW_FORM_ref4
, DW_FORM_ref4
, Data4
);
164 const auto Attr_DW_FORM_ref8
= static_cast<dwarf::Attribute
>(Attr
++);
165 CUDie
.addAttribute(Attr_DW_FORM_ref8
, DW_FORM_ref8
, Data8
);
167 const auto Attr_DW_FORM_ref_sig8
= static_cast<dwarf::Attribute
>(Attr
++);
169 CUDie
.addAttribute(Attr_DW_FORM_ref_sig8
, DW_FORM_ref_sig8
, Data8_2
);
171 const auto Attr_DW_FORM_ref_udata
= static_cast<dwarf::Attribute
>(Attr
++);
172 CUDie
.addAttribute(Attr_DW_FORM_ref_udata
, DW_FORM_ref_udata
, UData
[0]);
174 //----------------------------------------------------------------------
176 //----------------------------------------------------------------------
177 const auto Attr_DW_FORM_flag_true
= static_cast<dwarf::Attribute
>(Attr
++);
178 CUDie
.addAttribute(Attr_DW_FORM_flag_true
, DW_FORM_flag
, true);
180 const auto Attr_DW_FORM_flag_false
= static_cast<dwarf::Attribute
>(Attr
++);
181 CUDie
.addAttribute(Attr_DW_FORM_flag_false
, DW_FORM_flag
, false);
183 const auto Attr_DW_FORM_flag_present
= static_cast<dwarf::Attribute
>(Attr
++);
185 CUDie
.addAttribute(Attr_DW_FORM_flag_present
, DW_FORM_flag_present
);
187 //----------------------------------------------------------------------
188 // Test SLEB128 based forms
189 //----------------------------------------------------------------------
190 const auto Attr_DW_FORM_sdata
= static_cast<dwarf::Attribute
>(Attr
++);
191 CUDie
.addAttribute(Attr_DW_FORM_sdata
, DW_FORM_sdata
, SData
);
193 const auto Attr_DW_FORM_implicit_const
=
194 static_cast<dwarf::Attribute
>(Attr
++);
196 CUDie
.addAttribute(Attr_DW_FORM_implicit_const
, DW_FORM_implicit_const
,
199 //----------------------------------------------------------------------
200 // Test ULEB128 based forms
201 //----------------------------------------------------------------------
202 const auto Attr_DW_FORM_udata
= static_cast<dwarf::Attribute
>(Attr
++);
203 CUDie
.addAttribute(Attr_DW_FORM_udata
, DW_FORM_udata
, UData
[0]);
205 //----------------------------------------------------------------------
206 // Test DWARF32/DWARF64 forms
207 //----------------------------------------------------------------------
208 const auto Attr_DW_FORM_GNU_ref_alt
= static_cast<dwarf::Attribute
>(Attr
++);
209 CUDie
.addAttribute(Attr_DW_FORM_GNU_ref_alt
, DW_FORM_GNU_ref_alt
,
212 const auto Attr_DW_FORM_sec_offset
= static_cast<dwarf::Attribute
>(Attr
++);
214 CUDie
.addAttribute(Attr_DW_FORM_sec_offset
, DW_FORM_sec_offset
,
217 //----------------------------------------------------------------------
218 // Add an address at the end to make sure we can decode this value
219 //----------------------------------------------------------------------
220 const auto Attr_Last
= static_cast<dwarf::Attribute
>(Attr
++);
221 CUDie
.addAttribute(Attr_Last
, DW_FORM_addr
, AddrValue
);
223 //----------------------------------------------------------------------
224 // Generate the DWARF
225 //----------------------------------------------------------------------
226 StringRef FileBytes
= DG
->generate();
227 MemoryBufferRef
FileBuffer(FileBytes
, "dwarf");
228 auto Obj
= object::ObjectFile::createObjectFile(FileBuffer
);
229 EXPECT_TRUE((bool)Obj
);
230 std::unique_ptr
<DWARFContext
> DwarfContext
= DWARFContext::create(**Obj
);
231 uint32_t NumCUs
= DwarfContext
->getNumCompileUnits();
232 EXPECT_EQ(NumCUs
, 1u);
233 DWARFCompileUnit
*U
=
234 cast
<DWARFCompileUnit
>(DwarfContext
->getUnitAtIndex(0));
235 auto DieDG
= U
->getUnitDIE(false);
236 EXPECT_TRUE(DieDG
.isValid());
238 //----------------------------------------------------------------------
239 // Test address forms
240 //----------------------------------------------------------------------
241 EXPECT_EQ(AddrValue
, toAddress(DieDG
.find(Attr_DW_FORM_addr
), 0));
243 //----------------------------------------------------------------------
245 //----------------------------------------------------------------------
246 Optional
<DWARFFormValue
> FormValue
;
247 ArrayRef
<uint8_t> ExtractedBlockData
;
248 Optional
<ArrayRef
<uint8_t>> BlockDataOpt
;
250 FormValue
= DieDG
.find(Attr_DW_FORM_block
);
251 EXPECT_TRUE((bool)FormValue
);
252 BlockDataOpt
= FormValue
->getAsBlock();
253 EXPECT_TRUE(BlockDataOpt
.hasValue());
254 ExtractedBlockData
= BlockDataOpt
.getValue();
255 EXPECT_EQ(ExtractedBlockData
.size(), BlockSize
);
256 EXPECT_TRUE(memcmp(ExtractedBlockData
.data(), BlockData
, BlockSize
) == 0);
258 FormValue
= DieDG
.find(Attr_DW_FORM_block1
);
259 EXPECT_TRUE((bool)FormValue
);
260 BlockDataOpt
= FormValue
->getAsBlock();
261 EXPECT_TRUE(BlockDataOpt
.hasValue());
262 ExtractedBlockData
= BlockDataOpt
.getValue();
263 EXPECT_EQ(ExtractedBlockData
.size(), BlockSize
);
264 EXPECT_TRUE(memcmp(ExtractedBlockData
.data(), BlockData
, BlockSize
) == 0);
266 FormValue
= DieDG
.find(Attr_DW_FORM_block2
);
267 EXPECT_TRUE((bool)FormValue
);
268 BlockDataOpt
= FormValue
->getAsBlock();
269 EXPECT_TRUE(BlockDataOpt
.hasValue());
270 ExtractedBlockData
= BlockDataOpt
.getValue();
271 EXPECT_EQ(ExtractedBlockData
.size(), BlockSize
);
272 EXPECT_TRUE(memcmp(ExtractedBlockData
.data(), BlockData
, BlockSize
) == 0);
274 FormValue
= DieDG
.find(Attr_DW_FORM_block4
);
275 EXPECT_TRUE((bool)FormValue
);
276 BlockDataOpt
= FormValue
->getAsBlock();
277 EXPECT_TRUE(BlockDataOpt
.hasValue());
278 ExtractedBlockData
= BlockDataOpt
.getValue();
279 EXPECT_EQ(ExtractedBlockData
.size(), BlockSize
);
280 EXPECT_TRUE(memcmp(ExtractedBlockData
.data(), BlockData
, BlockSize
) == 0);
282 // Data16 is handled like a block.
284 FormValue
= DieDG
.find(Attr_DW_FORM_data16
);
285 EXPECT_TRUE((bool)FormValue
);
286 BlockDataOpt
= FormValue
->getAsBlock();
287 EXPECT_TRUE(BlockDataOpt
.hasValue());
288 ExtractedBlockData
= BlockDataOpt
.getValue();
289 EXPECT_EQ(ExtractedBlockData
.size(), 16u);
290 EXPECT_TRUE(memcmp(ExtractedBlockData
.data(), Data16
, 16) == 0);
293 //----------------------------------------------------------------------
295 //----------------------------------------------------------------------
296 EXPECT_EQ(Data1
, toUnsigned(DieDG
.find(Attr_DW_FORM_data1
), 0));
297 EXPECT_EQ(Data2
, toUnsigned(DieDG
.find(Attr_DW_FORM_data2
), 0));
298 EXPECT_EQ(Data4
, toUnsigned(DieDG
.find(Attr_DW_FORM_data4
), 0));
299 EXPECT_EQ(Data8
, toUnsigned(DieDG
.find(Attr_DW_FORM_data8
), 0));
301 //----------------------------------------------------------------------
303 //----------------------------------------------------------------------
304 auto ExtractedStringValue
= toString(DieDG
.find(Attr_DW_FORM_string
));
305 EXPECT_TRUE((bool)ExtractedStringValue
);
306 EXPECT_STREQ(StringValue
, *ExtractedStringValue
);
309 auto ExtractedStrxValue
= toString(DieDG
.find(Attr_DW_FORM_strx
));
310 EXPECT_TRUE((bool)ExtractedStrxValue
);
311 EXPECT_STREQ(StrxValue
, *ExtractedStrxValue
);
313 auto ExtractedStrx1Value
= toString(DieDG
.find(Attr_DW_FORM_strx1
));
314 EXPECT_TRUE((bool)ExtractedStrx1Value
);
315 EXPECT_STREQ(Strx1Value
, *ExtractedStrx1Value
);
317 auto ExtractedStrx2Value
= toString(DieDG
.find(Attr_DW_FORM_strx2
));
318 EXPECT_TRUE((bool)ExtractedStrx2Value
);
319 EXPECT_STREQ(Strx2Value
, *ExtractedStrx2Value
);
321 auto ExtractedStrx3Value
= toString(DieDG
.find(Attr_DW_FORM_strx3
));
322 EXPECT_TRUE((bool)ExtractedStrx3Value
);
323 EXPECT_STREQ(Strx3Value
, *ExtractedStrx3Value
);
325 auto ExtractedStrx4Value
= toString(DieDG
.find(Attr_DW_FORM_strx4
));
326 EXPECT_TRUE((bool)ExtractedStrx4Value
);
327 EXPECT_STREQ(Strx4Value
, *ExtractedStrx4Value
);
330 auto ExtractedStrpValue
= toString(DieDG
.find(Attr_DW_FORM_strp
));
331 EXPECT_TRUE((bool)ExtractedStrpValue
);
332 EXPECT_STREQ(StrpValue
, *ExtractedStrpValue
);
334 //----------------------------------------------------------------------
335 // Test reference forms
336 //----------------------------------------------------------------------
337 EXPECT_EQ(RefAddr
, toReference(DieDG
.find(Attr_DW_FORM_ref_addr
), 0));
338 EXPECT_EQ(Data1
, toReference(DieDG
.find(Attr_DW_FORM_ref1
), 0));
339 EXPECT_EQ(Data2
, toReference(DieDG
.find(Attr_DW_FORM_ref2
), 0));
340 EXPECT_EQ(Data4
, toReference(DieDG
.find(Attr_DW_FORM_ref4
), 0));
341 EXPECT_EQ(Data8
, toReference(DieDG
.find(Attr_DW_FORM_ref8
), 0));
343 EXPECT_EQ(Data8_2
, toReference(DieDG
.find(Attr_DW_FORM_ref_sig8
), 0));
345 EXPECT_EQ(UData
[0], toReference(DieDG
.find(Attr_DW_FORM_ref_udata
), 0));
347 //----------------------------------------------------------------------
349 //----------------------------------------------------------------------
350 EXPECT_EQ(1ULL, toUnsigned(DieDG
.find(Attr_DW_FORM_flag_true
), 0));
351 EXPECT_EQ(0ULL, toUnsigned(DieDG
.find(Attr_DW_FORM_flag_false
), 1));
353 EXPECT_EQ(1ULL, toUnsigned(DieDG
.find(Attr_DW_FORM_flag_present
), 0));
356 //----------------------------------------------------------------------
357 // Test SLEB128 based forms
358 //----------------------------------------------------------------------
359 EXPECT_EQ(SData
, toSigned(DieDG
.find(Attr_DW_FORM_sdata
), 0));
361 EXPECT_EQ(ICSData
, toSigned(DieDG
.find(Attr_DW_FORM_implicit_const
), 0));
364 //----------------------------------------------------------------------
365 // Test ULEB128 based forms
366 //----------------------------------------------------------------------
367 EXPECT_EQ(UData
[0], toUnsigned(DieDG
.find(Attr_DW_FORM_udata
), 0));
369 //----------------------------------------------------------------------
370 // Test DWARF32/DWARF64 forms
371 //----------------------------------------------------------------------
372 EXPECT_EQ(Dwarf32Values
[0],
373 toReference(DieDG
.find(Attr_DW_FORM_GNU_ref_alt
), 0));
375 EXPECT_EQ(Dwarf32Values
[1],
376 toSectionOffset(DieDG
.find(Attr_DW_FORM_sec_offset
), 0));
379 //----------------------------------------------------------------------
380 // Add an address at the end to make sure we can decode this value
381 //----------------------------------------------------------------------
382 EXPECT_EQ(AddrValue
, toAddress(DieDG
.find(Attr_Last
), 0));
385 TEST(DWARFDebugInfo
, TestDWARF32Version2Addr4AllForms
) {
386 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
388 typedef uint32_t AddrType
;
389 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2.
390 typedef AddrType RefAddrType
;
391 TestAllForms
<2, AddrType
, RefAddrType
>();
394 TEST(DWARFDebugInfo
, TestDWARF32Version2Addr8AllForms
) {
395 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
397 typedef uint64_t AddrType
;
398 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2.
399 typedef AddrType RefAddrType
;
400 TestAllForms
<2, AddrType
, RefAddrType
>();
403 TEST(DWARFDebugInfo
, TestDWARF32Version3Addr4AllForms
) {
404 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
406 typedef uint32_t AddrType
;
407 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later.
408 typedef uint32_t RefAddrType
;
409 TestAllForms
<3, AddrType
, RefAddrType
>();
412 TEST(DWARFDebugInfo
, TestDWARF32Version3Addr8AllForms
) {
413 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
415 typedef uint64_t AddrType
;
416 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
417 typedef uint32_t RefAddrType
;
418 TestAllForms
<3, AddrType
, RefAddrType
>();
421 TEST(DWARFDebugInfo
, TestDWARF32Version4Addr4AllForms
) {
422 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
424 typedef uint32_t AddrType
;
425 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
426 typedef uint32_t RefAddrType
;
427 TestAllForms
<4, AddrType
, RefAddrType
>();
430 TEST(DWARFDebugInfo
, TestDWARF32Version4Addr8AllForms
) {
431 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
433 typedef uint64_t AddrType
;
434 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
435 typedef uint32_t RefAddrType
;
436 TestAllForms
<4, AddrType
, RefAddrType
>();
439 TEST(DWARFDebugInfo
, TestDWARF32Version5Addr4AllForms
) {
440 // Test that we can decode all forms for DWARF32, version 5, with 4 byte
442 typedef uint32_t AddrType
;
443 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
444 typedef uint32_t RefAddrType
;
445 TestAllForms
<5, AddrType
, RefAddrType
>();
448 TEST(DWARFDebugInfo
, TestDWARF32Version5Addr8AllForms
) {
449 // Test that we can decode all forms for DWARF32, version 5, with 8 byte
451 typedef uint64_t AddrType
;
452 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
453 typedef uint32_t RefAddrType
;
454 TestAllForms
<5, AddrType
, RefAddrType
>();
457 template <uint16_t Version
, class AddrType
> void TestChildren() {
458 Triple Triple
= getDefaultTargetTripleForAddrSize(sizeof(AddrType
));
459 if (!isConfigurationSupported(Triple
))
462 // Test that we can decode DW_FORM_ref_addr values correctly in DWARF 2 with
463 // 4 byte addresses. DW_FORM_ref_addr values should be 4 bytes when using
466 auto ExpectedDG
= dwarfgen::Generator::create(Triple
, Version
);
467 ASSERT_THAT_EXPECTED(ExpectedDG
, Succeeded());
468 dwarfgen::Generator
*DG
= ExpectedDG
.get().get();
469 dwarfgen::CompileUnit
&CU
= DG
->addCompileUnit();
470 dwarfgen::DIE CUDie
= CU
.getUnitDIE();
472 CUDie
.addAttribute(DW_AT_name
, DW_FORM_strp
, "/tmp/main.c");
473 CUDie
.addAttribute(DW_AT_language
, DW_FORM_data2
, DW_LANG_C
);
475 dwarfgen::DIE SubprogramDie
= CUDie
.addChild(DW_TAG_subprogram
);
476 SubprogramDie
.addAttribute(DW_AT_name
, DW_FORM_strp
, "main");
477 SubprogramDie
.addAttribute(DW_AT_low_pc
, DW_FORM_addr
, 0x1000U
);
478 SubprogramDie
.addAttribute(DW_AT_high_pc
, DW_FORM_addr
, 0x2000U
);
480 dwarfgen::DIE IntDie
= CUDie
.addChild(DW_TAG_base_type
);
481 IntDie
.addAttribute(DW_AT_name
, DW_FORM_strp
, "int");
482 IntDie
.addAttribute(DW_AT_encoding
, DW_FORM_data1
, DW_ATE_signed
);
483 IntDie
.addAttribute(DW_AT_byte_size
, DW_FORM_data1
, 4);
485 dwarfgen::DIE ArgcDie
= SubprogramDie
.addChild(DW_TAG_formal_parameter
);
486 ArgcDie
.addAttribute(DW_AT_name
, DW_FORM_strp
, "argc");
487 // ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref4, IntDie);
488 ArgcDie
.addAttribute(DW_AT_type
, DW_FORM_ref_addr
, IntDie
);
490 StringRef FileBytes
= DG
->generate();
491 MemoryBufferRef
FileBuffer(FileBytes
, "dwarf");
492 auto Obj
= object::ObjectFile::createObjectFile(FileBuffer
);
493 EXPECT_TRUE((bool)Obj
);
494 std::unique_ptr
<DWARFContext
> DwarfContext
= DWARFContext::create(**Obj
);
496 // Verify the number of compile units is correct.
497 uint32_t NumCUs
= DwarfContext
->getNumCompileUnits();
498 EXPECT_EQ(NumCUs
, 1u);
499 DWARFCompileUnit
*U
=
500 cast
<DWARFCompileUnit
>(DwarfContext
->getUnitAtIndex(0));
502 // Get the compile unit DIE is valid.
503 auto DieDG
= U
->getUnitDIE(false);
504 EXPECT_TRUE(DieDG
.isValid());
506 // Verify the first child of the compile unit DIE is our subprogram.
507 auto SubprogramDieDG
= DieDG
.getFirstChild();
508 EXPECT_TRUE(SubprogramDieDG
.isValid());
509 EXPECT_EQ(SubprogramDieDG
.getTag(), DW_TAG_subprogram
);
511 // Verify the first child of the subprogram is our formal parameter.
512 auto ArgcDieDG
= SubprogramDieDG
.getFirstChild();
513 EXPECT_TRUE(ArgcDieDG
.isValid());
514 EXPECT_EQ(ArgcDieDG
.getTag(), DW_TAG_formal_parameter
);
516 // Verify our formal parameter has a NULL tag sibling.
517 auto NullDieDG
= ArgcDieDG
.getSibling();
518 EXPECT_TRUE(NullDieDG
.isValid());
520 EXPECT_EQ(NullDieDG
.getTag(), DW_TAG_null
);
521 EXPECT_TRUE(!NullDieDG
.getSibling().isValid());
522 EXPECT_TRUE(!NullDieDG
.getFirstChild().isValid());
525 // Verify the sibling of our subprogram is our integer base type.
526 auto IntDieDG
= SubprogramDieDG
.getSibling();
527 EXPECT_TRUE(IntDieDG
.isValid());
528 EXPECT_EQ(IntDieDG
.getTag(), DW_TAG_base_type
);
530 // Verify the sibling of our subprogram is our integer base is a NULL tag.
531 NullDieDG
= IntDieDG
.getSibling();
532 EXPECT_TRUE(NullDieDG
.isValid());
534 EXPECT_EQ(NullDieDG
.getTag(), DW_TAG_null
);
535 EXPECT_TRUE(!NullDieDG
.getSibling().isValid());
536 EXPECT_TRUE(!NullDieDG
.getFirstChild().isValid());
539 // Verify the previous sibling of our subprogram is our integer base type.
540 IntDieDG
= NullDieDG
.getPreviousSibling();
541 EXPECT_TRUE(IntDieDG
.isValid());
542 EXPECT_EQ(IntDieDG
.getTag(), DW_TAG_base_type
);
545 TEST(DWARFDebugInfo
, TestDWARF32Version2Addr4Children
) {
546 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
548 typedef uint32_t AddrType
;
549 TestChildren
<2, AddrType
>();
552 TEST(DWARFDebugInfo
, TestDWARF32Version2Addr8Children
) {
553 // Test that we can decode all forms for DWARF32, version 2, with 8 byte
555 typedef uint64_t AddrType
;
556 TestChildren
<2, AddrType
>();
559 TEST(DWARFDebugInfo
, TestDWARF32Version3Addr4Children
) {
560 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
562 typedef uint32_t AddrType
;
563 TestChildren
<3, AddrType
>();
566 TEST(DWARFDebugInfo
, TestDWARF32Version3Addr8Children
) {
567 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
569 typedef uint64_t AddrType
;
570 TestChildren
<3, AddrType
>();
573 TEST(DWARFDebugInfo
, TestDWARF32Version4Addr4Children
) {
574 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
576 typedef uint32_t AddrType
;
577 TestChildren
<4, AddrType
>();
580 TEST(DWARFDebugInfo
, TestDWARF32Version4Addr8Children
) {
581 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
583 typedef uint64_t AddrType
;
584 TestChildren
<4, AddrType
>();
587 template <uint16_t Version
, class AddrType
> void TestReferences() {
588 Triple Triple
= getDefaultTargetTripleForAddrSize(sizeof(AddrType
));
589 if (!isConfigurationSupported(Triple
))
592 // Test that we can decode DW_FORM_refXXX values correctly in DWARF.
593 auto ExpectedDG
= dwarfgen::Generator::create(Triple
, Version
);
594 ASSERT_THAT_EXPECTED(ExpectedDG
, Succeeded());
595 dwarfgen::Generator
*DG
= ExpectedDG
.get().get();
596 dwarfgen::CompileUnit
&CU1
= DG
->addCompileUnit();
597 dwarfgen::CompileUnit
&CU2
= DG
->addCompileUnit();
599 dwarfgen::DIE CU1Die
= CU1
.getUnitDIE();
600 CU1Die
.addAttribute(DW_AT_name
, DW_FORM_strp
, "/tmp/main.c");
601 CU1Die
.addAttribute(DW_AT_language
, DW_FORM_data2
, DW_LANG_C
);
603 dwarfgen::DIE CU1TypeDie
= CU1Die
.addChild(DW_TAG_base_type
);
604 CU1TypeDie
.addAttribute(DW_AT_name
, DW_FORM_strp
, "int");
605 CU1TypeDie
.addAttribute(DW_AT_encoding
, DW_FORM_data1
, DW_ATE_signed
);
606 CU1TypeDie
.addAttribute(DW_AT_byte_size
, DW_FORM_data1
, 4);
608 dwarfgen::DIE CU1Ref1Die
= CU1Die
.addChild(DW_TAG_variable
);
609 CU1Ref1Die
.addAttribute(DW_AT_name
, DW_FORM_strp
, "CU1Ref1");
610 CU1Ref1Die
.addAttribute(DW_AT_type
, DW_FORM_ref1
, CU1TypeDie
);
612 dwarfgen::DIE CU1Ref2Die
= CU1Die
.addChild(DW_TAG_variable
);
613 CU1Ref2Die
.addAttribute(DW_AT_name
, DW_FORM_strp
, "CU1Ref2");
614 CU1Ref2Die
.addAttribute(DW_AT_type
, DW_FORM_ref2
, CU1TypeDie
);
616 dwarfgen::DIE CU1Ref4Die
= CU1Die
.addChild(DW_TAG_variable
);
617 CU1Ref4Die
.addAttribute(DW_AT_name
, DW_FORM_strp
, "CU1Ref4");
618 CU1Ref4Die
.addAttribute(DW_AT_type
, DW_FORM_ref4
, CU1TypeDie
);
620 dwarfgen::DIE CU1Ref8Die
= CU1Die
.addChild(DW_TAG_variable
);
621 CU1Ref8Die
.addAttribute(DW_AT_name
, DW_FORM_strp
, "CU1Ref8");
622 CU1Ref8Die
.addAttribute(DW_AT_type
, DW_FORM_ref8
, CU1TypeDie
);
624 dwarfgen::DIE CU1RefAddrDie
= CU1Die
.addChild(DW_TAG_variable
);
625 CU1RefAddrDie
.addAttribute(DW_AT_name
, DW_FORM_strp
, "CU1RefAddr");
626 CU1RefAddrDie
.addAttribute(DW_AT_type
, DW_FORM_ref_addr
, CU1TypeDie
);
628 dwarfgen::DIE CU2Die
= CU2
.getUnitDIE();
629 CU2Die
.addAttribute(DW_AT_name
, DW_FORM_strp
, "/tmp/foo.c");
630 CU2Die
.addAttribute(DW_AT_language
, DW_FORM_data2
, DW_LANG_C
);
632 dwarfgen::DIE CU2TypeDie
= CU2Die
.addChild(DW_TAG_base_type
);
633 CU2TypeDie
.addAttribute(DW_AT_name
, DW_FORM_strp
, "float");
634 CU2TypeDie
.addAttribute(DW_AT_encoding
, DW_FORM_data1
, DW_ATE_float
);
635 CU2TypeDie
.addAttribute(DW_AT_byte_size
, DW_FORM_data1
, 4);
637 dwarfgen::DIE CU2Ref1Die
= CU2Die
.addChild(DW_TAG_variable
);
638 CU2Ref1Die
.addAttribute(DW_AT_name
, DW_FORM_strp
, "CU2Ref1");
639 CU2Ref1Die
.addAttribute(DW_AT_type
, DW_FORM_ref1
, CU2TypeDie
);
641 dwarfgen::DIE CU2Ref2Die
= CU2Die
.addChild(DW_TAG_variable
);
642 CU2Ref2Die
.addAttribute(DW_AT_name
, DW_FORM_strp
, "CU2Ref2");
643 CU2Ref2Die
.addAttribute(DW_AT_type
, DW_FORM_ref2
, CU2TypeDie
);
645 dwarfgen::DIE CU2Ref4Die
= CU2Die
.addChild(DW_TAG_variable
);
646 CU2Ref4Die
.addAttribute(DW_AT_name
, DW_FORM_strp
, "CU2Ref4");
647 CU2Ref4Die
.addAttribute(DW_AT_type
, DW_FORM_ref4
, CU2TypeDie
);
649 dwarfgen::DIE CU2Ref8Die
= CU2Die
.addChild(DW_TAG_variable
);
650 CU2Ref8Die
.addAttribute(DW_AT_name
, DW_FORM_strp
, "CU2Ref8");
651 CU2Ref8Die
.addAttribute(DW_AT_type
, DW_FORM_ref8
, CU2TypeDie
);
653 dwarfgen::DIE CU2RefAddrDie
= CU2Die
.addChild(DW_TAG_variable
);
654 CU2RefAddrDie
.addAttribute(DW_AT_name
, DW_FORM_strp
, "CU2RefAddr");
655 CU2RefAddrDie
.addAttribute(DW_AT_type
, DW_FORM_ref_addr
, CU2TypeDie
);
657 // Refer to a type in CU1 from CU2
658 dwarfgen::DIE CU2ToCU1RefAddrDie
= CU2Die
.addChild(DW_TAG_variable
);
659 CU2ToCU1RefAddrDie
.addAttribute(DW_AT_name
, DW_FORM_strp
, "CU2ToCU1RefAddr");
660 CU2ToCU1RefAddrDie
.addAttribute(DW_AT_type
, DW_FORM_ref_addr
, CU1TypeDie
);
662 // Refer to a type in CU2 from CU1
663 dwarfgen::DIE CU1ToCU2RefAddrDie
= CU1Die
.addChild(DW_TAG_variable
);
664 CU1ToCU2RefAddrDie
.addAttribute(DW_AT_name
, DW_FORM_strp
, "CU1ToCU2RefAddr");
665 CU1ToCU2RefAddrDie
.addAttribute(DW_AT_type
, DW_FORM_ref_addr
, CU2TypeDie
);
667 StringRef FileBytes
= DG
->generate();
668 MemoryBufferRef
FileBuffer(FileBytes
, "dwarf");
669 auto Obj
= object::ObjectFile::createObjectFile(FileBuffer
);
670 EXPECT_TRUE((bool)Obj
);
671 std::unique_ptr
<DWARFContext
> DwarfContext
= DWARFContext::create(**Obj
);
673 // Verify the number of compile units is correct.
674 uint32_t NumCUs
= DwarfContext
->getNumCompileUnits();
675 EXPECT_EQ(NumCUs
, 2u);
676 DWARFCompileUnit
*U1
=
677 cast
<DWARFCompileUnit
>(DwarfContext
->getUnitAtIndex(0));
678 DWARFCompileUnit
*U2
=
679 cast
<DWARFCompileUnit
>(DwarfContext
->getUnitAtIndex(1));
681 // Get the compile unit DIE is valid.
682 auto Unit1DieDG
= U1
->getUnitDIE(false);
683 EXPECT_TRUE(Unit1DieDG
.isValid());
685 auto Unit2DieDG
= U2
->getUnitDIE(false);
686 EXPECT_TRUE(Unit2DieDG
.isValid());
688 // Verify the first child of the compile unit 1 DIE is our int base type.
689 auto CU1TypeDieDG
= Unit1DieDG
.getFirstChild();
690 EXPECT_TRUE(CU1TypeDieDG
.isValid());
691 EXPECT_EQ(CU1TypeDieDG
.getTag(), DW_TAG_base_type
);
692 EXPECT_EQ(DW_ATE_signed
, toUnsigned(CU1TypeDieDG
.find(DW_AT_encoding
), 0));
694 // Verify the first child of the compile unit 2 DIE is our float base type.
695 auto CU2TypeDieDG
= Unit2DieDG
.getFirstChild();
696 EXPECT_TRUE(CU2TypeDieDG
.isValid());
697 EXPECT_EQ(CU2TypeDieDG
.getTag(), DW_TAG_base_type
);
698 EXPECT_EQ(DW_ATE_float
, toUnsigned(CU2TypeDieDG
.find(DW_AT_encoding
), 0));
700 // Verify the sibling of the base type DIE is our Ref1 DIE and that its
701 // DW_AT_type points to our base type DIE.
702 auto CU1Ref1DieDG
= CU1TypeDieDG
.getSibling();
703 EXPECT_TRUE(CU1Ref1DieDG
.isValid());
704 EXPECT_EQ(CU1Ref1DieDG
.getTag(), DW_TAG_variable
);
705 EXPECT_EQ(CU1TypeDieDG
.getOffset(),
706 toReference(CU1Ref1DieDG
.find(DW_AT_type
), -1ULL));
707 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our
708 // base type DIE in CU1.
709 auto CU1Ref2DieDG
= CU1Ref1DieDG
.getSibling();
710 EXPECT_TRUE(CU1Ref2DieDG
.isValid());
711 EXPECT_EQ(CU1Ref2DieDG
.getTag(), DW_TAG_variable
);
712 EXPECT_EQ(CU1TypeDieDG
.getOffset(),
713 toReference(CU1Ref2DieDG
.find(DW_AT_type
), -1ULL));
715 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our
716 // base type DIE in CU1.
717 auto CU1Ref4DieDG
= CU1Ref2DieDG
.getSibling();
718 EXPECT_TRUE(CU1Ref4DieDG
.isValid());
719 EXPECT_EQ(CU1Ref4DieDG
.getTag(), DW_TAG_variable
);
720 EXPECT_EQ(CU1TypeDieDG
.getOffset(),
721 toReference(CU1Ref4DieDG
.find(DW_AT_type
), -1ULL));
723 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our
724 // base type DIE in CU1.
725 auto CU1Ref8DieDG
= CU1Ref4DieDG
.getSibling();
726 EXPECT_TRUE(CU1Ref8DieDG
.isValid());
727 EXPECT_EQ(CU1Ref8DieDG
.getTag(), DW_TAG_variable
);
728 EXPECT_EQ(CU1TypeDieDG
.getOffset(),
729 toReference(CU1Ref8DieDG
.find(DW_AT_type
), -1ULL));
731 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our
732 // base type DIE in CU1.
733 auto CU1RefAddrDieDG
= CU1Ref8DieDG
.getSibling();
734 EXPECT_TRUE(CU1RefAddrDieDG
.isValid());
735 EXPECT_EQ(CU1RefAddrDieDG
.getTag(), DW_TAG_variable
);
736 EXPECT_EQ(CU1TypeDieDG
.getOffset(),
737 toReference(CU1RefAddrDieDG
.find(DW_AT_type
), -1ULL));
739 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its
740 // DW_AT_type points to our base type DIE.
741 auto CU1ToCU2RefAddrDieDG
= CU1RefAddrDieDG
.getSibling();
742 EXPECT_TRUE(CU1ToCU2RefAddrDieDG
.isValid());
743 EXPECT_EQ(CU1ToCU2RefAddrDieDG
.getTag(), DW_TAG_variable
);
744 EXPECT_EQ(CU2TypeDieDG
.getOffset(),
745 toReference(CU1ToCU2RefAddrDieDG
.find(DW_AT_type
), -1ULL));
747 // Verify the sibling of the base type DIE is our Ref1 DIE and that its
748 // DW_AT_type points to our base type DIE.
749 auto CU2Ref1DieDG
= CU2TypeDieDG
.getSibling();
750 EXPECT_TRUE(CU2Ref1DieDG
.isValid());
751 EXPECT_EQ(CU2Ref1DieDG
.getTag(), DW_TAG_variable
);
752 EXPECT_EQ(CU2TypeDieDG
.getOffset(),
753 toReference(CU2Ref1DieDG
.find(DW_AT_type
), -1ULL));
754 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our
755 // base type DIE in CU2.
756 auto CU2Ref2DieDG
= CU2Ref1DieDG
.getSibling();
757 EXPECT_TRUE(CU2Ref2DieDG
.isValid());
758 EXPECT_EQ(CU2Ref2DieDG
.getTag(), DW_TAG_variable
);
759 EXPECT_EQ(CU2TypeDieDG
.getOffset(),
760 toReference(CU2Ref2DieDG
.find(DW_AT_type
), -1ULL));
762 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our
763 // base type DIE in CU2.
764 auto CU2Ref4DieDG
= CU2Ref2DieDG
.getSibling();
765 EXPECT_TRUE(CU2Ref4DieDG
.isValid());
766 EXPECT_EQ(CU2Ref4DieDG
.getTag(), DW_TAG_variable
);
767 EXPECT_EQ(CU2TypeDieDG
.getOffset(),
768 toReference(CU2Ref4DieDG
.find(DW_AT_type
), -1ULL));
770 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our
771 // base type DIE in CU2.
772 auto CU2Ref8DieDG
= CU2Ref4DieDG
.getSibling();
773 EXPECT_TRUE(CU2Ref8DieDG
.isValid());
774 EXPECT_EQ(CU2Ref8DieDG
.getTag(), DW_TAG_variable
);
775 EXPECT_EQ(CU2TypeDieDG
.getOffset(),
776 toReference(CU2Ref8DieDG
.find(DW_AT_type
), -1ULL));
778 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our
779 // base type DIE in CU2.
780 auto CU2RefAddrDieDG
= CU2Ref8DieDG
.getSibling();
781 EXPECT_TRUE(CU2RefAddrDieDG
.isValid());
782 EXPECT_EQ(CU2RefAddrDieDG
.getTag(), DW_TAG_variable
);
783 EXPECT_EQ(CU2TypeDieDG
.getOffset(),
784 toReference(CU2RefAddrDieDG
.find(DW_AT_type
), -1ULL));
786 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its
787 // DW_AT_type points to our base type DIE.
788 auto CU2ToCU1RefAddrDieDG
= CU2RefAddrDieDG
.getSibling();
789 EXPECT_TRUE(CU2ToCU1RefAddrDieDG
.isValid());
790 EXPECT_EQ(CU2ToCU1RefAddrDieDG
.getTag(), DW_TAG_variable
);
791 EXPECT_EQ(CU1TypeDieDG
.getOffset(),
792 toReference(CU2ToCU1RefAddrDieDG
.find(DW_AT_type
), -1ULL));
795 TEST(DWARFDebugInfo
, TestDWARF32Version2Addr4References
) {
796 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
798 typedef uint32_t AddrType
;
799 TestReferences
<2, AddrType
>();
802 TEST(DWARFDebugInfo
, TestDWARF32Version2Addr8References
) {
803 // Test that we can decode all forms for DWARF32, version 2, with 8 byte
805 typedef uint64_t AddrType
;
806 TestReferences
<2, AddrType
>();
809 TEST(DWARFDebugInfo
, TestDWARF32Version3Addr4References
) {
810 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
812 typedef uint32_t AddrType
;
813 TestReferences
<3, AddrType
>();
816 TEST(DWARFDebugInfo
, TestDWARF32Version3Addr8References
) {
817 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
819 typedef uint64_t AddrType
;
820 TestReferences
<3, AddrType
>();
823 TEST(DWARFDebugInfo
, TestDWARF32Version4Addr4References
) {
824 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
826 typedef uint32_t AddrType
;
827 TestReferences
<4, AddrType
>();
830 TEST(DWARFDebugInfo
, TestDWARF32Version4Addr8References
) {
831 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
833 typedef uint64_t AddrType
;
834 TestReferences
<4, AddrType
>();
837 template <uint16_t Version
, class AddrType
> void TestAddresses() {
838 Triple Triple
= getDefaultTargetTripleForAddrSize(sizeof(AddrType
));
839 if (!isConfigurationSupported(Triple
))
842 // Test the DWARF APIs related to accessing the DW_AT_low_pc and
844 const bool SupportsHighPCAsOffset
= Version
>= 4;
845 auto ExpectedDG
= dwarfgen::Generator::create(Triple
, Version
);
846 ASSERT_THAT_EXPECTED(ExpectedDG
, Succeeded());
847 dwarfgen::Generator
*DG
= ExpectedDG
.get().get();
848 dwarfgen::CompileUnit
&CU
= DG
->addCompileUnit();
849 dwarfgen::DIE CUDie
= CU
.getUnitDIE();
851 CUDie
.addAttribute(DW_AT_name
, DW_FORM_strp
, "/tmp/main.c");
852 CUDie
.addAttribute(DW_AT_language
, DW_FORM_data2
, DW_LANG_C
);
854 // Create a subprogram DIE with no low or high PC.
855 dwarfgen::DIE SubprogramNoPC
= CUDie
.addChild(DW_TAG_subprogram
);
856 SubprogramNoPC
.addAttribute(DW_AT_name
, DW_FORM_strp
, "no_pc");
858 // Create a subprogram DIE with a low PC only.
859 dwarfgen::DIE SubprogramLowPC
= CUDie
.addChild(DW_TAG_subprogram
);
860 SubprogramLowPC
.addAttribute(DW_AT_name
, DW_FORM_strp
, "low_pc");
861 const uint64_t ActualLowPC
= 0x1000;
862 const uint64_t ActualHighPC
= 0x2000;
863 const uint64_t ActualHighPCOffset
= ActualHighPC
- ActualLowPC
;
864 SubprogramLowPC
.addAttribute(DW_AT_low_pc
, DW_FORM_addr
, ActualLowPC
);
866 // Create a subprogram DIE with a low and high PC.
867 dwarfgen::DIE SubprogramLowHighPC
= CUDie
.addChild(DW_TAG_subprogram
);
868 SubprogramLowHighPC
.addAttribute(DW_AT_name
, DW_FORM_strp
, "low_high_pc");
869 SubprogramLowHighPC
.addAttribute(DW_AT_low_pc
, DW_FORM_addr
, ActualLowPC
);
870 // Encode the high PC as an offset from the low PC if supported.
871 if (SupportsHighPCAsOffset
)
872 SubprogramLowHighPC
.addAttribute(DW_AT_high_pc
, DW_FORM_data4
,
875 SubprogramLowHighPC
.addAttribute(DW_AT_high_pc
, DW_FORM_addr
, ActualHighPC
);
877 StringRef FileBytes
= DG
->generate();
878 MemoryBufferRef
FileBuffer(FileBytes
, "dwarf");
879 auto Obj
= object::ObjectFile::createObjectFile(FileBuffer
);
880 EXPECT_TRUE((bool)Obj
);
881 std::unique_ptr
<DWARFContext
> DwarfContext
= DWARFContext::create(**Obj
);
883 // Verify the number of compile units is correct.
884 uint32_t NumCUs
= DwarfContext
->getNumCompileUnits();
885 EXPECT_EQ(NumCUs
, 1u);
886 DWARFCompileUnit
*U
=
887 cast
<DWARFCompileUnit
>(DwarfContext
->getUnitAtIndex(0));
889 // Get the compile unit DIE is valid.
890 auto DieDG
= U
->getUnitDIE(false);
891 EXPECT_TRUE(DieDG
.isValid());
893 uint64_t LowPC
, HighPC
, SectionIndex
;
894 Optional
<uint64_t> OptU64
;
895 // Verify the that our subprogram with no PC value fails appropriately when
896 // asked for any PC values.
897 auto SubprogramDieNoPC
= DieDG
.getFirstChild();
898 EXPECT_TRUE(SubprogramDieNoPC
.isValid());
899 EXPECT_EQ(SubprogramDieNoPC
.getTag(), DW_TAG_subprogram
);
900 OptU64
= toAddress(SubprogramDieNoPC
.find(DW_AT_low_pc
));
901 EXPECT_FALSE((bool)OptU64
);
902 OptU64
= toAddress(SubprogramDieNoPC
.find(DW_AT_high_pc
));
903 EXPECT_FALSE((bool)OptU64
);
904 EXPECT_FALSE(SubprogramDieNoPC
.getLowAndHighPC(LowPC
, HighPC
, SectionIndex
));
905 OptU64
= toAddress(SubprogramDieNoPC
.find(DW_AT_high_pc
));
906 EXPECT_FALSE((bool)OptU64
);
907 OptU64
= toUnsigned(SubprogramDieNoPC
.find(DW_AT_high_pc
));
908 EXPECT_FALSE((bool)OptU64
);
909 OptU64
= SubprogramDieNoPC
.getHighPC(ActualLowPC
);
910 EXPECT_FALSE((bool)OptU64
);
911 EXPECT_FALSE(SubprogramDieNoPC
.getLowAndHighPC(LowPC
, HighPC
, SectionIndex
));
913 // Verify the that our subprogram with only a low PC value succeeds when
914 // we ask for the Low PC, but fails appropriately when asked for the high PC
915 // or both low and high PC values.
916 auto SubprogramDieLowPC
= SubprogramDieNoPC
.getSibling();
917 EXPECT_TRUE(SubprogramDieLowPC
.isValid());
918 EXPECT_EQ(SubprogramDieLowPC
.getTag(), DW_TAG_subprogram
);
919 OptU64
= toAddress(SubprogramDieLowPC
.find(DW_AT_low_pc
));
920 EXPECT_TRUE((bool)OptU64
);
921 EXPECT_EQ(OptU64
.getValue(), ActualLowPC
);
922 OptU64
= toAddress(SubprogramDieLowPC
.find(DW_AT_high_pc
));
923 EXPECT_FALSE((bool)OptU64
);
924 OptU64
= toUnsigned(SubprogramDieLowPC
.find(DW_AT_high_pc
));
925 EXPECT_FALSE((bool)OptU64
);
926 OptU64
= SubprogramDieLowPC
.getHighPC(ActualLowPC
);
927 EXPECT_FALSE((bool)OptU64
);
928 EXPECT_FALSE(SubprogramDieLowPC
.getLowAndHighPC(LowPC
, HighPC
, SectionIndex
));
930 // Verify the that our subprogram with only a low PC value succeeds when
931 // we ask for the Low PC, but fails appropriately when asked for the high PC
932 // or both low and high PC values.
933 auto SubprogramDieLowHighPC
= SubprogramDieLowPC
.getSibling();
934 EXPECT_TRUE(SubprogramDieLowHighPC
.isValid());
935 EXPECT_EQ(SubprogramDieLowHighPC
.getTag(), DW_TAG_subprogram
);
936 OptU64
= toAddress(SubprogramDieLowHighPC
.find(DW_AT_low_pc
));
937 EXPECT_TRUE((bool)OptU64
);
938 EXPECT_EQ(OptU64
.getValue(), ActualLowPC
);
939 // Get the high PC as an address. This should succeed if the high PC was
940 // encoded as an address and fail if the high PC was encoded as an offset.
941 OptU64
= toAddress(SubprogramDieLowHighPC
.find(DW_AT_high_pc
));
942 if (SupportsHighPCAsOffset
) {
943 EXPECT_FALSE((bool)OptU64
);
945 EXPECT_TRUE((bool)OptU64
);
946 EXPECT_EQ(OptU64
.getValue(), ActualHighPC
);
948 // Get the high PC as an unsigned constant. This should succeed if the high PC
949 // was encoded as an offset and fail if the high PC was encoded as an address.
950 OptU64
= toUnsigned(SubprogramDieLowHighPC
.find(DW_AT_high_pc
));
951 if (SupportsHighPCAsOffset
) {
952 EXPECT_TRUE((bool)OptU64
);
953 EXPECT_EQ(OptU64
.getValue(), ActualHighPCOffset
);
955 EXPECT_FALSE((bool)OptU64
);
958 OptU64
= SubprogramDieLowHighPC
.getHighPC(ActualLowPC
);
959 EXPECT_TRUE((bool)OptU64
);
960 EXPECT_EQ(OptU64
.getValue(), ActualHighPC
);
962 EXPECT_TRUE(SubprogramDieLowHighPC
.getLowAndHighPC(LowPC
, HighPC
, SectionIndex
));
963 EXPECT_EQ(LowPC
, ActualLowPC
);
964 EXPECT_EQ(HighPC
, ActualHighPC
);
967 TEST(DWARFDebugInfo
, TestDWARF32Version2Addr4Addresses
) {
968 // Test that we can decode address values in DWARF32, version 2, with 4 byte
970 typedef uint32_t AddrType
;
971 TestAddresses
<2, AddrType
>();
974 TEST(DWARFDebugInfo
, TestDWARF32Version2Addr8Addresses
) {
975 // Test that we can decode address values in DWARF32, version 2, with 8 byte
977 typedef uint64_t AddrType
;
978 TestAddresses
<2, AddrType
>();
981 TEST(DWARFDebugInfo
, TestDWARF32Version3Addr4Addresses
) {
982 // Test that we can decode address values in DWARF32, version 3, with 4 byte
984 typedef uint32_t AddrType
;
985 TestAddresses
<3, AddrType
>();
988 TEST(DWARFDebugInfo
, TestDWARF32Version3Addr8Addresses
) {
989 // Test that we can decode address values in DWARF32, version 3, with 8 byte
991 typedef uint64_t AddrType
;
992 TestAddresses
<3, AddrType
>();
995 TEST(DWARFDebugInfo
, TestDWARF32Version4Addr4Addresses
) {
996 // Test that we can decode address values in DWARF32, version 4, with 4 byte
998 typedef uint32_t AddrType
;
999 TestAddresses
<4, AddrType
>();
1002 TEST(DWARFDebugInfo
, TestDWARF32Version4Addr8Addresses
) {
1003 // Test that we can decode address values in DWARF32, version 4, with 8 byte
1005 typedef uint64_t AddrType
;
1006 TestAddresses
<4, AddrType
>();
1009 TEST(DWARFDebugInfo
, TestStringOffsets
) {
1010 Triple Triple
= getNormalizedDefaultTargetTriple();
1011 if (!isConfigurationSupported(Triple
))
1014 const char *String1
= "Hello";
1015 const char *String2
= "World";
1017 auto ExpectedDG
= dwarfgen::Generator::create(Triple
, 5);
1018 ASSERT_THAT_EXPECTED(ExpectedDG
, Succeeded());
1019 dwarfgen::Generator
*DG
= ExpectedDG
.get().get();
1020 dwarfgen::CompileUnit
&CU
= DG
->addCompileUnit();
1021 dwarfgen::DIE CUDie
= CU
.getUnitDIE();
1023 CUDie
.addStrOffsetsBaseAttribute();
1025 uint16_t Attr
= DW_AT_lo_user
;
1027 // Create our strings. First we create a non-indexed reference to String1,
1028 // followed by an indexed String2. Finally, we add an indexed reference to
1030 const auto Attr1
= static_cast<dwarf::Attribute
>(Attr
++);
1031 CUDie
.addAttribute(Attr1
, DW_FORM_strp
, String1
);
1033 const auto Attr2
= static_cast<dwarf::Attribute
>(Attr
++);
1034 CUDie
.addAttribute(Attr2
, DW_FORM_strx
, String2
);
1036 const auto Attr3
= static_cast<dwarf::Attribute
>(Attr
++);
1037 CUDie
.addAttribute(Attr3
, DW_FORM_strx
, String1
);
1039 // Generate the DWARF
1040 StringRef FileBytes
= DG
->generate();
1041 MemoryBufferRef
FileBuffer(FileBytes
, "dwarf");
1042 auto Obj
= object::ObjectFile::createObjectFile(FileBuffer
);
1043 ASSERT_TRUE((bool)Obj
);
1044 std::unique_ptr
<DWARFContext
> DwarfContext
= DWARFContext::create(**Obj
);
1045 uint32_t NumCUs
= DwarfContext
->getNumCompileUnits();
1046 ASSERT_EQ(NumCUs
, 1u);
1047 DWARFUnit
*U
= DwarfContext
->getUnitAtIndex(0);
1048 auto DieDG
= U
->getUnitDIE(false);
1049 ASSERT_TRUE(DieDG
.isValid());
1051 // Now make sure the string offsets came out properly. Attr2 should have index
1052 // 0 (because it was the first indexed string) even though the string itself
1053 // was added eariler.
1054 auto Extracted1
= toString(DieDG
.find(Attr1
));
1055 ASSERT_TRUE((bool)Extracted1
);
1056 EXPECT_STREQ(String1
, *Extracted1
);
1058 Optional
<DWARFFormValue
> Form2
= DieDG
.find(Attr2
);
1059 ASSERT_TRUE((bool)Form2
);
1060 EXPECT_EQ(0u, Form2
->getRawUValue());
1061 auto Extracted2
= toString(Form2
);
1062 ASSERT_TRUE((bool)Extracted2
);
1063 EXPECT_STREQ(String2
, *Extracted2
);
1065 Optional
<DWARFFormValue
> Form3
= DieDG
.find(Attr3
);
1066 ASSERT_TRUE((bool)Form3
);
1067 EXPECT_EQ(1u, Form3
->getRawUValue());
1068 auto Extracted3
= toString(Form3
);
1069 ASSERT_TRUE((bool)Extracted3
);
1070 EXPECT_STREQ(String1
, *Extracted3
);
1073 TEST(DWARFDebugInfo
, TestEmptyStringOffsets
) {
1074 Triple Triple
= getNormalizedDefaultTargetTriple();
1075 if (!isConfigurationSupported(Triple
))
1078 const char *String1
= "Hello";
1080 auto ExpectedDG
= dwarfgen::Generator::create(Triple
, 5);
1081 ASSERT_THAT_EXPECTED(ExpectedDG
, Succeeded());
1082 dwarfgen::Generator
*DG
= ExpectedDG
.get().get();
1083 dwarfgen::CompileUnit
&CU
= DG
->addCompileUnit();
1084 dwarfgen::DIE CUDie
= CU
.getUnitDIE();
1086 uint16_t Attr
= DW_AT_lo_user
;
1088 // We shall insert only one string. It will be referenced directly.
1089 const auto Attr1
= static_cast<dwarf::Attribute
>(Attr
++);
1090 CUDie
.addAttribute(Attr1
, DW_FORM_strp
, String1
);
1092 // Generate the DWARF
1093 StringRef FileBytes
= DG
->generate();
1094 MemoryBufferRef
FileBuffer(FileBytes
, "dwarf");
1095 auto Obj
= object::ObjectFile::createObjectFile(FileBuffer
);
1096 ASSERT_TRUE((bool)Obj
);
1097 std::unique_ptr
<DWARFContext
> DwarfContext
= DWARFContext::create(**Obj
);
1099 DwarfContext
->getDWARFObj().getStrOffsetsSection().Data
.empty());
1102 TEST(DWARFDebugInfo
, TestRelations
) {
1103 Triple Triple
= getNormalizedDefaultTargetTriple();
1104 if (!isConfigurationSupported(Triple
))
1107 // Test the DWARF APIs related to accessing the DW_AT_low_pc and
1109 uint16_t Version
= 4;
1110 auto ExpectedDG
= dwarfgen::Generator::create(Triple
, Version
);
1111 ASSERT_THAT_EXPECTED(ExpectedDG
, Succeeded());
1112 dwarfgen::Generator
*DG
= ExpectedDG
.get().get();
1113 dwarfgen::CompileUnit
&CU
= DG
->addCompileUnit();
1115 enum class Tag
: uint16_t {
1116 A
= dwarf::DW_TAG_lo_user
,
1125 // Scope to allow us to re-use the same DIE names
1127 // Create DWARF tree that looks like:
1137 dwarfgen::DIE CUDie
= CU
.getUnitDIE();
1138 dwarfgen::DIE A
= CUDie
.addChild((dwarf::Tag
)Tag::A
);
1139 A
.addChild((dwarf::Tag
)Tag::B
);
1140 dwarfgen::DIE C
= A
.addChild((dwarf::Tag
)Tag::C
);
1141 dwarfgen::DIE D
= A
.addChild((dwarf::Tag
)Tag::D
);
1142 C
.addChild((dwarf::Tag
)Tag::C1
);
1143 C
.addChild((dwarf::Tag
)Tag::C2
);
1144 D
.addChild((dwarf::Tag
)Tag::D1
);
1147 MemoryBufferRef
FileBuffer(DG
->generate(), "dwarf");
1148 auto Obj
= object::ObjectFile::createObjectFile(FileBuffer
);
1149 EXPECT_TRUE((bool)Obj
);
1150 std::unique_ptr
<DWARFContext
> DwarfContext
= DWARFContext::create(**Obj
);
1152 // Verify the number of compile units is correct.
1153 uint32_t NumCUs
= DwarfContext
->getNumCompileUnits();
1154 EXPECT_EQ(NumCUs
, 1u);
1155 DWARFCompileUnit
*U
=
1156 cast
<DWARFCompileUnit
>(DwarfContext
->getUnitAtIndex(0));
1158 // Get the compile unit DIE is valid.
1159 auto CUDie
= U
->getUnitDIE(false);
1160 EXPECT_TRUE(CUDie
.isValid());
1162 // The compile unit doesn't have a parent or a sibling.
1163 auto ParentDie
= CUDie
.getParent();
1164 EXPECT_FALSE(ParentDie
.isValid());
1165 auto SiblingDie
= CUDie
.getSibling();
1166 EXPECT_FALSE(SiblingDie
.isValid());
1168 // Get the children of the compile unit
1169 auto A
= CUDie
.getFirstChild();
1170 auto B
= A
.getFirstChild();
1171 auto C
= B
.getSibling();
1172 auto D
= C
.getSibling();
1173 auto Null
= D
.getSibling();
1175 // Verify NULL Die is NULL and has no children or siblings
1176 EXPECT_TRUE(Null
.isNULL());
1177 EXPECT_FALSE(Null
.getSibling().isValid());
1178 EXPECT_FALSE(Null
.getFirstChild().isValid());
1180 // Verify all children of the compile unit DIE are correct.
1181 EXPECT_EQ(A
.getTag(), (dwarf::Tag
)Tag::A
);
1182 EXPECT_EQ(B
.getTag(), (dwarf::Tag
)Tag::B
);
1183 EXPECT_EQ(C
.getTag(), (dwarf::Tag
)Tag::C
);
1184 EXPECT_EQ(D
.getTag(), (dwarf::Tag
)Tag::D
);
1186 // Verify who has children
1187 EXPECT_TRUE(A
.hasChildren());
1188 EXPECT_FALSE(B
.hasChildren());
1189 EXPECT_TRUE(C
.hasChildren());
1190 EXPECT_TRUE(D
.hasChildren());
1192 // Make sure the parent of all the children of the compile unit are the
1194 EXPECT_EQ(A
.getParent(), CUDie
);
1196 // Make sure the parent of all the children of A are the A.
1197 // B is the first child in A, so we need to verify we can get the previous
1198 // DIE as the parent.
1199 EXPECT_EQ(B
.getParent(), A
);
1200 // C is the second child in A, so we need to make sure we can backup across
1201 // other DIE (B) at the same level to get the correct parent.
1202 EXPECT_EQ(C
.getParent(), A
);
1203 // D is the third child of A. We need to verify we can backup across other DIE
1204 // (B and C) including DIE that have children (D) to get the correct parent.
1205 EXPECT_EQ(D
.getParent(), A
);
1207 // Verify that a DIE with no children returns an invalid DWARFDie.
1208 EXPECT_FALSE(B
.getFirstChild().isValid());
1210 // Verify the children of the B DIE
1211 auto C1
= C
.getFirstChild();
1212 auto C2
= C1
.getSibling();
1213 EXPECT_TRUE(C2
.getSibling().isNULL());
1215 // Verify all children of the B DIE correctly valid or invalid.
1216 EXPECT_EQ(C1
.getTag(), (dwarf::Tag
)Tag::C1
);
1217 EXPECT_EQ(C2
.getTag(), (dwarf::Tag
)Tag::C2
);
1219 // Make sure the parent of all the children of the B are the B.
1220 EXPECT_EQ(C1
.getParent(), C
);
1221 EXPECT_EQ(C2
.getParent(), C
);
1223 // Make sure iterators work as expected.
1224 EXPECT_THAT(std::vector
<DWARFDie
>(A
.begin(), A
.end()),
1225 testing::ElementsAre(B
, C
, D
));
1226 EXPECT_THAT(std::vector
<DWARFDie
>(A
.rbegin(), A
.rend()),
1227 testing::ElementsAre(D
, C
, B
));
1229 // Make sure conversion from reverse iterator works as expected.
1230 EXPECT_EQ(A
.rbegin().base(), A
.end());
1231 EXPECT_EQ(A
.rend().base(), A
.begin());
1233 // Make sure iterator is bidirectional.
1235 auto Begin
= A
.begin();
1237 auto It
= A
.begin();
1239 EXPECT_EQ(It
, Begin
);
1253 EXPECT_EQ(It
, Begin
);
1256 // Make sure reverse iterator is bidirectional.
1258 auto Begin
= A
.rbegin();
1259 auto End
= A
.rend();
1260 auto It
= A
.rbegin();
1262 EXPECT_EQ(It
, Begin
);
1276 EXPECT_EQ(It
, Begin
);
1280 TEST(DWARFDebugInfo
, TestDWARFDie
) {
1281 // Make sure a default constructed DWARFDie doesn't have any parent, sibling
1283 DWARFDie DefaultDie
;
1284 EXPECT_FALSE(DefaultDie
.getParent().isValid());
1285 EXPECT_FALSE(DefaultDie
.getFirstChild().isValid());
1286 EXPECT_FALSE(DefaultDie
.getSibling().isValid());
1289 TEST(DWARFDebugInfo
, TestChildIterators
) {
1290 Triple Triple
= getNormalizedDefaultTargetTriple();
1291 if (!isConfigurationSupported(Triple
))
1294 // Test the DWARF APIs related to iterating across the children of a DIE using
1295 // the DWARFDie::iterator class.
1296 uint16_t Version
= 4;
1297 auto ExpectedDG
= dwarfgen::Generator::create(Triple
, Version
);
1298 ASSERT_THAT_EXPECTED(ExpectedDG
, Succeeded());
1299 dwarfgen::Generator
*DG
= ExpectedDG
.get().get();
1300 dwarfgen::CompileUnit
&CU
= DG
->addCompileUnit();
1302 enum class Tag
: uint16_t {
1303 A
= dwarf::DW_TAG_lo_user
,
1307 // Scope to allow us to re-use the same DIE names
1309 // Create DWARF tree that looks like:
1314 auto CUDie
= CU
.getUnitDIE();
1315 CUDie
.addChild((dwarf::Tag
)Tag::A
);
1316 CUDie
.addChild((dwarf::Tag
)Tag::B
);
1319 MemoryBufferRef
FileBuffer(DG
->generate(), "dwarf");
1320 auto Obj
= object::ObjectFile::createObjectFile(FileBuffer
);
1321 EXPECT_TRUE((bool)Obj
);
1322 std::unique_ptr
<DWARFContext
> DwarfContext
= DWARFContext::create(**Obj
);
1324 // Verify the number of compile units is correct.
1325 uint32_t NumCUs
= DwarfContext
->getNumCompileUnits();
1326 EXPECT_EQ(NumCUs
, 1u);
1327 DWARFCompileUnit
*U
=
1328 cast
<DWARFCompileUnit
>(DwarfContext
->getUnitAtIndex(0));
1330 // Get the compile unit DIE is valid.
1331 auto CUDie
= U
->getUnitDIE(false);
1332 EXPECT_TRUE(CUDie
.isValid());
1337 // Verify the compile unit DIE's children.
1339 for (auto Die
: CUDie
.children()) {
1341 case 0: A
= Die
; break;
1342 case 1: B
= Die
; break;
1346 EXPECT_EQ(A
.getTag(), (dwarf::Tag
)Tag::A
);
1347 EXPECT_EQ(B
.getTag(), (dwarf::Tag
)Tag::B
);
1349 // Verify that A has no children by verifying that the begin and end contain
1350 // invalid DIEs and also that the iterators are equal.
1351 EXPECT_EQ(A
.begin(), A
.end());
1354 TEST(DWARFDebugInfo
, TestChildIteratorsOnInvalidDie
) {
1355 // Verify that an invalid DIE has no children.
1357 auto begin
= Invalid
.begin();
1358 auto end
= Invalid
.end();
1359 EXPECT_FALSE(begin
->isValid());
1360 EXPECT_FALSE(end
->isValid());
1361 EXPECT_EQ(begin
, end
);
1364 TEST(DWARFDebugInfo
, TestEmptyChildren
) {
1365 const char *yamldata
= "debug_abbrev:\n"
1366 " - Code: 0x00000001\n"
1367 " Tag: DW_TAG_compile_unit\n"
1368 " Children: DW_CHILDREN_yes\n"
1377 " - AbbrCode: 0x00000001\n"
1379 " - AbbrCode: 0x00000000\n"
1382 auto ErrOrSections
= DWARFYAML::EmitDebugSections(StringRef(yamldata
), true);
1383 ASSERT_TRUE((bool)ErrOrSections
);
1384 std::unique_ptr
<DWARFContext
> DwarfContext
=
1385 DWARFContext::create(*ErrOrSections
, 8);
1387 // Verify the number of compile units is correct.
1388 uint32_t NumCUs
= DwarfContext
->getNumCompileUnits();
1389 EXPECT_EQ(NumCUs
, 1u);
1390 DWARFCompileUnit
*U
=
1391 cast
<DWARFCompileUnit
>(DwarfContext
->getUnitAtIndex(0));
1393 // Get the compile unit DIE is valid.
1394 auto CUDie
= U
->getUnitDIE(false);
1395 EXPECT_TRUE(CUDie
.isValid());
1397 // Verify that the CU Die that says it has children, but doesn't, actually
1398 // has begin and end iterators that are equal. We want to make sure we don't
1399 // see the Null DIEs during iteration.
1400 EXPECT_EQ(CUDie
.begin(), CUDie
.end());
1403 TEST(DWARFDebugInfo
, TestAttributeIterators
) {
1404 Triple Triple
= getNormalizedDefaultTargetTriple();
1405 if (!isConfigurationSupported(Triple
))
1408 // Test the DWARF APIs related to iterating across all attribute values in a
1410 uint16_t Version
= 4;
1411 auto ExpectedDG
= dwarfgen::Generator::create(Triple
, Version
);
1412 ASSERT_THAT_EXPECTED(ExpectedDG
, Succeeded());
1413 dwarfgen::Generator
*DG
= ExpectedDG
.get().get();
1414 dwarfgen::CompileUnit
&CU
= DG
->addCompileUnit();
1415 const uint64_t CULowPC
= 0x1000;
1416 StringRef
CUPath("/tmp/main.c");
1418 // Scope to allow us to re-use the same DIE names
1420 auto CUDie
= CU
.getUnitDIE();
1421 // Encode an attribute value before an attribute with no data.
1422 CUDie
.addAttribute(DW_AT_name
, DW_FORM_strp
, CUPath
.data());
1423 // Encode an attribute value with no data in .debug_info/types to ensure
1424 // the iteration works correctly.
1425 CUDie
.addAttribute(DW_AT_declaration
, DW_FORM_flag_present
);
1426 // Encode an attribute value after an attribute with no data.
1427 CUDie
.addAttribute(DW_AT_low_pc
, DW_FORM_addr
, CULowPC
);
1430 MemoryBufferRef
FileBuffer(DG
->generate(), "dwarf");
1431 auto Obj
= object::ObjectFile::createObjectFile(FileBuffer
);
1432 EXPECT_TRUE((bool)Obj
);
1433 std::unique_ptr
<DWARFContext
> DwarfContext
= DWARFContext::create(**Obj
);
1435 // Verify the number of compile units is correct.
1436 uint32_t NumCUs
= DwarfContext
->getNumCompileUnits();
1437 EXPECT_EQ(NumCUs
, 1u);
1438 DWARFCompileUnit
*U
=
1439 cast
<DWARFCompileUnit
>(DwarfContext
->getUnitAtIndex(0));
1441 // Get the compile unit DIE is valid.
1442 auto CUDie
= U
->getUnitDIE(false);
1443 EXPECT_TRUE(CUDie
.isValid());
1445 auto R
= CUDie
.attributes();
1450 EXPECT_EQ(I
->Attr
, DW_AT_name
);
1451 auto ActualCUPath
= I
->Value
.getAsCString();
1452 EXPECT_EQ(CUPath
, *ActualCUPath
);
1455 EXPECT_EQ(I
->Attr
, DW_AT_declaration
);
1456 EXPECT_EQ(1ull, *I
->Value
.getAsUnsignedConstant());
1459 EXPECT_EQ(I
->Attr
, DW_AT_low_pc
);
1460 EXPECT_EQ(CULowPC
, *I
->Value
.getAsAddress());
1465 TEST(DWARFDebugInfo
, TestFindRecurse
) {
1466 Triple Triple
= getNormalizedDefaultTargetTriple();
1467 if (!isConfigurationSupported(Triple
))
1470 uint16_t Version
= 4;
1471 auto ExpectedDG
= dwarfgen::Generator::create(Triple
, Version
);
1472 ASSERT_THAT_EXPECTED(ExpectedDG
, Succeeded());
1473 dwarfgen::Generator
*DG
= ExpectedDG
.get().get();
1474 dwarfgen::CompileUnit
&CU
= DG
->addCompileUnit();
1476 StringRef SpecDieName
= "spec";
1477 StringRef SpecLinkageName
= "spec_linkage";
1478 StringRef AbsDieName
= "abs";
1479 // Scope to allow us to re-use the same DIE names
1481 auto CUDie
= CU
.getUnitDIE();
1482 auto FuncSpecDie
= CUDie
.addChild(DW_TAG_subprogram
);
1483 auto FuncAbsDie
= CUDie
.addChild(DW_TAG_subprogram
);
1484 // Put the linkage name in a second abstract origin DIE to ensure we
1485 // recurse through more than just one DIE when looking for attributes.
1486 auto FuncAbsDie2
= CUDie
.addChild(DW_TAG_subprogram
);
1487 auto FuncDie
= CUDie
.addChild(DW_TAG_subprogram
);
1488 auto VarAbsDie
= CUDie
.addChild(DW_TAG_variable
);
1489 auto VarDie
= CUDie
.addChild(DW_TAG_variable
);
1490 FuncSpecDie
.addAttribute(DW_AT_name
, DW_FORM_strp
, SpecDieName
);
1491 FuncAbsDie2
.addAttribute(DW_AT_linkage_name
, DW_FORM_strp
, SpecLinkageName
);
1492 FuncAbsDie
.addAttribute(DW_AT_specification
, DW_FORM_ref4
, FuncSpecDie
);
1493 FuncAbsDie
.addAttribute(DW_AT_abstract_origin
, DW_FORM_ref4
, FuncAbsDie2
);
1494 FuncDie
.addAttribute(DW_AT_abstract_origin
, DW_FORM_ref4
, FuncAbsDie
);
1495 VarAbsDie
.addAttribute(DW_AT_name
, DW_FORM_strp
, AbsDieName
);
1496 VarDie
.addAttribute(DW_AT_abstract_origin
, DW_FORM_ref4
, VarAbsDie
);
1499 MemoryBufferRef
FileBuffer(DG
->generate(), "dwarf");
1500 auto Obj
= object::ObjectFile::createObjectFile(FileBuffer
);
1501 EXPECT_TRUE((bool)Obj
);
1502 std::unique_ptr
<DWARFContext
> DwarfContext
= DWARFContext::create(**Obj
);
1504 // Verify the number of compile units is correct.
1505 uint32_t NumCUs
= DwarfContext
->getNumCompileUnits();
1506 EXPECT_EQ(NumCUs
, 1u);
1507 DWARFCompileUnit
*U
=
1508 cast
<DWARFCompileUnit
>(DwarfContext
->getUnitAtIndex(0));
1510 // Get the compile unit DIE is valid.
1511 auto CUDie
= U
->getUnitDIE(false);
1512 EXPECT_TRUE(CUDie
.isValid());
1514 auto FuncSpecDie
= CUDie
.getFirstChild();
1515 auto FuncAbsDie
= FuncSpecDie
.getSibling();
1516 auto FuncAbsDie2
= FuncAbsDie
.getSibling();
1517 auto FuncDie
= FuncAbsDie2
.getSibling();
1518 auto VarAbsDie
= FuncDie
.getSibling();
1519 auto VarDie
= VarAbsDie
.getSibling();
1521 // Make sure we can't extract the name from the specification die when using
1522 // DWARFDie::find() since it won't check the DW_AT_specification DIE.
1523 EXPECT_FALSE(FuncDie
.find(DW_AT_name
));
1525 // Make sure we can extract the name from the specification die when using
1526 // DWARFDie::findRecursively() since it should recurse through the
1527 // DW_AT_specification DIE.
1528 auto NameOpt
= FuncDie
.findRecursively(DW_AT_name
);
1529 EXPECT_TRUE(NameOpt
);
1530 // Test the dwarf::toString() helper function.
1531 auto StringOpt
= toString(NameOpt
);
1532 EXPECT_TRUE(StringOpt
);
1533 EXPECT_EQ(SpecDieName
, StringOpt
.getValueOr(nullptr));
1534 // Test the dwarf::toString() helper function with a default value specified.
1535 EXPECT_EQ(SpecDieName
, toString(NameOpt
, nullptr));
1537 auto LinkageNameOpt
= FuncDie
.findRecursively(DW_AT_linkage_name
);
1538 EXPECT_EQ(SpecLinkageName
, toString(LinkageNameOpt
).getValueOr(nullptr));
1540 // Make sure we can't extract the name from the abstract origin die when using
1541 // DWARFDie::find() since it won't check the DW_AT_abstract_origin DIE.
1542 EXPECT_FALSE(VarDie
.find(DW_AT_name
));
1544 // Make sure we can extract the name from the abstract origin die when using
1545 // DWARFDie::findRecursively() since it should recurse through the
1546 // DW_AT_abstract_origin DIE.
1547 NameOpt
= VarDie
.findRecursively(DW_AT_name
);
1548 EXPECT_TRUE(NameOpt
);
1549 // Test the dwarf::toString() helper function.
1550 StringOpt
= toString(NameOpt
);
1551 EXPECT_TRUE(StringOpt
);
1552 EXPECT_EQ(AbsDieName
, StringOpt
.getValueOr(nullptr));
1555 TEST(DWARFDebugInfo
, TestDwarfToFunctions
) {
1556 // Test all of the dwarf::toXXX functions that take a
1557 // Optional<DWARFFormValue> and extract the values from it.
1558 uint64_t InvalidU64
= 0xBADBADBADBADBADB;
1559 int64_t InvalidS64
= 0xBADBADBADBADBADB;
1561 // First test that we don't get valid values back when using an optional with
1563 Optional
<DWARFFormValue
> FormValOpt1
= DWARFFormValue();
1564 EXPECT_FALSE(toString(FormValOpt1
).hasValue());
1565 EXPECT_FALSE(toUnsigned(FormValOpt1
).hasValue());
1566 EXPECT_FALSE(toReference(FormValOpt1
).hasValue());
1567 EXPECT_FALSE(toSigned(FormValOpt1
).hasValue());
1568 EXPECT_FALSE(toAddress(FormValOpt1
).hasValue());
1569 EXPECT_FALSE(toSectionOffset(FormValOpt1
).hasValue());
1570 EXPECT_FALSE(toBlock(FormValOpt1
).hasValue());
1571 EXPECT_EQ(nullptr, toString(FormValOpt1
, nullptr));
1572 EXPECT_EQ(InvalidU64
, toUnsigned(FormValOpt1
, InvalidU64
));
1573 EXPECT_EQ(InvalidU64
, toReference(FormValOpt1
, InvalidU64
));
1574 EXPECT_EQ(InvalidU64
, toAddress(FormValOpt1
, InvalidU64
));
1575 EXPECT_EQ(InvalidU64
, toSectionOffset(FormValOpt1
, InvalidU64
));
1576 EXPECT_EQ(InvalidS64
, toSigned(FormValOpt1
, InvalidS64
));
1578 // Test successful and unsuccessful address decoding.
1579 uint64_t Address
= 0x100000000ULL
;
1580 Optional
<DWARFFormValue
> FormValOpt2
=
1581 DWARFFormValue::createFromUValue(DW_FORM_addr
, Address
);
1583 EXPECT_FALSE(toString(FormValOpt2
).hasValue());
1584 EXPECT_FALSE(toUnsigned(FormValOpt2
).hasValue());
1585 EXPECT_FALSE(toReference(FormValOpt2
).hasValue());
1586 EXPECT_FALSE(toSigned(FormValOpt2
).hasValue());
1587 EXPECT_TRUE(toAddress(FormValOpt2
).hasValue());
1588 EXPECT_FALSE(toSectionOffset(FormValOpt2
).hasValue());
1589 EXPECT_FALSE(toBlock(FormValOpt2
).hasValue());
1590 EXPECT_EQ(nullptr, toString(FormValOpt2
, nullptr));
1591 EXPECT_EQ(InvalidU64
, toUnsigned(FormValOpt2
, InvalidU64
));
1592 EXPECT_EQ(InvalidU64
, toReference(FormValOpt2
, InvalidU64
));
1593 EXPECT_EQ(Address
, toAddress(FormValOpt2
, InvalidU64
));
1594 EXPECT_EQ(InvalidU64
, toSectionOffset(FormValOpt2
, InvalidU64
));
1595 EXPECT_EQ(InvalidS64
, toSigned(FormValOpt2
, InvalidU64
));
1597 // Test successful and unsuccessful unsigned constant decoding.
1598 uint64_t UData8
= 0x1020304050607080ULL
;
1599 Optional
<DWARFFormValue
> FormValOpt3
=
1600 DWARFFormValue::createFromUValue(DW_FORM_udata
, UData8
);
1602 EXPECT_FALSE(toString(FormValOpt3
).hasValue());
1603 EXPECT_TRUE(toUnsigned(FormValOpt3
).hasValue());
1604 EXPECT_FALSE(toReference(FormValOpt3
).hasValue());
1605 EXPECT_TRUE(toSigned(FormValOpt3
).hasValue());
1606 EXPECT_FALSE(toAddress(FormValOpt3
).hasValue());
1607 EXPECT_FALSE(toSectionOffset(FormValOpt3
).hasValue());
1608 EXPECT_FALSE(toBlock(FormValOpt3
).hasValue());
1609 EXPECT_EQ(nullptr, toString(FormValOpt3
, nullptr));
1610 EXPECT_EQ(UData8
, toUnsigned(FormValOpt3
, InvalidU64
));
1611 EXPECT_EQ(InvalidU64
, toReference(FormValOpt3
, InvalidU64
));
1612 EXPECT_EQ(InvalidU64
, toAddress(FormValOpt3
, InvalidU64
));
1613 EXPECT_EQ(InvalidU64
, toSectionOffset(FormValOpt3
, InvalidU64
));
1614 EXPECT_EQ((int64_t)UData8
, toSigned(FormValOpt3
, InvalidU64
));
1616 // Test successful and unsuccessful reference decoding.
1617 uint32_t RefData
= 0x11223344U
;
1618 Optional
<DWARFFormValue
> FormValOpt4
=
1619 DWARFFormValue::createFromUValue(DW_FORM_ref_addr
, RefData
);
1621 EXPECT_FALSE(toString(FormValOpt4
).hasValue());
1622 EXPECT_FALSE(toUnsigned(FormValOpt4
).hasValue());
1623 EXPECT_TRUE(toReference(FormValOpt4
).hasValue());
1624 EXPECT_FALSE(toSigned(FormValOpt4
).hasValue());
1625 EXPECT_FALSE(toAddress(FormValOpt4
).hasValue());
1626 EXPECT_FALSE(toSectionOffset(FormValOpt4
).hasValue());
1627 EXPECT_FALSE(toBlock(FormValOpt4
).hasValue());
1628 EXPECT_EQ(nullptr, toString(FormValOpt4
, nullptr));
1629 EXPECT_EQ(InvalidU64
, toUnsigned(FormValOpt4
, InvalidU64
));
1630 EXPECT_EQ(RefData
, toReference(FormValOpt4
, InvalidU64
));
1631 EXPECT_EQ(InvalidU64
, toAddress(FormValOpt4
, InvalidU64
));
1632 EXPECT_EQ(InvalidU64
, toSectionOffset(FormValOpt4
, InvalidU64
));
1633 EXPECT_EQ(InvalidS64
, toSigned(FormValOpt4
, InvalidU64
));
1635 // Test successful and unsuccessful signed constant decoding.
1636 int64_t SData8
= 0x1020304050607080ULL
;
1637 Optional
<DWARFFormValue
> FormValOpt5
=
1638 DWARFFormValue::createFromSValue(DW_FORM_udata
, SData8
);
1640 EXPECT_FALSE(toString(FormValOpt5
).hasValue());
1641 EXPECT_TRUE(toUnsigned(FormValOpt5
).hasValue());
1642 EXPECT_FALSE(toReference(FormValOpt5
).hasValue());
1643 EXPECT_TRUE(toSigned(FormValOpt5
).hasValue());
1644 EXPECT_FALSE(toAddress(FormValOpt5
).hasValue());
1645 EXPECT_FALSE(toSectionOffset(FormValOpt5
).hasValue());
1646 EXPECT_FALSE(toBlock(FormValOpt5
).hasValue());
1647 EXPECT_EQ(nullptr, toString(FormValOpt5
, nullptr));
1648 EXPECT_EQ((uint64_t)SData8
, toUnsigned(FormValOpt5
, InvalidU64
));
1649 EXPECT_EQ(InvalidU64
, toReference(FormValOpt5
, InvalidU64
));
1650 EXPECT_EQ(InvalidU64
, toAddress(FormValOpt5
, InvalidU64
));
1651 EXPECT_EQ(InvalidU64
, toSectionOffset(FormValOpt5
, InvalidU64
));
1652 EXPECT_EQ(SData8
, toSigned(FormValOpt5
, InvalidU64
));
1654 // Test successful and unsuccessful block decoding.
1655 uint8_t Data
[] = { 2, 3, 4 };
1656 ArrayRef
<uint8_t> Array(Data
);
1657 Optional
<DWARFFormValue
> FormValOpt6
=
1658 DWARFFormValue::createFromBlockValue(DW_FORM_block1
, Array
);
1660 EXPECT_FALSE(toString(FormValOpt6
).hasValue());
1661 EXPECT_FALSE(toUnsigned(FormValOpt6
).hasValue());
1662 EXPECT_FALSE(toReference(FormValOpt6
).hasValue());
1663 EXPECT_FALSE(toSigned(FormValOpt6
).hasValue());
1664 EXPECT_FALSE(toAddress(FormValOpt6
).hasValue());
1665 EXPECT_FALSE(toSectionOffset(FormValOpt6
).hasValue());
1666 auto BlockOpt
= toBlock(FormValOpt6
);
1667 EXPECT_TRUE(BlockOpt
.hasValue());
1668 EXPECT_EQ(*BlockOpt
, Array
);
1669 EXPECT_EQ(nullptr, toString(FormValOpt6
, nullptr));
1670 EXPECT_EQ(InvalidU64
, toUnsigned(FormValOpt6
, InvalidU64
));
1671 EXPECT_EQ(InvalidU64
, toReference(FormValOpt6
, InvalidU64
));
1672 EXPECT_EQ(InvalidU64
, toAddress(FormValOpt6
, InvalidU64
));
1673 EXPECT_EQ(InvalidU64
, toSectionOffset(FormValOpt6
, InvalidU64
));
1674 EXPECT_EQ(InvalidS64
, toSigned(FormValOpt6
, InvalidU64
));
1679 TEST(DWARFDebugInfo
, TestFindAttrs
) {
1680 Triple Triple
= getNormalizedDefaultTargetTriple();
1681 if (!isConfigurationSupported(Triple
))
1684 // Test the DWARFDie::find() and DWARFDie::findRecursively() that take an
1685 // ArrayRef<dwarf::Attribute> value to make sure they work correctly.
1686 uint16_t Version
= 4;
1687 auto ExpectedDG
= dwarfgen::Generator::create(Triple
, Version
);
1688 ASSERT_THAT_EXPECTED(ExpectedDG
, Succeeded());
1689 dwarfgen::Generator
*DG
= ExpectedDG
.get().get();
1690 dwarfgen::CompileUnit
&CU
= DG
->addCompileUnit();
1692 StringRef
DieMangled("_Z3fooi");
1693 // Scope to allow us to re-use the same DIE names
1695 auto CUDie
= CU
.getUnitDIE();
1696 auto FuncSpecDie
= CUDie
.addChild(DW_TAG_subprogram
);
1697 auto FuncDie
= CUDie
.addChild(DW_TAG_subprogram
);
1698 FuncSpecDie
.addAttribute(DW_AT_MIPS_linkage_name
, DW_FORM_strp
, DieMangled
);
1699 FuncDie
.addAttribute(DW_AT_specification
, DW_FORM_ref4
, FuncSpecDie
);
1702 MemoryBufferRef
FileBuffer(DG
->generate(), "dwarf");
1703 auto Obj
= object::ObjectFile::createObjectFile(FileBuffer
);
1704 EXPECT_TRUE((bool)Obj
);
1705 std::unique_ptr
<DWARFContext
> DwarfContext
= DWARFContext::create(**Obj
);
1707 // Verify the number of compile units is correct.
1708 uint32_t NumCUs
= DwarfContext
->getNumCompileUnits();
1709 EXPECT_EQ(NumCUs
, 1u);
1710 DWARFCompileUnit
*U
=
1711 cast
<DWARFCompileUnit
>(DwarfContext
->getUnitAtIndex(0));
1713 // Get the compile unit DIE is valid.
1714 auto CUDie
= U
->getUnitDIE(false);
1715 EXPECT_TRUE(CUDie
.isValid());
1717 auto FuncSpecDie
= CUDie
.getFirstChild();
1718 auto FuncDie
= FuncSpecDie
.getSibling();
1720 // Make sure that passing in an empty attribute list behave correctly.
1721 EXPECT_FALSE(FuncDie
.find(ArrayRef
<dwarf::Attribute
>()).hasValue());
1723 // Make sure that passing in a list of attribute that are not contained
1724 // in the DIE returns nothing.
1725 EXPECT_FALSE(FuncDie
.find({DW_AT_low_pc
, DW_AT_entry_pc
}).hasValue());
1727 const dwarf::Attribute Attrs
[] = {DW_AT_linkage_name
,
1728 DW_AT_MIPS_linkage_name
};
1730 // Make sure we can't extract the linkage name attributes when using
1731 // DWARFDie::find() since it won't check the DW_AT_specification DIE.
1732 EXPECT_FALSE(FuncDie
.find(Attrs
).hasValue());
1734 // Make sure we can extract the name from the specification die when using
1735 // DWARFDie::findRecursively() since it should recurse through the
1736 // DW_AT_specification DIE.
1737 auto NameOpt
= FuncDie
.findRecursively(Attrs
);
1738 EXPECT_TRUE(NameOpt
.hasValue());
1739 EXPECT_EQ(DieMangled
, toString(NameOpt
, ""));
1742 TEST(DWARFDebugInfo
, TestImplicitConstAbbrevs
) {
1743 Triple Triple
= getNormalizedDefaultTargetTriple();
1744 if (!isConfigurationSupported(Triple
))
1747 uint16_t Version
= 5;
1748 auto ExpectedDG
= dwarfgen::Generator::create(Triple
, Version
);
1749 ASSERT_THAT_EXPECTED(ExpectedDG
, Succeeded());
1750 dwarfgen::Generator
*DG
= ExpectedDG
.get().get();
1751 dwarfgen::CompileUnit
&CU
= DG
->addCompileUnit();
1752 dwarfgen::DIE CUDie
= CU
.getUnitDIE();
1753 const dwarf::Attribute Attr
= DW_AT_lo_user
;
1754 const int64_t Val1
= 42;
1755 const int64_t Val2
= 43;
1757 auto FirstVal1DIE
= CUDie
.addChild(DW_TAG_class_type
);
1758 FirstVal1DIE
.addAttribute(Attr
, DW_FORM_implicit_const
, Val1
);
1760 auto SecondVal1DIE
= CUDie
.addChild(DW_TAG_class_type
);
1761 SecondVal1DIE
.addAttribute(Attr
, DW_FORM_implicit_const
, Val1
);
1763 auto Val2DIE
= CUDie
.addChild(DW_TAG_class_type
);
1764 Val2DIE
.addAttribute(Attr
, DW_FORM_implicit_const
, Val2
);
1766 MemoryBufferRef
FileBuffer(DG
->generate(), "dwarf");
1767 auto Obj
= object::ObjectFile::createObjectFile(FileBuffer
);
1768 EXPECT_TRUE((bool)Obj
);
1769 std::unique_ptr
<DWARFContext
> DwarfContext
= DWARFContext::create(**Obj
);
1770 DWARFCompileUnit
*U
=
1771 cast
<DWARFCompileUnit
>(DwarfContext
->getUnitAtIndex(0));
1772 EXPECT_TRUE((bool)U
);
1774 const auto *Abbrevs
= U
->getAbbreviations();
1775 EXPECT_TRUE((bool)Abbrevs
);
1777 // Let's find implicit_const abbrevs and verify,
1778 // that there are exactly two of them and both of them
1779 // can be dumped correctly.
1780 typedef decltype(Abbrevs
->begin()) AbbrevIt
;
1781 AbbrevIt Val1Abbrev
= Abbrevs
->end();
1782 AbbrevIt Val2Abbrev
= Abbrevs
->end();
1783 for(auto it
= Abbrevs
->begin(); it
!= Abbrevs
->end(); ++it
) {
1784 if (it
->getNumAttributes() == 0)
1785 continue; // root abbrev for DW_TAG_compile_unit
1787 auto A
= it
->getAttrByIndex(0);
1790 auto FormValue
= it
->getAttributeValue(/* offset */ 0, A
, *U
);
1791 EXPECT_TRUE((bool)FormValue
);
1792 EXPECT_EQ(FormValue
->getForm(), dwarf::DW_FORM_implicit_const
);
1794 const auto V
= FormValue
->getAsSignedConstant();
1795 EXPECT_TRUE((bool)V
);
1797 auto VerifyAbbrevDump
= [&V
](AbbrevIt it
) {
1799 llvm::raw_string_ostream
OS(S
);
1801 auto FormPos
= OS
.str().find("DW_FORM_implicit_const");
1802 EXPECT_NE(FormPos
, std::string::npos
);
1803 auto ValPos
= S
.find_first_of("-0123456789", FormPos
);
1804 EXPECT_NE(ValPos
, std::string::npos
);
1805 int64_t Val
= std::atoll(S
.substr(ValPos
).c_str());
1811 EXPECT_EQ(Val1Abbrev
, Abbrevs
->end());
1813 VerifyAbbrevDump(it
);
1816 EXPECT_EQ(Val2Abbrev
, Abbrevs
->end());
1818 VerifyAbbrevDump(it
);
1821 FAIL() << "Unexpected attribute value: " << *V
;
1825 // Now let's make sure that two Val1-DIEs refer to the same abbrev,
1826 // and Val2-DIE refers to another one.
1827 auto DieDG
= U
->getUnitDIE(false);
1828 auto it
= DieDG
.begin();
1829 std::multimap
<int64_t, decltype(it
->getAbbreviationDeclarationPtr())> DIEs
;
1830 const DWARFAbbreviationDeclaration
*AbbrevPtrVal1
= nullptr;
1831 const DWARFAbbreviationDeclaration
*AbbrevPtrVal2
= nullptr;
1832 for (; it
!= DieDG
.end(); ++it
) {
1833 const auto *AbbrevPtr
= it
->getAbbreviationDeclarationPtr();
1834 EXPECT_TRUE((bool)AbbrevPtr
);
1835 auto FormValue
= it
->find(Attr
);
1836 EXPECT_TRUE((bool)FormValue
);
1837 const auto V
= FormValue
->getAsSignedConstant();
1838 EXPECT_TRUE((bool)V
);
1841 AbbrevPtrVal1
= AbbrevPtr
;
1844 AbbrevPtrVal2
= AbbrevPtr
;
1847 FAIL() << "Unexpected attribute value: " << *V
;
1849 DIEs
.insert(std::make_pair(*V
, AbbrevPtr
));
1851 EXPECT_EQ(DIEs
.count(Val1
), 2u);
1852 EXPECT_EQ(DIEs
.count(Val2
), 1u);
1853 auto Val1Range
= DIEs
.equal_range(Val1
);
1854 for (auto it
= Val1Range
.first
; it
!= Val1Range
.second
; ++it
)
1855 EXPECT_EQ(it
->second
, AbbrevPtrVal1
);
1856 EXPECT_EQ(DIEs
.find(Val2
)->second
, AbbrevPtrVal2
);
1859 void VerifyWarning(DWARFContext
&DwarfContext
, StringRef Error
) {
1860 SmallString
<1024> Str
;
1861 raw_svector_ostream
Strm(Str
);
1862 EXPECT_TRUE(DwarfContext
.verify(Strm
));
1863 EXPECT_TRUE(Str
.str().contains(Error
));
1866 void VerifyError(DWARFContext
&DwarfContext
, StringRef Error
) {
1867 SmallString
<1024> Str
;
1868 raw_svector_ostream
Strm(Str
);
1869 EXPECT_FALSE(DwarfContext
.verify(Strm
));
1870 EXPECT_TRUE(Str
.str().contains(Error
));
1873 void VerifySuccess(DWARFContext
&DwarfContext
) {
1874 SmallString
<1024> Str
;
1875 raw_svector_ostream
Strm(Str
);
1876 EXPECT_TRUE(DwarfContext
.verify(Strm
));
1879 TEST(DWARFDebugInfo
, TestDwarfVerifyInvalidCURef
) {
1880 // Create a single compile unit with a single function that has a DW_AT_type
1881 // that is CU relative. The CU offset is not valid because it is larger than
1882 // the compile unit itself.
1884 const char *yamldata
= R
"(
1891 Tag: DW_TAG_compile_unit
1892 Children: DW_CHILDREN_yes
1894 - Attribute: DW_AT_name
1897 Tag: DW_TAG_subprogram
1898 Children: DW_CHILDREN_no
1900 - Attribute: DW_AT_name
1902 - Attribute: DW_AT_type
1911 - AbbrCode: 0x00000001
1913 - Value: 0x0000000000000001
1914 - AbbrCode: 0x00000002
1916 - Value: 0x000000000000000D
1917 - Value: 0x0000000000001234
1918 - AbbrCode: 0x00000000
1921 auto ErrOrSections
= DWARFYAML::EmitDebugSections(StringRef(yamldata
));
1922 ASSERT_TRUE((bool)ErrOrSections
);
1923 std::unique_ptr
<DWARFContext
> DwarfContext
=
1924 DWARFContext::create(*ErrOrSections
, 8);
1925 VerifyError(*DwarfContext
, "error: DW_FORM_ref4 CU offset 0x00001234 is "
1926 "invalid (must be less than CU size of "
1930 TEST(DWARFDebugInfo
, TestDwarfVerifyInvalidRefAddr
) {
1931 // Create a single compile unit with a single function that has an invalid
1932 // DW_AT_type with an invalid .debug_info offset in its DW_FORM_ref_addr.
1933 const char *yamldata
= R
"(
1940 Tag: DW_TAG_compile_unit
1941 Children: DW_CHILDREN_yes
1943 - Attribute: DW_AT_name
1946 Tag: DW_TAG_subprogram
1947 Children: DW_CHILDREN_no
1949 - Attribute: DW_AT_name
1951 - Attribute: DW_AT_type
1952 Form: DW_FORM_ref_addr
1960 - AbbrCode: 0x00000001
1962 - Value: 0x0000000000000001
1963 - AbbrCode: 0x00000002
1965 - Value: 0x000000000000000D
1966 - Value: 0x0000000000001234
1967 - AbbrCode: 0x00000000
1970 auto ErrOrSections
= DWARFYAML::EmitDebugSections(StringRef(yamldata
));
1971 ASSERT_TRUE((bool)ErrOrSections
);
1972 std::unique_ptr
<DWARFContext
> DwarfContext
=
1973 DWARFContext::create(*ErrOrSections
, 8);
1974 VerifyError(*DwarfContext
,
1975 "error: DW_FORM_ref_addr offset beyond .debug_info bounds:");
1978 TEST(DWARFDebugInfo
, TestDwarfVerifyInvalidRanges
) {
1979 // Create a single compile unit with a DW_AT_ranges whose section offset
1981 const char *yamldata
= R
"(
1987 Tag: DW_TAG_compile_unit
1988 Children: DW_CHILDREN_no
1990 - Attribute: DW_AT_name
1992 - Attribute: DW_AT_ranges
1993 Form: DW_FORM_sec_offset
2001 - AbbrCode: 0x00000001
2003 - Value: 0x0000000000000001
2004 - Value: 0x0000000000001000
2007 auto ErrOrSections
= DWARFYAML::EmitDebugSections(StringRef(yamldata
));
2008 ASSERT_TRUE((bool)ErrOrSections
);
2009 std::unique_ptr
<DWARFContext
> DwarfContext
=
2010 DWARFContext::create(*ErrOrSections
, 8);
2011 VerifyError(*DwarfContext
,
2012 "error: DW_AT_ranges offset is beyond .debug_ranges bounds:");
2015 TEST(DWARFDebugInfo
, TestDwarfVerifyInvalidStmtList
) {
2016 // Create a single compile unit with a DW_AT_stmt_list whose section offset
2018 const char *yamldata
= R
"(
2024 Tag: DW_TAG_compile_unit
2025 Children: DW_CHILDREN_no
2027 - Attribute: DW_AT_name
2029 - Attribute: DW_AT_stmt_list
2030 Form: DW_FORM_sec_offset
2038 - AbbrCode: 0x00000001
2040 - Value: 0x0000000000000001
2041 - Value: 0x0000000000001000
2044 auto ErrOrSections
= DWARFYAML::EmitDebugSections(StringRef(yamldata
));
2045 ASSERT_TRUE((bool)ErrOrSections
);
2046 std::unique_ptr
<DWARFContext
> DwarfContext
=
2047 DWARFContext::create(*ErrOrSections
, 8);
2050 "error: DW_AT_stmt_list offset is beyond .debug_line bounds: 0x00001000");
2053 TEST(DWARFDebugInfo
, TestDwarfVerifyInvalidStrp
) {
2054 // Create a single compile unit with a single function that has an invalid
2055 // DW_FORM_strp for the DW_AT_name.
2056 const char *yamldata
= R
"(
2061 Tag: DW_TAG_compile_unit
2062 Children: DW_CHILDREN_no
2064 - Attribute: DW_AT_name
2073 - AbbrCode: 0x00000001
2075 - Value: 0x0000000000001234
2077 auto ErrOrSections
= DWARFYAML::EmitDebugSections(StringRef(yamldata
));
2078 ASSERT_TRUE((bool)ErrOrSections
);
2079 std::unique_ptr
<DWARFContext
> DwarfContext
=
2080 DWARFContext::create(*ErrOrSections
, 8);
2081 VerifyError(*DwarfContext
,
2082 "error: DW_FORM_strp offset beyond .debug_str bounds:");
2085 TEST(DWARFDebugInfo
, TestDwarfVerifyInvalidRefAddrBetween
) {
2086 // Create a single compile unit with a single function that has a DW_AT_type
2087 // with a valid .debug_info offset, but the offset is between two DIEs.
2088 const char *yamldata
= R
"(
2095 Tag: DW_TAG_compile_unit
2096 Children: DW_CHILDREN_yes
2098 - Attribute: DW_AT_name
2101 Tag: DW_TAG_subprogram
2102 Children: DW_CHILDREN_no
2104 - Attribute: DW_AT_name
2106 - Attribute: DW_AT_type
2107 Form: DW_FORM_ref_addr
2115 - AbbrCode: 0x00000001
2117 - Value: 0x0000000000000001
2118 - AbbrCode: 0x00000002
2120 - Value: 0x000000000000000D
2121 - Value: 0x0000000000000011
2122 - AbbrCode: 0x00000000
2125 auto ErrOrSections
= DWARFYAML::EmitDebugSections(StringRef(yamldata
));
2126 ASSERT_TRUE((bool)ErrOrSections
);
2127 std::unique_ptr
<DWARFContext
> DwarfContext
=
2128 DWARFContext::create(*ErrOrSections
, 8);
2131 "error: invalid DIE reference 0x00000011. Offset is in between DIEs:");
2134 TEST(DWARFDebugInfo
, TestDwarfVerifyInvalidLineSequence
) {
2135 // Create a single compile unit whose line table has a sequence in it where
2136 // the address decreases.
2137 StringRef yamldata
= R
"(
2143 Tag: DW_TAG_compile_unit
2144 Children: DW_CHILDREN_no
2146 - Attribute: DW_AT_name
2148 - Attribute: DW_AT_stmt_list
2149 Form: DW_FORM_sec_offset
2157 - AbbrCode: 0x00000001
2159 - Value: 0x0000000000000001
2160 - Value: 0x0000000000000000
2171 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
2180 - Opcode: DW_LNS_extended_op
2182 SubOpcode: DW_LNE_set_address
2184 - Opcode: DW_LNS_advance_line
2187 - Opcode: DW_LNS_copy
2189 - Opcode: DW_LNS_advance_pc
2190 Data: 18446744073709551600
2191 - Opcode: DW_LNS_extended_op
2193 SubOpcode: DW_LNE_end_sequence
2194 Data: 18446744073709551600
2196 auto ErrOrSections
= DWARFYAML::EmitDebugSections(yamldata
);
2197 ASSERT_TRUE((bool)ErrOrSections
);
2198 std::unique_ptr
<DWARFContext
> DwarfContext
=
2199 DWARFContext::create(*ErrOrSections
, 8);
2200 VerifyError(*DwarfContext
, "error: .debug_line[0x00000000] row[1] decreases "
2201 "in address from previous row:");
2204 TEST(DWARFDebugInfo
, TestDwarfVerifyInvalidLineFileIndex
) {
2205 // Create a single compile unit whose line table has a line table row with
2206 // an invalid file index.
2207 StringRef yamldata
= R
"(
2213 Tag: DW_TAG_compile_unit
2214 Children: DW_CHILDREN_no
2216 - Attribute: DW_AT_name
2218 - Attribute: DW_AT_stmt_list
2219 Form: DW_FORM_sec_offset
2227 - AbbrCode: 0x00000001
2229 - Value: 0x0000000000000001
2230 - Value: 0x0000000000000000
2241 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
2250 - Opcode: DW_LNS_extended_op
2252 SubOpcode: DW_LNE_set_address
2254 - Opcode: DW_LNS_advance_line
2257 - Opcode: DW_LNS_copy
2259 - Opcode: DW_LNS_advance_pc
2261 - Opcode: DW_LNS_set_file
2263 - Opcode: DW_LNS_extended_op
2265 SubOpcode: DW_LNE_end_sequence
2268 auto ErrOrSections
= DWARFYAML::EmitDebugSections(yamldata
);
2269 ASSERT_TRUE((bool)ErrOrSections
);
2270 std::unique_ptr
<DWARFContext
> DwarfContext
=
2271 DWARFContext::create(*ErrOrSections
, 8);
2272 VerifyError(*DwarfContext
, "error: .debug_line[0x00000000][1] has invalid "
2273 "file index 5 (valid values are [1,1]):");
2276 TEST(DWARFDebugInfo
, TestDwarfVerifyInvalidLineTablePorlogueDirIndex
) {
2277 // Create a single compile unit whose line table has a prologue with an
2278 // invalid dir index.
2279 StringRef yamldata
= R
"(
2285 Tag: DW_TAG_compile_unit
2286 Children: DW_CHILDREN_no
2288 - Attribute: DW_AT_name
2290 - Attribute: DW_AT_stmt_list
2291 Form: DW_FORM_sec_offset
2299 - AbbrCode: 0x00000001
2301 - Value: 0x0000000000000001
2302 - Value: 0x0000000000000000
2313 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
2322 - Opcode: DW_LNS_extended_op
2324 SubOpcode: DW_LNE_set_address
2326 - Opcode: DW_LNS_advance_line
2329 - Opcode: DW_LNS_copy
2331 - Opcode: DW_LNS_advance_pc
2333 - Opcode: DW_LNS_set_file
2335 - Opcode: DW_LNS_extended_op
2337 SubOpcode: DW_LNE_end_sequence
2340 auto ErrOrSections
= DWARFYAML::EmitDebugSections(yamldata
);
2341 ASSERT_TRUE((bool)ErrOrSections
);
2342 std::unique_ptr
<DWARFContext
> DwarfContext
=
2343 DWARFContext::create(*ErrOrSections
, 8);
2344 VerifyError(*DwarfContext
,
2345 "error: .debug_line[0x00000000].prologue."
2346 "file_names[1].dir_idx contains an invalid index: 2");
2349 TEST(DWARFDebugInfo
, TestDwarfVerifyDuplicateFileWarning
) {
2350 // Create a single compile unit whose line table has a prologue with an
2351 // invalid dir index.
2352 StringRef yamldata
= R
"(
2358 Tag: DW_TAG_compile_unit
2359 Children: DW_CHILDREN_no
2361 - Attribute: DW_AT_name
2363 - Attribute: DW_AT_stmt_list
2364 Form: DW_FORM_sec_offset
2372 - AbbrCode: 0x00000001
2374 - Value: 0x0000000000000001
2375 - Value: 0x0000000000000000
2386 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
2399 - Opcode: DW_LNS_extended_op
2401 SubOpcode: DW_LNE_set_address
2403 - Opcode: DW_LNS_advance_line
2406 - Opcode: DW_LNS_copy
2408 - Opcode: DW_LNS_advance_pc
2410 - Opcode: DW_LNS_set_file
2412 - Opcode: DW_LNS_extended_op
2414 SubOpcode: DW_LNE_end_sequence
2417 auto ErrOrSections
= DWARFYAML::EmitDebugSections(yamldata
);
2418 ASSERT_TRUE((bool)ErrOrSections
);
2419 std::unique_ptr
<DWARFContext
> DwarfContext
=
2420 DWARFContext::create(*ErrOrSections
, 8);
2421 VerifyWarning(*DwarfContext
,
2422 "warning: .debug_line[0x00000000].prologue.file_names[2] is "
2423 "a duplicate of file_names[1]");
2426 TEST(DWARFDebugInfo
, TestDwarfVerifyCUDontShareLineTable
) {
2427 // Create a two compile units where both compile units share the same
2428 // DW_AT_stmt_list value and verify we report the error correctly.
2429 StringRef yamldata
= R
"(
2436 Tag: DW_TAG_compile_unit
2437 Children: DW_CHILDREN_no
2439 - Attribute: DW_AT_name
2441 - Attribute: DW_AT_stmt_list
2442 Form: DW_FORM_sec_offset
2450 - AbbrCode: 0x00000001
2452 - Value: 0x0000000000000001
2453 - Value: 0x0000000000000000
2460 - AbbrCode: 0x00000001
2462 - Value: 0x000000000000000D
2463 - Value: 0x0000000000000000
2474 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
2483 - Opcode: DW_LNS_extended_op
2485 SubOpcode: DW_LNE_set_address
2487 - Opcode: DW_LNS_advance_line
2490 - Opcode: DW_LNS_copy
2492 - Opcode: DW_LNS_advance_pc
2494 - Opcode: DW_LNS_extended_op
2496 SubOpcode: DW_LNE_end_sequence
2499 auto ErrOrSections
= DWARFYAML::EmitDebugSections(yamldata
);
2500 ASSERT_TRUE((bool)ErrOrSections
);
2501 std::unique_ptr
<DWARFContext
> DwarfContext
=
2502 DWARFContext::create(*ErrOrSections
, 8);
2503 VerifyError(*DwarfContext
,
2504 "error: two compile unit DIEs, 0x0000000b and "
2505 "0x0000001f, have the same DW_AT_stmt_list section "
2509 TEST(DWARFDebugInfo
, TestErrorReportingPolicy
) {
2510 Triple
Triple("x86_64-pc-linux");
2511 if (!isConfigurationSupported(Triple
))
2514 auto ExpectedDG
= dwarfgen::Generator::create(Triple
, 4 /*DwarfVersion*/);
2515 ASSERT_THAT_EXPECTED(ExpectedDG
, Succeeded());
2516 dwarfgen::Generator
*DG
= ExpectedDG
.get().get();
2517 AsmPrinter
*AP
= DG
->getAsmPrinter();
2518 MCContext
*MC
= DG
->getMCContext();
2520 // Emit two compressed sections with broken headers.
2521 AP
->OutStreamer
->SwitchSection(
2522 MC
->getELFSection(".zdebug_foo", 0 /*Type*/, 0 /*Flags*/));
2523 AP
->OutStreamer
->EmitBytes("0");
2524 AP
->OutStreamer
->SwitchSection(
2525 MC
->getELFSection(".zdebug_bar", 0 /*Type*/, 0 /*Flags*/));
2526 AP
->OutStreamer
->EmitBytes("0");
2528 MemoryBufferRef
FileBuffer(DG
->generate(), "dwarf");
2529 auto Obj
= object::ObjectFile::createObjectFile(FileBuffer
);
2530 EXPECT_TRUE((bool)Obj
);
2532 // Case 1: error handler handles all errors. That allows
2533 // DWARFContext to parse whole file and find both two errors we know about.
2535 std::unique_ptr
<DWARFContext
> Ctx1
=
2536 DWARFContext::create(**Obj
, nullptr, [&](Error E
) {
2538 consumeError(std::move(E
));
2539 return ErrorPolicy::Continue
;
2541 EXPECT_TRUE(Errors
== 2);
2543 // Case 2: error handler stops parsing of object after first error.
2545 std::unique_ptr
<DWARFContext
> Ctx2
=
2546 DWARFContext::create(**Obj
, nullptr, [&](Error E
) {
2548 consumeError(std::move(E
));
2549 return ErrorPolicy::Halt
;
2551 EXPECT_TRUE(Errors
== 1);
2554 TEST(DWARFDebugInfo
, TestDwarfVerifyCURangesIncomplete
) {
2555 // Create a single compile unit with a single function. The compile
2556 // unit has a DW_AT_ranges attribute that doesn't fully contain the
2557 // address range of the function. The verification should fail due to
2558 // the CU ranges not containing all of the address ranges of all of the
2560 StringRef yamldata
= R
"(
2566 Tag: DW_TAG_compile_unit
2567 Children: DW_CHILDREN_yes
2569 - Attribute: DW_AT_low_pc
2571 - Attribute: DW_AT_high_pc
2573 - Attribute: DW_AT_name
2576 Tag: DW_TAG_subprogram
2577 Children: DW_CHILDREN_no
2579 - Attribute: DW_AT_low_pc
2581 - Attribute: DW_AT_high_pc
2590 - AbbrCode: 0x00000001
2592 - Value: 0x0000000000001000
2593 - Value: 0x0000000000001500
2594 - Value: 0x0000000000000001
2595 - AbbrCode: 0x00000002
2597 - Value: 0x0000000000001000
2598 - Value: 0x0000000000002000
2599 - AbbrCode: 0x00000000
2602 auto ErrOrSections
= DWARFYAML::EmitDebugSections(yamldata
);
2603 ASSERT_TRUE((bool)ErrOrSections
);
2604 std::unique_ptr
<DWARFContext
> DwarfContext
=
2605 DWARFContext::create(*ErrOrSections
, 8);
2606 VerifyError(*DwarfContext
, "error: DIE address ranges are not "
2607 "contained in its parent's ranges:");
2610 TEST(DWARFDebugInfo
, TestDwarfVerifyLexicalBlockRanges
) {
2611 // Create a single compile unit with a single function that has a lexical
2612 // block whose address range is not contained in the function address range.
2613 StringRef yamldata
= R
"(
2620 Tag: DW_TAG_compile_unit
2621 Children: DW_CHILDREN_yes
2623 - Attribute: DW_AT_name
2626 Tag: DW_TAG_subprogram
2627 Children: DW_CHILDREN_yes
2629 - Attribute: DW_AT_name
2631 - Attribute: DW_AT_low_pc
2633 - Attribute: DW_AT_high_pc
2636 Tag: DW_TAG_lexical_block
2637 Children: DW_CHILDREN_no
2639 - Attribute: DW_AT_low_pc
2641 - Attribute: DW_AT_high_pc
2650 - AbbrCode: 0x00000001
2652 - Value: 0x0000000000000001
2653 - AbbrCode: 0x00000002
2655 - Value: 0x000000000000000D
2656 - Value: 0x0000000000001000
2657 - Value: 0x0000000000002000
2658 - AbbrCode: 0x00000003
2660 - Value: 0x0000000000001000
2661 - Value: 0x0000000000002001
2662 - AbbrCode: 0x00000000
2664 - AbbrCode: 0x00000000
2667 auto ErrOrSections
= DWARFYAML::EmitDebugSections(yamldata
);
2668 ASSERT_TRUE((bool)ErrOrSections
);
2669 std::unique_ptr
<DWARFContext
> DwarfContext
=
2670 DWARFContext::create(*ErrOrSections
, 8);
2671 VerifyError(*DwarfContext
, "error: DIE address ranges are not "
2672 "contained in its parent's ranges:");
2675 TEST(DWARFDebugInfo
, TestDwarfVerifyOverlappingFunctionRanges
) {
2676 // Create a single compile unit with a two functions that have overlapping
2678 StringRef yamldata
= R
"(
2686 Tag: DW_TAG_compile_unit
2687 Children: DW_CHILDREN_yes
2689 - Attribute: DW_AT_name
2692 Tag: DW_TAG_subprogram
2693 Children: DW_CHILDREN_no
2695 - Attribute: DW_AT_name
2697 - Attribute: DW_AT_low_pc
2699 - Attribute: DW_AT_high_pc
2708 - AbbrCode: 0x00000001
2710 - Value: 0x0000000000000001
2711 - AbbrCode: 0x00000002
2713 - Value: 0x000000000000000D
2714 - Value: 0x0000000000001000
2715 - Value: 0x0000000000002000
2716 - AbbrCode: 0x00000002
2718 - Value: 0x0000000000000012
2719 - Value: 0x0000000000001FFF
2720 - Value: 0x0000000000002000
2721 - AbbrCode: 0x00000000
2724 auto ErrOrSections
= DWARFYAML::EmitDebugSections(yamldata
);
2725 ASSERT_TRUE((bool)ErrOrSections
);
2726 std::unique_ptr
<DWARFContext
> DwarfContext
=
2727 DWARFContext::create(*ErrOrSections
, 8);
2728 VerifyError(*DwarfContext
, "error: DIEs have overlapping address ranges:");
2731 TEST(DWARFDebugInfo
, TestDwarfVerifyOverlappingLexicalBlockRanges
) {
2732 // Create a single compile unit with a one function that has two lexical
2733 // blocks with overlapping address ranges.
2734 StringRef yamldata
= R
"(
2741 Tag: DW_TAG_compile_unit
2742 Children: DW_CHILDREN_yes
2744 - Attribute: DW_AT_low_pc
2746 - Attribute: DW_AT_high_pc
2748 - Attribute: DW_AT_name
2751 Tag: DW_TAG_subprogram
2752 Children: DW_CHILDREN_yes
2754 - Attribute: DW_AT_name
2756 - Attribute: DW_AT_low_pc
2758 - Attribute: DW_AT_high_pc
2761 Tag: DW_TAG_lexical_block
2762 Children: DW_CHILDREN_no
2764 - Attribute: DW_AT_low_pc
2766 - Attribute: DW_AT_high_pc
2775 - AbbrCode: 0x00000001
2777 - Value: 0x0000000000001000
2778 - Value: 0x0000000000002000
2779 - Value: 0x0000000000000001
2780 - AbbrCode: 0x00000002
2782 - Value: 0x000000000000000D
2783 - Value: 0x0000000000001000
2784 - Value: 0x0000000000002000
2785 - AbbrCode: 0x00000003
2787 - Value: 0x0000000000001100
2788 - Value: 0x0000000000001300
2789 - AbbrCode: 0x00000003
2791 - Value: 0x00000000000012FF
2792 - Value: 0x0000000000001300
2793 - AbbrCode: 0x00000000
2795 - AbbrCode: 0x00000000
2798 auto ErrOrSections
= DWARFYAML::EmitDebugSections(yamldata
);
2799 ASSERT_TRUE((bool)ErrOrSections
);
2800 std::unique_ptr
<DWARFContext
> DwarfContext
=
2801 DWARFContext::create(*ErrOrSections
, 8);
2802 VerifyError(*DwarfContext
, "error: DIEs have overlapping address ranges:");
2805 TEST(DWARFDebugInfo
, TestDwarfVerifyInvalidDIERange
) {
2806 // Create a single compile unit with a single function that has an invalid
2807 // address range where the high PC is smaller than the low PC.
2808 StringRef yamldata
= R
"(
2815 Tag: DW_TAG_compile_unit
2816 Children: DW_CHILDREN_yes
2818 - Attribute: DW_AT_name
2821 Tag: DW_TAG_subprogram
2822 Children: DW_CHILDREN_no
2824 - Attribute: DW_AT_name
2826 - Attribute: DW_AT_low_pc
2828 - Attribute: DW_AT_high_pc
2837 - AbbrCode: 0x00000001
2839 - Value: 0x0000000000000001
2840 - AbbrCode: 0x00000002
2842 - Value: 0x000000000000000D
2843 - Value: 0x0000000000001000
2844 - Value: 0x0000000000000900
2845 - AbbrCode: 0x00000000
2848 auto ErrOrSections
= DWARFYAML::EmitDebugSections(yamldata
);
2849 ASSERT_TRUE((bool)ErrOrSections
);
2850 std::unique_ptr
<DWARFContext
> DwarfContext
=
2851 DWARFContext::create(*ErrOrSections
, 8);
2852 VerifyError(*DwarfContext
, "error: Invalid address range");
2855 TEST(DWARFDebugInfo
, TestDwarfVerifyElidedDoesntFail
) {
2856 // Create a single compile unit with two functions: one that has a valid range
2857 // and one whose low and high PC are the same. When the low and high PC are
2858 // the same, this indicates the function was dead code stripped. We want to
2859 // ensure that verification succeeds.
2860 StringRef yamldata
= R
"(
2868 Tag: DW_TAG_compile_unit
2869 Children: DW_CHILDREN_yes
2871 - Attribute: DW_AT_low_pc
2873 - Attribute: DW_AT_high_pc
2875 - Attribute: DW_AT_name
2878 Tag: DW_TAG_subprogram
2879 Children: DW_CHILDREN_no
2881 - Attribute: DW_AT_name
2883 - Attribute: DW_AT_low_pc
2885 - Attribute: DW_AT_high_pc
2894 - AbbrCode: 0x00000001
2896 - Value: 0x0000000000001000
2897 - Value: 0x0000000000002000
2898 - Value: 0x0000000000000001
2899 - AbbrCode: 0x00000002
2901 - Value: 0x000000000000000D
2902 - Value: 0x0000000000001000
2903 - Value: 0x0000000000002000
2904 - AbbrCode: 0x00000002
2906 - Value: 0x0000000000000012
2907 - Value: 0x0000000000002000
2908 - Value: 0x0000000000002000
2909 - AbbrCode: 0x00000000
2912 auto ErrOrSections
= DWARFYAML::EmitDebugSections(yamldata
);
2913 ASSERT_TRUE((bool)ErrOrSections
);
2914 std::unique_ptr
<DWARFContext
> DwarfContext
=
2915 DWARFContext::create(*ErrOrSections
, 8);
2916 VerifySuccess(*DwarfContext
);
2919 TEST(DWARFDebugInfo
, TestDwarfVerifyNestedFunctions
) {
2920 // Create a single compile unit with a nested function which is not contained
2921 // in its parent. Although LLVM doesn't generate this, it is valid accoridng
2922 // to the DWARF standard.
2923 StringRef yamldata
= R
"(
2931 Tag: DW_TAG_compile_unit
2932 Children: DW_CHILDREN_yes
2934 - Attribute: DW_AT_low_pc
2936 - Attribute: DW_AT_high_pc
2938 - Attribute: DW_AT_name
2941 Tag: DW_TAG_subprogram
2942 Children: DW_CHILDREN_yes
2944 - Attribute: DW_AT_name
2946 - Attribute: DW_AT_low_pc
2948 - Attribute: DW_AT_high_pc
2957 - AbbrCode: 0x00000001
2959 - Value: 0x0000000000001000
2960 - Value: 0x0000000000002000
2961 - Value: 0x0000000000000001
2962 - AbbrCode: 0x00000002
2964 - Value: 0x000000000000000D
2965 - Value: 0x0000000000001000
2966 - Value: 0x0000000000001500
2967 - AbbrCode: 0x00000002
2969 - Value: 0x0000000000000012
2970 - Value: 0x0000000000001500
2971 - Value: 0x0000000000002000
2972 - AbbrCode: 0x00000000
2974 - AbbrCode: 0x00000000
2976 - AbbrCode: 0x00000000
2979 auto ErrOrSections
= DWARFYAML::EmitDebugSections(yamldata
);
2980 ASSERT_TRUE((bool)ErrOrSections
);
2981 std::unique_ptr
<DWARFContext
> DwarfContext
=
2982 DWARFContext::create(*ErrOrSections
, 8);
2983 VerifySuccess(*DwarfContext
);
2986 TEST(DWARFDebugInfo
, TestDWARFDieRangeInfoContains
) {
2987 DWARFVerifier::DieRangeInfo Empty
;
2988 ASSERT_TRUE(Empty
.contains(Empty
));
2990 DWARFVerifier::DieRangeInfo
Ranges(
2991 {{0x10, 0x20}, {0x30, 0x40}, {0x40, 0x50}});
2993 ASSERT_TRUE(Ranges
.contains(Empty
));
2994 ASSERT_FALSE(Ranges
.contains({{{0x0f, 0x10}}}));
2995 ASSERT_FALSE(Ranges
.contains({{{0x0f, 0x20}}}));
2996 ASSERT_FALSE(Ranges
.contains({{{0x0f, 0x21}}}));
2998 // Test ranges that start at R's start address
2999 ASSERT_TRUE(Ranges
.contains({{{0x10, 0x10}}}));
3000 ASSERT_TRUE(Ranges
.contains({{{0x10, 0x11}}}));
3001 ASSERT_TRUE(Ranges
.contains({{{0x10, 0x20}}}));
3002 ASSERT_FALSE(Ranges
.contains({{{0x10, 0x21}}}));
3004 ASSERT_TRUE(Ranges
.contains({{{0x11, 0x12}}}));
3006 // Test ranges that start at last bytes of Range
3007 ASSERT_TRUE(Ranges
.contains({{{0x1f, 0x20}}}));
3008 ASSERT_FALSE(Ranges
.contains({{{0x1f, 0x21}}}));
3010 // Test ranges that start after Range
3011 ASSERT_TRUE(Ranges
.contains({{{0x20, 0x20}}}));
3012 ASSERT_FALSE(Ranges
.contains({{{0x20, 0x21}}}));
3014 ASSERT_TRUE(Ranges
.contains({{{0x31, 0x32}}}));
3015 ASSERT_TRUE(Ranges
.contains({{{0x3f, 0x40}}}));
3016 ASSERT_TRUE(Ranges
.contains({{{0x10, 0x20}, {0x30, 0x40}}}));
3017 ASSERT_TRUE(Ranges
.contains({{{0x11, 0x12}, {0x31, 0x32}}}));
3018 ASSERT_TRUE(Ranges
.contains(
3019 {{{0x11, 0x12}, {0x12, 0x13}, {0x31, 0x32}, {0x32, 0x33}}}));
3020 ASSERT_FALSE(Ranges
.contains({{{0x11, 0x12},
3025 ASSERT_FALSE(Ranges
.contains(
3026 {{{0x11, 0x12}, {0x12, 0x13}, {0x31, 0x32}, {0x32, 0x51}}}));
3027 ASSERT_TRUE(Ranges
.contains({{{0x11, 0x12}, {0x30, 0x50}}}));
3028 ASSERT_FALSE(Ranges
.contains({{{0x30, 0x51}}}));
3029 ASSERT_FALSE(Ranges
.contains({{{0x50, 0x51}}}));
3034 void AssertRangesIntersect(const DWARFAddressRange
&LHS
,
3035 const DWARFAddressRange
&RHS
) {
3036 ASSERT_TRUE(LHS
.intersects(RHS
));
3037 ASSERT_TRUE(RHS
.intersects(LHS
));
3039 void AssertRangesDontIntersect(const DWARFAddressRange
&LHS
,
3040 const DWARFAddressRange
&RHS
) {
3041 ASSERT_FALSE(LHS
.intersects(RHS
));
3042 ASSERT_FALSE(RHS
.intersects(LHS
));
3045 void AssertRangesIntersect(const DWARFVerifier::DieRangeInfo
&LHS
,
3046 const DWARFAddressRangesVector
&Ranges
) {
3047 DWARFVerifier::DieRangeInfo
RHS(Ranges
);
3048 ASSERT_TRUE(LHS
.intersects(RHS
));
3049 ASSERT_TRUE(RHS
.intersects(LHS
));
3052 void AssertRangesDontIntersect(const DWARFVerifier::DieRangeInfo
&LHS
,
3053 const DWARFAddressRangesVector
&Ranges
) {
3054 DWARFVerifier::DieRangeInfo
RHS(Ranges
);
3055 ASSERT_FALSE(LHS
.intersects(RHS
));
3056 ASSERT_FALSE(RHS
.intersects(LHS
));
3060 TEST(DWARFDebugInfo
, TestDwarfRangesIntersect
) {
3061 DWARFAddressRange
R(0x10, 0x20);
3063 //----------------------------------------------------------------------
3064 // Test ranges that start before R...
3065 //----------------------------------------------------------------------
3066 // Other range ends before start of R
3067 AssertRangesDontIntersect(R
, {0x00, 0x10});
3068 // Other range end address is start of a R
3069 AssertRangesIntersect(R
, {0x00, 0x11});
3070 // Other range end address is in R
3071 AssertRangesIntersect(R
, {0x00, 0x15});
3072 // Other range end address is at and of R
3073 AssertRangesIntersect(R
, {0x00, 0x20});
3074 // Other range end address is past end of R
3075 AssertRangesIntersect(R
, {0x00, 0x40});
3077 //----------------------------------------------------------------------
3078 // Test ranges that start at R's start address
3079 //----------------------------------------------------------------------
3080 // Ensure empty ranges doesn't match
3081 AssertRangesDontIntersect(R
, {0x10, 0x10});
3083 AssertRangesIntersect(R
, {0x10, 0x11});
3085 AssertRangesIntersect(R
, {0x10, 0x20});
3086 // 1 byte past Range
3087 AssertRangesIntersect(R
, {0x10, 0x21});
3089 //----------------------------------------------------------------------
3090 // Test ranges that start inside Range
3091 //----------------------------------------------------------------------
3093 AssertRangesDontIntersect(R
, {0x11, 0x11});
3095 AssertRangesIntersect(R
, {0x11, 0x1f});
3096 // ends at end of Range
3097 AssertRangesIntersect(R
, {0x11, 0x20});
3099 AssertRangesIntersect(R
, {0x11, 0x21});
3101 //----------------------------------------------------------------------
3102 // Test ranges that start at last bytes of Range
3103 //----------------------------------------------------------------------
3104 // ends at end of Range
3105 AssertRangesIntersect(R
, {0x1f, 0x20});
3107 AssertRangesIntersect(R
, {0x1f, 0x21});
3109 //----------------------------------------------------------------------
3110 // Test ranges that start after Range
3111 //----------------------------------------------------------------------
3112 // empty just past in Range
3113 AssertRangesDontIntersect(R
, {0x20, 0x20});
3115 AssertRangesDontIntersect(R
, {0x20, 0x21});
3118 TEST(DWARFDebugInfo
, TestDWARFDieRangeInfoIntersects
) {
3120 DWARFVerifier::DieRangeInfo
Ranges({{0x10, 0x20}, {0x30, 0x40}});
3123 AssertRangesDontIntersect(Ranges
, {});
3124 // Test range that appears before all ranges in Ranges
3125 AssertRangesDontIntersect(Ranges
, {{0x00, 0x10}});
3126 // Test range that appears between ranges in Ranges
3127 AssertRangesDontIntersect(Ranges
, {{0x20, 0x30}});
3128 // Test range that appears after ranges in Ranges
3129 AssertRangesDontIntersect(Ranges
, {{0x40, 0x50}});
3131 // Test range that start before first range
3132 AssertRangesIntersect(Ranges
, {{0x00, 0x11}});
3133 // Test range that start at first range
3134 AssertRangesIntersect(Ranges
, {{0x10, 0x11}});
3135 // Test range that start in first range
3136 AssertRangesIntersect(Ranges
, {{0x11, 0x12}});
3137 // Test range that start at end of first range
3138 AssertRangesIntersect(Ranges
, {{0x1f, 0x20}});
3139 // Test range that starts at end of first range
3140 AssertRangesDontIntersect(Ranges
, {{0x20, 0x21}});
3141 // Test range that starts at end of first range
3142 AssertRangesIntersect(Ranges
, {{0x20, 0x31}});
3144 // Test range that start before second range and ends before second
3145 AssertRangesDontIntersect(Ranges
, {{0x2f, 0x30}});
3146 // Test range that start before second range and ends in second
3147 AssertRangesIntersect(Ranges
, {{0x2f, 0x31}});
3148 // Test range that start at second range
3149 AssertRangesIntersect(Ranges
, {{0x30, 0x31}});
3150 // Test range that start in second range
3151 AssertRangesIntersect(Ranges
, {{0x31, 0x32}});
3152 // Test range that start at end of second range
3153 AssertRangesIntersect(Ranges
, {{0x3f, 0x40}});
3154 // Test range that starts at end of second range
3155 AssertRangesDontIntersect(Ranges
, {{0x40, 0x41}});
3157 AssertRangesDontIntersect(Ranges
, {{0x20, 0x21}, {0x2f, 0x30}});
3158 AssertRangesIntersect(Ranges
, {{0x20, 0x21}, {0x2f, 0x31}});
3161 TEST(DWARFDebugInfo
, TestDWARF64UnitLength
) {
3162 static const char DebugInfoSecRaw
[] =
3163 "\xff\xff\xff\xff" // DWARF64 mark
3164 "\x88\x77\x66\x55\x44\x33\x22\x11" // Length
3165 "\x05\x00" // Version
3166 "\x01" // DW_UT_compile
3167 "\x04" // Address size
3168 "\0\0\0\0\0\0\0\0"; // Offset Into Abbrev. Sec.
3169 StringMap
<std::unique_ptr
<MemoryBuffer
>> Sections
;
3170 Sections
.insert(std::make_pair(
3171 "debug_info", MemoryBuffer::getMemBuffer(StringRef(
3172 DebugInfoSecRaw
, sizeof(DebugInfoSecRaw
) - 1))));
3173 auto Context
= DWARFContext::create(Sections
, /* AddrSize = */ 4,
3174 /* isLittleEndian = */ true);
3175 const auto &Obj
= Context
->getDWARFObj();
3176 Obj
.forEachInfoSections([&](const DWARFSection
&Sec
) {
3177 DWARFUnitHeader Header
;
3178 DWARFDataExtractor
Data(Obj
, Sec
, /* IsLittleEndian = */ true,
3179 /* AddressSize = */ 4);
3180 uint64_t Offset
= 0;
3181 EXPECT_FALSE(Header
.extract(*Context
, Data
, &Offset
));
3182 // Header.extract() returns false because there is not enough space
3183 // in the section for the declared length. Anyway, we can check that
3184 // the properties are read correctly.
3185 ASSERT_EQ(DwarfFormat::DWARF64
, Header
.getFormat());
3186 ASSERT_EQ(0x1122334455667788ULL
, Header
.getLength());
3187 ASSERT_EQ(5, Header
.getVersion());
3188 ASSERT_EQ(DW_UT_compile
, Header
.getUnitType());
3189 ASSERT_EQ(4, Header
.getAddressByteSize());
3191 // Check that the length can be correctly read in the unit class.
3192 DWARFUnitVector DummyUnitVector
;
3193 DWARFSection DummySec
;
3194 DWARFCompileUnit
CU(*Context
, Sec
, Header
, /* DA = */ 0, /* RS = */ 0,
3195 /* LocSection = */ 0, /* SS = */ StringRef(),
3196 /* SOS = */ DummySec
, /* AOS = */ 0,
3197 /* LS = */ DummySec
, /* LE = */ true,
3198 /* isDWO= */ false, DummyUnitVector
);
3199 ASSERT_EQ(0x1122334455667788ULL
, CU
.getLength());
3203 } // end anonymous namespace