1 //===- llvm/unittest/DebugInfo/DWARFFormValueTest.cpp ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "DwarfGenerator.h"
11 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
12 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
13 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
14 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
15 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
16 #include "llvm/Support/Dwarf.h"
17 #include "llvm/Support/Host.h"
18 #include "llvm/Support/TargetSelect.h"
19 #include "gtest/gtest.h"
23 using namespace dwarf;
27 void initLLVMIfNeeded() {
28 static bool gInitialized = false;
31 InitializeAllTargets();
32 InitializeAllTargetMCs();
33 InitializeAllAsmPrinters();
34 InitializeAllAsmParsers();
38 Triple getHostTripleForAddrSize(uint8_t AddrSize) {
39 Triple PT(Triple::normalize(LLVM_HOST_TRIPLE));
41 if (AddrSize == 8 && PT.isArch32Bit())
42 return PT.get64BitArchVariant();
43 if (AddrSize == 4 && PT.isArch64Bit())
44 return PT.get32BitArchVariant();
48 /// Take any llvm::Expected and check and handle any errors.
50 /// \param Expected a llvm::Excepted instance to check.
51 /// \returns true if there were errors, false otherwise.
53 static bool HandleExpectedError(T &Expected) {
55 handleAllErrors(Expected.takeError(), [&](const llvm::ErrorInfoBase &EI) {
56 ErrorMsg = EI.message();
58 if (!ErrorMsg.empty()) {
59 ::testing::AssertionFailure() << "error: " << ErrorMsg;
65 template <uint16_t Version, class AddrType, class RefAddrType>
67 // Test that we can decode all DW_FORM values correctly.
69 const uint8_t AddrSize = sizeof(AddrType);
70 const AddrType AddrValue = (AddrType)0x0123456789abcdefULL;
71 const uint8_t BlockData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
72 const uint32_t BlockSize = sizeof(BlockData);
73 const RefAddrType RefAddr = 0x12345678;
74 const uint8_t Data1 = 0x01U;
75 const uint16_t Data2 = 0x2345U;
76 const uint32_t Data4 = 0x6789abcdU;
77 const uint64_t Data8 = 0x0011223344556677ULL;
78 const uint64_t Data8_2 = 0xAABBCCDDEEFF0011ULL;
79 const int64_t SData = INT64_MIN;
80 const uint64_t UData[] = {UINT64_MAX - 1, UINT64_MAX - 2, UINT64_MAX - 3,
81 UINT64_MAX - 4, UINT64_MAX - 5, UINT64_MAX - 6,
82 UINT64_MAX - 7, UINT64_MAX - 8, UINT64_MAX - 9};
83 #define UDATA_1 18446744073709551614ULL
84 const uint32_t Dwarf32Values[] = {1, 2, 3, 4, 5, 6, 7, 8};
85 const char *StringValue = "Hello";
86 const char *StrpValue = "World";
88 Triple Triple = getHostTripleForAddrSize(AddrSize);
89 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
90 if (HandleExpectedError(ExpectedDG))
92 dwarfgen::Generator *DG = ExpectedDG.get().get();
93 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
94 dwarfgen::DIE CUDie = CU.getUnitDIE();
95 uint16_t Attr = DW_AT_lo_user;
97 //----------------------------------------------------------------------
99 //----------------------------------------------------------------------
100 const auto Attr_DW_FORM_addr = static_cast<dwarf::Attribute>(Attr++);
101 CUDie.addAttribute(Attr_DW_FORM_addr, DW_FORM_addr, AddrValue);
103 //----------------------------------------------------------------------
105 //----------------------------------------------------------------------
106 const auto Attr_DW_FORM_block = static_cast<dwarf::Attribute>(Attr++);
107 CUDie.addAttribute(Attr_DW_FORM_block, DW_FORM_block, BlockData, BlockSize);
109 const auto Attr_DW_FORM_block1 = static_cast<dwarf::Attribute>(Attr++);
110 CUDie.addAttribute(Attr_DW_FORM_block1, DW_FORM_block1, BlockData, BlockSize);
112 const auto Attr_DW_FORM_block2 = static_cast<dwarf::Attribute>(Attr++);
113 CUDie.addAttribute(Attr_DW_FORM_block2, DW_FORM_block2, BlockData, BlockSize);
115 const auto Attr_DW_FORM_block4 = static_cast<dwarf::Attribute>(Attr++);
116 CUDie.addAttribute(Attr_DW_FORM_block4, DW_FORM_block4, BlockData, BlockSize);
118 //----------------------------------------------------------------------
120 //----------------------------------------------------------------------
121 const auto Attr_DW_FORM_data1 = static_cast<dwarf::Attribute>(Attr++);
122 CUDie.addAttribute(Attr_DW_FORM_data1, DW_FORM_data1, Data1);
124 const auto Attr_DW_FORM_data2 = static_cast<dwarf::Attribute>(Attr++);
125 CUDie.addAttribute(Attr_DW_FORM_data2, DW_FORM_data2, Data2);
127 const auto Attr_DW_FORM_data4 = static_cast<dwarf::Attribute>(Attr++);
128 CUDie.addAttribute(Attr_DW_FORM_data4, DW_FORM_data4, Data4);
130 const auto Attr_DW_FORM_data8 = static_cast<dwarf::Attribute>(Attr++);
131 CUDie.addAttribute(Attr_DW_FORM_data8, DW_FORM_data8, Data8);
133 //----------------------------------------------------------------------
135 //----------------------------------------------------------------------
136 const auto Attr_DW_FORM_string = static_cast<dwarf::Attribute>(Attr++);
137 CUDie.addAttribute(Attr_DW_FORM_string, DW_FORM_string, StringValue);
139 const auto Attr_DW_FORM_strp = static_cast<dwarf::Attribute>(Attr++);
140 CUDie.addAttribute(Attr_DW_FORM_strp, DW_FORM_strp, StrpValue);
142 //----------------------------------------------------------------------
143 // Test reference forms
144 //----------------------------------------------------------------------
145 const auto Attr_DW_FORM_ref_addr = static_cast<dwarf::Attribute>(Attr++);
146 CUDie.addAttribute(Attr_DW_FORM_ref_addr, DW_FORM_ref_addr, RefAddr);
148 const auto Attr_DW_FORM_ref1 = static_cast<dwarf::Attribute>(Attr++);
149 CUDie.addAttribute(Attr_DW_FORM_ref1, DW_FORM_ref1, Data1);
151 const auto Attr_DW_FORM_ref2 = static_cast<dwarf::Attribute>(Attr++);
152 CUDie.addAttribute(Attr_DW_FORM_ref2, DW_FORM_ref2, Data2);
154 const auto Attr_DW_FORM_ref4 = static_cast<dwarf::Attribute>(Attr++);
155 CUDie.addAttribute(Attr_DW_FORM_ref4, DW_FORM_ref4, Data4);
157 const auto Attr_DW_FORM_ref8 = static_cast<dwarf::Attribute>(Attr++);
158 CUDie.addAttribute(Attr_DW_FORM_ref8, DW_FORM_ref8, Data8);
160 const auto Attr_DW_FORM_ref_sig8 = static_cast<dwarf::Attribute>(Attr++);
161 CUDie.addAttribute(Attr_DW_FORM_ref_sig8, DW_FORM_ref_sig8, Data8_2);
163 const auto Attr_DW_FORM_ref_udata = static_cast<dwarf::Attribute>(Attr++);
164 CUDie.addAttribute(Attr_DW_FORM_ref_udata, DW_FORM_ref_udata, UData[0]);
166 //----------------------------------------------------------------------
168 //----------------------------------------------------------------------
169 const auto Attr_DW_FORM_flag_true = static_cast<dwarf::Attribute>(Attr++);
170 CUDie.addAttribute(Attr_DW_FORM_flag_true, DW_FORM_flag, true);
172 const auto Attr_DW_FORM_flag_false = static_cast<dwarf::Attribute>(Attr++);
173 CUDie.addAttribute(Attr_DW_FORM_flag_false, DW_FORM_flag, false);
175 const auto Attr_DW_FORM_flag_present = static_cast<dwarf::Attribute>(Attr++);
176 CUDie.addAttribute(Attr_DW_FORM_flag_present, DW_FORM_flag_present);
178 //----------------------------------------------------------------------
179 // Test SLEB128 based forms
180 //----------------------------------------------------------------------
181 const auto Attr_DW_FORM_sdata = static_cast<dwarf::Attribute>(Attr++);
182 CUDie.addAttribute(Attr_DW_FORM_sdata, DW_FORM_sdata, SData);
184 //----------------------------------------------------------------------
185 // Test ULEB128 based forms
186 //----------------------------------------------------------------------
187 const auto Attr_DW_FORM_udata = static_cast<dwarf::Attribute>(Attr++);
188 CUDie.addAttribute(Attr_DW_FORM_udata, DW_FORM_udata, UData[0]);
190 //----------------------------------------------------------------------
191 // Test DWARF32/DWARF64 forms
192 //----------------------------------------------------------------------
193 const auto Attr_DW_FORM_GNU_ref_alt = static_cast<dwarf::Attribute>(Attr++);
194 CUDie.addAttribute(Attr_DW_FORM_GNU_ref_alt, DW_FORM_GNU_ref_alt,
197 const auto Attr_DW_FORM_sec_offset = static_cast<dwarf::Attribute>(Attr++);
198 CUDie.addAttribute(Attr_DW_FORM_sec_offset, DW_FORM_sec_offset,
201 //----------------------------------------------------------------------
202 // Add an address at the end to make sure we can decode this value
203 //----------------------------------------------------------------------
204 const auto Attr_Last = static_cast<dwarf::Attribute>(Attr++);
205 CUDie.addAttribute(Attr_Last, DW_FORM_addr, AddrValue);
207 //----------------------------------------------------------------------
208 // Generate the DWARF
209 //----------------------------------------------------------------------
210 StringRef FileBytes = DG->generate();
211 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
212 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
213 EXPECT_TRUE((bool)Obj);
214 DWARFContextInMemory DwarfContext(*Obj.get());
215 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
216 EXPECT_EQ(NumCUs, 1u);
217 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
218 auto DieDG = U->getUnitDIE(false);
219 EXPECT_TRUE(DieDG.isValid());
221 //----------------------------------------------------------------------
222 // Test address forms
223 //----------------------------------------------------------------------
224 EXPECT_EQ(DieDG.getAttributeValueAsAddress(Attr_DW_FORM_addr, 0),
227 //----------------------------------------------------------------------
229 //----------------------------------------------------------------------
230 Optional<DWARFFormValue> FormValue;
231 ArrayRef<uint8_t> ExtractedBlockData;
232 Optional<ArrayRef<uint8_t>> BlockDataOpt;
234 FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block);
235 EXPECT_TRUE((bool)FormValue);
236 BlockDataOpt = FormValue->getAsBlock();
237 EXPECT_TRUE(BlockDataOpt.hasValue());
238 ExtractedBlockData = BlockDataOpt.getValue();
239 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
240 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
242 FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block1);
243 EXPECT_TRUE((bool)FormValue);
244 BlockDataOpt = FormValue->getAsBlock();
245 EXPECT_TRUE(BlockDataOpt.hasValue());
246 ExtractedBlockData = BlockDataOpt.getValue();
247 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
248 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
250 FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block2);
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.getAttributeValue(Attr_DW_FORM_block4);
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 //----------------------------------------------------------------------
268 //----------------------------------------------------------------------
270 DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data1, 0),
273 DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data2, 0),
276 DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data4, 0),
279 DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data8, 0),
282 //----------------------------------------------------------------------
284 //----------------------------------------------------------------------
285 const char *ExtractedStringValue =
286 DieDG.getAttributeValueAsString(Attr_DW_FORM_string, nullptr);
287 EXPECT_TRUE(ExtractedStringValue != nullptr);
288 EXPECT_TRUE(strcmp(StringValue, ExtractedStringValue) == 0);
290 const char *ExtractedStrpValue =
291 DieDG.getAttributeValueAsString(Attr_DW_FORM_strp, nullptr);
292 EXPECT_TRUE(ExtractedStrpValue != nullptr);
293 EXPECT_TRUE(strcmp(StrpValue, ExtractedStrpValue) == 0);
295 //----------------------------------------------------------------------
296 // Test reference forms
297 //----------------------------------------------------------------------
298 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_addr, 0),
300 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref1, 0),
302 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref2, 0),
304 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref4, 0),
306 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref8, 0),
308 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_sig8, 0),
310 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_udata, 0),
313 //----------------------------------------------------------------------
315 //----------------------------------------------------------------------
316 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(
317 Attr_DW_FORM_flag_true, 0ULL),
319 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(
320 Attr_DW_FORM_flag_false, 1ULL),
322 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(
323 Attr_DW_FORM_flag_present, 0ULL),
326 // TODO: test Attr_DW_FORM_implicit_const extraction
328 //----------------------------------------------------------------------
329 // Test SLEB128 based forms
330 //----------------------------------------------------------------------
331 EXPECT_EQ(DieDG.getAttributeValueAsSignedConstant(Attr_DW_FORM_sdata, 0),
334 //----------------------------------------------------------------------
335 // Test ULEB128 based forms
336 //----------------------------------------------------------------------
338 DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_udata, 0),
341 //----------------------------------------------------------------------
342 // Test DWARF32/DWARF64 forms
343 //----------------------------------------------------------------------
345 DieDG.getAttributeValueAsReference(Attr_DW_FORM_GNU_ref_alt, 0),
348 DieDG.getAttributeValueAsSectionOffset(Attr_DW_FORM_sec_offset, 0),
351 //----------------------------------------------------------------------
352 // Add an address at the end to make sure we can decode this value
353 //----------------------------------------------------------------------
354 EXPECT_EQ(DieDG.getAttributeValueAsAddress(Attr_Last, 0), AddrValue);
357 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4AllForms) {
358 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
360 typedef uint32_t AddrType;
361 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2.
362 typedef AddrType RefAddrType;
363 TestAllForms<2, AddrType, RefAddrType>();
366 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8AllForms) {
367 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
369 typedef uint64_t AddrType;
370 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2.
371 typedef AddrType RefAddrType;
372 TestAllForms<2, AddrType, RefAddrType>();
375 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4AllForms) {
376 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
378 typedef uint32_t AddrType;
379 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later.
380 typedef uint32_t RefAddrType;
381 TestAllForms<3, AddrType, RefAddrType>();
384 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8AllForms) {
385 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
387 typedef uint64_t AddrType;
388 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
389 typedef uint32_t RefAddrType;
390 TestAllForms<3, AddrType, RefAddrType>();
393 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4AllForms) {
394 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
396 typedef uint32_t AddrType;
397 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
398 typedef uint32_t RefAddrType;
399 TestAllForms<4, AddrType, RefAddrType>();
402 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8AllForms) {
403 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
405 typedef uint64_t AddrType;
406 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
407 typedef uint32_t RefAddrType;
408 TestAllForms<4, AddrType, RefAddrType>();
411 template <uint16_t Version, class AddrType> void TestChildren() {
412 // Test that we can decode DW_FORM_ref_addr values correctly in DWARF 2 with
413 // 4 byte addresses. DW_FORM_ref_addr values should be 4 bytes when using
416 const uint8_t AddrSize = sizeof(AddrType);
418 Triple Triple = getHostTripleForAddrSize(AddrSize);
419 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
420 if (HandleExpectedError(ExpectedDG))
422 dwarfgen::Generator *DG = ExpectedDG.get().get();
423 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
424 dwarfgen::DIE CUDie = CU.getUnitDIE();
426 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
427 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
429 dwarfgen::DIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram);
430 SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main");
431 SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U);
432 SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U);
434 dwarfgen::DIE IntDie = CUDie.addChild(DW_TAG_base_type);
435 IntDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");
436 IntDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
437 IntDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
439 dwarfgen::DIE ArgcDie = SubprogramDie.addChild(DW_TAG_formal_parameter);
440 ArgcDie.addAttribute(DW_AT_name, DW_FORM_strp, "argc");
441 // ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref4, IntDie);
442 ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, IntDie);
444 StringRef FileBytes = DG->generate();
445 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
446 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
447 EXPECT_TRUE((bool)Obj);
448 DWARFContextInMemory DwarfContext(*Obj.get());
450 // Verify the number of compile units is correct.
451 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
452 EXPECT_EQ(NumCUs, 1u);
453 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
455 // Get the compile unit DIE is valid.
456 auto DieDG = U->getUnitDIE(false);
457 EXPECT_TRUE(DieDG.isValid());
458 // DieDG.dump(llvm::outs(), U, UINT32_MAX);
460 // Verify the first child of the compile unit DIE is our subprogram.
461 auto SubprogramDieDG = DieDG.getFirstChild();
462 EXPECT_TRUE(SubprogramDieDG.isValid());
463 EXPECT_EQ(SubprogramDieDG.getTag(), DW_TAG_subprogram);
465 // Verify the first child of the subprogram is our formal parameter.
466 auto ArgcDieDG = SubprogramDieDG.getFirstChild();
467 EXPECT_TRUE(ArgcDieDG.isValid());
468 EXPECT_EQ(ArgcDieDG.getTag(), DW_TAG_formal_parameter);
470 // Verify our formal parameter has a NULL tag sibling.
471 auto NullDieDG = ArgcDieDG.getSibling();
472 EXPECT_TRUE(NullDieDG.isValid());
474 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null);
475 EXPECT_TRUE(!NullDieDG.getSibling().isValid());
476 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid());
479 // Verify the sibling of our subprogram is our integer base type.
480 auto IntDieDG = SubprogramDieDG.getSibling();
481 EXPECT_TRUE(IntDieDG.isValid());
482 EXPECT_EQ(IntDieDG.getTag(), DW_TAG_base_type);
484 // Verify the sibling of our subprogram is our integer base is a NULL tag.
485 NullDieDG = IntDieDG.getSibling();
486 EXPECT_TRUE(NullDieDG.isValid());
488 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null);
489 EXPECT_TRUE(!NullDieDG.getSibling().isValid());
490 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid());
494 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Children) {
495 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
497 typedef uint32_t AddrType;
498 TestChildren<2, AddrType>();
501 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Children) {
502 // Test that we can decode all forms for DWARF32, version 2, with 8 byte
504 typedef uint64_t AddrType;
505 TestChildren<2, AddrType>();
508 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Children) {
509 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
511 typedef uint32_t AddrType;
512 TestChildren<3, AddrType>();
515 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Children) {
516 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
518 typedef uint64_t AddrType;
519 TestChildren<3, AddrType>();
522 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Children) {
523 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
525 typedef uint32_t AddrType;
526 TestChildren<4, AddrType>();
529 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Children) {
530 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
532 typedef uint64_t AddrType;
533 TestChildren<4, AddrType>();
536 template <uint16_t Version, class AddrType> void TestReferences() {
537 // Test that we can decode DW_FORM_refXXX values correctly in DWARF.
539 const uint8_t AddrSize = sizeof(AddrType);
541 Triple Triple = getHostTripleForAddrSize(AddrSize);
542 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
543 if (HandleExpectedError(ExpectedDG))
545 dwarfgen::Generator *DG = ExpectedDG.get().get();
546 dwarfgen::CompileUnit &CU1 = DG->addCompileUnit();
547 dwarfgen::CompileUnit &CU2 = DG->addCompileUnit();
549 dwarfgen::DIE CU1Die = CU1.getUnitDIE();
550 CU1Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
551 CU1Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
553 dwarfgen::DIE CU1TypeDie = CU1Die.addChild(DW_TAG_base_type);
554 CU1TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");
555 CU1TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
556 CU1TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
558 dwarfgen::DIE CU1Ref1Die = CU1Die.addChild(DW_TAG_variable);
559 CU1Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref1");
560 CU1Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU1TypeDie);
562 dwarfgen::DIE CU1Ref2Die = CU1Die.addChild(DW_TAG_variable);
563 CU1Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref2");
564 CU1Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU1TypeDie);
566 dwarfgen::DIE CU1Ref4Die = CU1Die.addChild(DW_TAG_variable);
567 CU1Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref4");
568 CU1Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU1TypeDie);
570 dwarfgen::DIE CU1Ref8Die = CU1Die.addChild(DW_TAG_variable);
571 CU1Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref8");
572 CU1Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU1TypeDie);
574 dwarfgen::DIE CU1RefAddrDie = CU1Die.addChild(DW_TAG_variable);
575 CU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1RefAddr");
576 CU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie);
578 dwarfgen::DIE CU2Die = CU2.getUnitDIE();
579 CU2Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/foo.c");
580 CU2Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
582 dwarfgen::DIE CU2TypeDie = CU2Die.addChild(DW_TAG_base_type);
583 CU2TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "float");
584 CU2TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_float);
585 CU2TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
587 dwarfgen::DIE CU2Ref1Die = CU2Die.addChild(DW_TAG_variable);
588 CU2Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref1");
589 CU2Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU2TypeDie);
591 dwarfgen::DIE CU2Ref2Die = CU2Die.addChild(DW_TAG_variable);
592 CU2Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref2");
593 CU2Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU2TypeDie);
595 dwarfgen::DIE CU2Ref4Die = CU2Die.addChild(DW_TAG_variable);
596 CU2Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref4");
597 CU2Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU2TypeDie);
599 dwarfgen::DIE CU2Ref8Die = CU2Die.addChild(DW_TAG_variable);
600 CU2Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref8");
601 CU2Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU2TypeDie);
603 dwarfgen::DIE CU2RefAddrDie = CU2Die.addChild(DW_TAG_variable);
604 CU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2RefAddr");
605 CU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie);
607 // Refer to a type in CU1 from CU2
608 dwarfgen::DIE CU2ToCU1RefAddrDie = CU2Die.addChild(DW_TAG_variable);
609 CU2ToCU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2ToCU1RefAddr");
610 CU2ToCU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie);
612 // Refer to a type in CU2 from CU1
613 dwarfgen::DIE CU1ToCU2RefAddrDie = CU1Die.addChild(DW_TAG_variable);
614 CU1ToCU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1ToCU2RefAddr");
615 CU1ToCU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie);
617 StringRef FileBytes = DG->generate();
618 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
619 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
620 EXPECT_TRUE((bool)Obj);
621 DWARFContextInMemory DwarfContext(*Obj.get());
623 // Verify the number of compile units is correct.
624 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
625 EXPECT_EQ(NumCUs, 2u);
626 DWARFCompileUnit *U1 = DwarfContext.getCompileUnitAtIndex(0);
627 DWARFCompileUnit *U2 = DwarfContext.getCompileUnitAtIndex(1);
629 // Get the compile unit DIE is valid.
630 auto Unit1DieDG = U1->getUnitDIE(false);
631 EXPECT_TRUE(Unit1DieDG.isValid());
632 // Unit1DieDG.dump(llvm::outs(), UINT32_MAX);
634 auto Unit2DieDG = U2->getUnitDIE(false);
635 EXPECT_TRUE(Unit2DieDG.isValid());
636 // Unit2DieDG.dump(llvm::outs(), UINT32_MAX);
638 // Verify the first child of the compile unit 1 DIE is our int base type.
639 auto CU1TypeDieDG = Unit1DieDG.getFirstChild();
640 EXPECT_TRUE(CU1TypeDieDG.isValid());
641 EXPECT_EQ(CU1TypeDieDG.getTag(), DW_TAG_base_type);
643 CU1TypeDieDG.getAttributeValueAsUnsignedConstant(DW_AT_encoding, 0),
646 // Verify the first child of the compile unit 2 DIE is our float base type.
647 auto CU2TypeDieDG = Unit2DieDG.getFirstChild();
648 EXPECT_TRUE(CU2TypeDieDG.isValid());
649 EXPECT_EQ(CU2TypeDieDG.getTag(), DW_TAG_base_type);
651 CU2TypeDieDG.getAttributeValueAsUnsignedConstant(DW_AT_encoding, 0),
654 // Verify the sibling of the base type DIE is our Ref1 DIE and that its
655 // DW_AT_type points to our base type DIE.
656 auto CU1Ref1DieDG = CU1TypeDieDG.getSibling();
657 EXPECT_TRUE(CU1Ref1DieDG.isValid());
658 EXPECT_EQ(CU1Ref1DieDG.getTag(), DW_TAG_variable);
659 EXPECT_EQ(CU1Ref1DieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
660 CU1TypeDieDG.getOffset());
661 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our
662 // base type DIE in CU1.
663 auto CU1Ref2DieDG = CU1Ref1DieDG.getSibling();
664 EXPECT_TRUE(CU1Ref2DieDG.isValid());
665 EXPECT_EQ(CU1Ref2DieDG.getTag(), DW_TAG_variable);
666 EXPECT_EQ(CU1Ref2DieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
667 CU1TypeDieDG.getOffset());
669 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our
670 // base type DIE in CU1.
671 auto CU1Ref4DieDG = CU1Ref2DieDG.getSibling();
672 EXPECT_TRUE(CU1Ref4DieDG.isValid());
673 EXPECT_EQ(CU1Ref4DieDG.getTag(), DW_TAG_variable);
674 EXPECT_EQ(CU1Ref4DieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
675 CU1TypeDieDG.getOffset());
677 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our
678 // base type DIE in CU1.
679 auto CU1Ref8DieDG = CU1Ref4DieDG.getSibling();
680 EXPECT_TRUE(CU1Ref8DieDG.isValid());
681 EXPECT_EQ(CU1Ref8DieDG.getTag(), DW_TAG_variable);
682 EXPECT_EQ(CU1Ref8DieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
683 CU1TypeDieDG.getOffset());
685 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our
686 // base type DIE in CU1.
687 auto CU1RefAddrDieDG = CU1Ref8DieDG.getSibling();
688 EXPECT_TRUE(CU1RefAddrDieDG.isValid());
689 EXPECT_EQ(CU1RefAddrDieDG.getTag(), DW_TAG_variable);
691 CU1RefAddrDieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
692 CU1TypeDieDG.getOffset());
694 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its
695 // DW_AT_type points to our base type DIE.
696 auto CU1ToCU2RefAddrDieDG = CU1RefAddrDieDG.getSibling();
697 EXPECT_TRUE(CU1ToCU2RefAddrDieDG.isValid());
698 EXPECT_EQ(CU1ToCU2RefAddrDieDG.getTag(), DW_TAG_variable);
699 EXPECT_EQ(CU1ToCU2RefAddrDieDG.getAttributeValueAsReference(DW_AT_type,
701 CU2TypeDieDG.getOffset());
703 // Verify the sibling of the base type DIE is our Ref1 DIE and that its
704 // DW_AT_type points to our base type DIE.
705 auto CU2Ref1DieDG = CU2TypeDieDG.getSibling();
706 EXPECT_TRUE(CU2Ref1DieDG.isValid());
707 EXPECT_EQ(CU2Ref1DieDG.getTag(), DW_TAG_variable);
708 EXPECT_EQ(CU2Ref1DieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
709 CU2TypeDieDG.getOffset());
710 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our
711 // base type DIE in CU2.
712 auto CU2Ref2DieDG = CU2Ref1DieDG.getSibling();
713 EXPECT_TRUE(CU2Ref2DieDG.isValid());
714 EXPECT_EQ(CU2Ref2DieDG.getTag(), DW_TAG_variable);
715 EXPECT_EQ(CU2Ref2DieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
716 CU2TypeDieDG.getOffset());
718 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our
719 // base type DIE in CU2.
720 auto CU2Ref4DieDG = CU2Ref2DieDG.getSibling();
721 EXPECT_TRUE(CU2Ref4DieDG.isValid());
722 EXPECT_EQ(CU2Ref4DieDG.getTag(), DW_TAG_variable);
723 EXPECT_EQ(CU2Ref4DieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
724 CU2TypeDieDG.getOffset());
726 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our
727 // base type DIE in CU2.
728 auto CU2Ref8DieDG = CU2Ref4DieDG.getSibling();
729 EXPECT_TRUE(CU2Ref8DieDG.isValid());
730 EXPECT_EQ(CU2Ref8DieDG.getTag(), DW_TAG_variable);
731 EXPECT_EQ(CU2Ref8DieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
732 CU2TypeDieDG.getOffset());
734 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our
735 // base type DIE in CU2.
736 auto CU2RefAddrDieDG = CU2Ref8DieDG.getSibling();
737 EXPECT_TRUE(CU2RefAddrDieDG.isValid());
738 EXPECT_EQ(CU2RefAddrDieDG.getTag(), DW_TAG_variable);
740 CU2RefAddrDieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
741 CU2TypeDieDG.getOffset());
743 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its
744 // DW_AT_type points to our base type DIE.
745 auto CU2ToCU1RefAddrDieDG = CU2RefAddrDieDG.getSibling();
746 EXPECT_TRUE(CU2ToCU1RefAddrDieDG.isValid());
747 EXPECT_EQ(CU2ToCU1RefAddrDieDG.getTag(), DW_TAG_variable);
748 EXPECT_EQ(CU2ToCU1RefAddrDieDG.getAttributeValueAsReference(DW_AT_type,
750 CU1TypeDieDG.getOffset());
753 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4References) {
754 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
756 typedef uint32_t AddrType;
757 TestReferences<2, AddrType>();
760 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8References) {
761 // Test that we can decode all forms for DWARF32, version 2, with 8 byte
763 typedef uint64_t AddrType;
764 TestReferences<2, AddrType>();
767 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4References) {
768 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
770 typedef uint32_t AddrType;
771 TestReferences<3, AddrType>();
774 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8References) {
775 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
777 typedef uint64_t AddrType;
778 TestReferences<3, AddrType>();
781 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4References) {
782 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
784 typedef uint32_t AddrType;
785 TestReferences<4, AddrType>();
788 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8References) {
789 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
791 typedef uint64_t AddrType;
792 TestReferences<4, AddrType>();
795 template <uint16_t Version, class AddrType> void TestAddresses() {
796 // Test the DWARF APIs related to accessing the DW_AT_low_pc and
798 const uint8_t AddrSize = sizeof(AddrType);
799 const bool SupportsHighPCAsOffset = Version >= 4;
801 Triple Triple = getHostTripleForAddrSize(AddrSize);
802 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
803 if (HandleExpectedError(ExpectedDG))
805 dwarfgen::Generator *DG = ExpectedDG.get().get();
806 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
807 dwarfgen::DIE CUDie = CU.getUnitDIE();
809 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
810 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
812 // Create a subprogram DIE with no low or high PC.
813 dwarfgen::DIE SubprogramNoPC = CUDie.addChild(DW_TAG_subprogram);
814 SubprogramNoPC.addAttribute(DW_AT_name, DW_FORM_strp, "no_pc");
816 // Create a subprogram DIE with a low PC only.
817 dwarfgen::DIE SubprogramLowPC = CUDie.addChild(DW_TAG_subprogram);
818 SubprogramLowPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_pc");
819 const uint64_t ActualLowPC = 0x1000;
820 const uint64_t ActualHighPC = 0x2000;
821 const uint64_t ActualHighPCOffset = ActualHighPC - ActualLowPC;
822 SubprogramLowPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC);
824 // Create a subprogram DIE with a low and high PC.
825 dwarfgen::DIE SubprogramLowHighPC = CUDie.addChild(DW_TAG_subprogram);
826 SubprogramLowHighPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_high_pc");
827 SubprogramLowHighPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC);
828 // Encode the high PC as an offset from the low PC if supported.
829 if (SupportsHighPCAsOffset)
830 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_data4,
833 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_addr, ActualHighPC);
835 StringRef FileBytes = DG->generate();
836 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
837 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
838 EXPECT_TRUE((bool)Obj);
839 DWARFContextInMemory DwarfContext(*Obj.get());
841 // Verify the number of compile units is correct.
842 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
843 EXPECT_EQ(NumCUs, 1u);
844 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
846 // Get the compile unit DIE is valid.
847 auto DieDG = U->getUnitDIE(false);
848 EXPECT_TRUE(DieDG.isValid());
849 // DieDG.dump(llvm::outs(), U, UINT32_MAX);
851 uint64_t LowPC, HighPC;
852 Optional<uint64_t> OptU64;
853 // Verify the that our subprogram with no PC value fails appropriately when
854 // asked for any PC values.
855 auto SubprogramDieNoPC = DieDG.getFirstChild();
856 EXPECT_TRUE(SubprogramDieNoPC.isValid());
857 EXPECT_EQ(SubprogramDieNoPC.getTag(), DW_TAG_subprogram);
858 OptU64 = SubprogramDieNoPC.getAttributeValueAsAddress(DW_AT_low_pc);
859 EXPECT_FALSE((bool)OptU64);
860 OptU64 = SubprogramDieNoPC.getAttributeValueAsAddress(DW_AT_high_pc);
861 EXPECT_FALSE((bool)OptU64);
862 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC));
863 OptU64 = SubprogramDieNoPC.getAttributeValueAsAddress(DW_AT_high_pc);
864 EXPECT_FALSE((bool)OptU64);
865 OptU64 = SubprogramDieNoPC.getAttributeValueAsUnsignedConstant(DW_AT_high_pc);
866 EXPECT_FALSE((bool)OptU64);
867 OptU64 = SubprogramDieNoPC.getHighPC(ActualLowPC);
868 EXPECT_FALSE((bool)OptU64);
869 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC));
872 // Verify the that our subprogram with only a low PC value succeeds when
873 // we ask for the Low PC, but fails appropriately when asked for the high PC
874 // or both low and high PC values.
875 auto SubprogramDieLowPC = SubprogramDieNoPC.getSibling();
876 EXPECT_TRUE(SubprogramDieLowPC.isValid());
877 EXPECT_EQ(SubprogramDieLowPC.getTag(), DW_TAG_subprogram);
878 OptU64 = SubprogramDieLowPC.getAttributeValueAsAddress(DW_AT_low_pc);
879 EXPECT_TRUE((bool)OptU64);
880 EXPECT_EQ(OptU64.getValue(), ActualLowPC);
881 OptU64 = SubprogramDieLowPC.getAttributeValueAsAddress(DW_AT_high_pc);
882 EXPECT_FALSE((bool)OptU64);
883 OptU64 = SubprogramDieLowPC.getAttributeValueAsUnsignedConstant(DW_AT_high_pc);
884 EXPECT_FALSE((bool)OptU64);
885 OptU64 = SubprogramDieLowPC.getHighPC(ActualLowPC);
886 EXPECT_FALSE((bool)OptU64);
887 EXPECT_FALSE(SubprogramDieLowPC.getLowAndHighPC(LowPC, HighPC));
890 // Verify the that our subprogram with only a low PC value succeeds when
891 // we ask for the Low PC, but fails appropriately when asked for the high PC
892 // or both low and high PC values.
893 auto SubprogramDieLowHighPC = SubprogramDieLowPC.getSibling();
894 EXPECT_TRUE(SubprogramDieLowHighPC.isValid());
895 EXPECT_EQ(SubprogramDieLowHighPC.getTag(), DW_TAG_subprogram);
896 OptU64 = SubprogramDieLowHighPC.getAttributeValueAsAddress(DW_AT_low_pc);
897 EXPECT_TRUE((bool)OptU64);
898 EXPECT_EQ(OptU64.getValue(), ActualLowPC);
899 // Get the high PC as an address. This should succeed if the high PC was
900 // encoded as an address and fail if the high PC was encoded as an offset.
901 OptU64 = SubprogramDieLowHighPC.getAttributeValueAsAddress(DW_AT_high_pc);
902 if (SupportsHighPCAsOffset) {
903 EXPECT_FALSE((bool)OptU64);
905 EXPECT_TRUE((bool)OptU64);
906 EXPECT_EQ(OptU64.getValue(), ActualHighPC);
908 // Get the high PC as an unsigned constant. This should succeed if the high PC
909 // was encoded as an offset and fail if the high PC was encoded as an address.
910 OptU64 = SubprogramDieLowHighPC.getAttributeValueAsUnsignedConstant(
912 if (SupportsHighPCAsOffset) {
913 EXPECT_TRUE((bool)OptU64);
914 EXPECT_EQ(OptU64.getValue(), ActualHighPCOffset);
916 EXPECT_FALSE((bool)OptU64);
919 OptU64 = SubprogramDieLowHighPC.getHighPC(ActualLowPC);
920 EXPECT_TRUE((bool)OptU64);
921 EXPECT_EQ(OptU64.getValue(), ActualHighPC);
923 EXPECT_TRUE(SubprogramDieLowHighPC.getLowAndHighPC(LowPC, HighPC));
924 EXPECT_EQ(LowPC, ActualLowPC);
925 EXPECT_EQ(HighPC, ActualHighPC);
928 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Addresses) {
929 // Test that we can decode address values in DWARF32, version 2, with 4 byte
931 typedef uint32_t AddrType;
932 TestAddresses<2, AddrType>();
935 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Addresses) {
936 // Test that we can decode address values in DWARF32, version 2, with 8 byte
938 typedef uint64_t AddrType;
939 TestAddresses<2, AddrType>();
942 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Addresses) {
943 // Test that we can decode address values in DWARF32, version 3, with 4 byte
945 typedef uint32_t AddrType;
946 TestAddresses<3, AddrType>();
949 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Addresses) {
950 // Test that we can decode address values in DWARF32, version 3, with 8 byte
952 typedef uint64_t AddrType;
953 TestAddresses<3, AddrType>();
956 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Addresses) {
957 // Test that we can decode address values in DWARF32, version 4, with 4 byte
959 typedef uint32_t AddrType;
960 TestAddresses<4, AddrType>();
963 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Addresses) {
964 // Test that we can decode address values in DWARF32, version 4, with 8 byte
966 typedef uint64_t AddrType;
967 TestAddresses<4, AddrType>();
970 TEST(DWARFDebugInfo, TestRelations) {
971 // Test the DWARF APIs related to accessing the DW_AT_low_pc and
973 uint16_t Version = 4;
975 const uint8_t AddrSize = sizeof(void *);
977 Triple Triple = getHostTripleForAddrSize(AddrSize);
978 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
979 if (HandleExpectedError(ExpectedDG))
981 dwarfgen::Generator *DG = ExpectedDG.get().get();
982 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
984 enum class Tag: uint16_t {
985 A = dwarf::DW_TAG_lo_user,
994 // Scope to allow us to re-use the same DIE names
996 // Create DWARF tree that looks like:
1006 dwarfgen::DIE CUDie = CU.getUnitDIE();
1007 dwarfgen::DIE A = CUDie.addChild((dwarf::Tag)Tag::A);
1008 A.addChild((dwarf::Tag)Tag::B);
1009 dwarfgen::DIE C = A.addChild((dwarf::Tag)Tag::C);
1010 dwarfgen::DIE D = A.addChild((dwarf::Tag)Tag::D);
1011 C.addChild((dwarf::Tag)Tag::C1);
1012 C.addChild((dwarf::Tag)Tag::C2);
1013 D.addChild((dwarf::Tag)Tag::D1);
1016 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1017 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1018 EXPECT_TRUE((bool)Obj);
1019 DWARFContextInMemory DwarfContext(*Obj.get());
1021 // Verify the number of compile units is correct.
1022 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
1023 EXPECT_EQ(NumCUs, 1u);
1024 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
1026 // Get the compile unit DIE is valid.
1027 auto CUDie = U->getUnitDIE(false);
1028 EXPECT_TRUE(CUDie.isValid());
1029 // CUDie.dump(llvm::outs(), UINT32_MAX);
1031 // The compile unit doesn't have a parent or a sibling.
1032 auto ParentDie = CUDie.getParent();
1033 EXPECT_FALSE(ParentDie.isValid());
1034 auto SiblingDie = CUDie.getSibling();
1035 EXPECT_FALSE(SiblingDie.isValid());
1037 // Get the children of the compile unit
1038 auto A = CUDie.getFirstChild();
1039 auto B = A.getFirstChild();
1040 auto C = B.getSibling();
1041 auto D = C.getSibling();
1042 auto Null = D.getSibling();
1044 // Verify NULL Die is NULL and has no children or siblings
1045 EXPECT_TRUE(Null.isNULL());
1046 EXPECT_FALSE(Null.getSibling().isValid());
1047 EXPECT_FALSE(Null.getFirstChild().isValid());
1049 // Verify all children of the compile unit DIE are correct.
1050 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A);
1051 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B);
1052 EXPECT_EQ(C.getTag(), (dwarf::Tag)Tag::C);
1053 EXPECT_EQ(D.getTag(), (dwarf::Tag)Tag::D);
1055 // Verify who has children
1056 EXPECT_TRUE(A.hasChildren());
1057 EXPECT_FALSE(B.hasChildren());
1058 EXPECT_TRUE(C.hasChildren());
1059 EXPECT_TRUE(D.hasChildren());
1061 // Make sure the parent of all the children of the compile unit are the
1063 EXPECT_EQ(A.getParent(), CUDie);
1065 // Make sure the parent of all the children of A are the A.
1066 // B is the first child in A, so we need to verify we can get the previous
1067 // DIE as the parent.
1068 EXPECT_EQ(B.getParent(), A);
1069 // C is the second child in A, so we need to make sure we can backup across
1070 // other DIE (B) at the same level to get the correct parent.
1071 EXPECT_EQ(C.getParent(), A);
1072 // D is the third child of A. We need to verify we can backup across other DIE
1073 // (B and C) including DIE that have children (D) to get the correct parent.
1074 EXPECT_EQ(D.getParent(), A);
1076 // Verify that a DIE with no children returns an invalid DWARFDie.
1077 EXPECT_FALSE(B.getFirstChild().isValid());
1079 // Verify the children of the B DIE
1080 auto C1 = C.getFirstChild();
1081 auto C2 = C1.getSibling();
1082 EXPECT_TRUE(C2.getSibling().isNULL());
1084 // Verify all children of the B DIE correctly valid or invalid.
1085 EXPECT_EQ(C1.getTag(), (dwarf::Tag)Tag::C1);
1086 EXPECT_EQ(C2.getTag(), (dwarf::Tag)Tag::C2);
1088 // Make sure the parent of all the children of the B are the B.
1089 EXPECT_EQ(C1.getParent(), C);
1090 EXPECT_EQ(C2.getParent(), C);
1093 TEST(DWARFDebugInfo, TestDWARFDie) {
1095 // Make sure a default constructed DWARFDie doesn't have any parent, sibling
1097 DWARFDie DefaultDie;
1098 EXPECT_FALSE(DefaultDie.getParent().isValid());
1099 EXPECT_FALSE(DefaultDie.getFirstChild().isValid());
1100 EXPECT_FALSE(DefaultDie.getSibling().isValid());
1103 TEST(DWARFDebugInfo, TestChildIterators) {
1104 // Test the DWARF APIs related to iterating across the children of a DIE using
1105 // the DWARFDie::iterator class.
1106 uint16_t Version = 4;
1108 const uint8_t AddrSize = sizeof(void *);
1110 Triple Triple = getHostTripleForAddrSize(AddrSize);
1111 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1112 if (HandleExpectedError(ExpectedDG))
1114 dwarfgen::Generator *DG = ExpectedDG.get().get();
1115 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1117 enum class Tag: uint16_t {
1118 A = dwarf::DW_TAG_lo_user,
1122 // Scope to allow us to re-use the same DIE names
1124 // Create DWARF tree that looks like:
1129 auto CUDie = CU.getUnitDIE();
1130 CUDie.addChild((dwarf::Tag)Tag::A);
1131 CUDie.addChild((dwarf::Tag)Tag::B);
1134 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1135 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1136 EXPECT_TRUE((bool)Obj);
1137 DWARFContextInMemory DwarfContext(*Obj.get());
1139 // Verify the number of compile units is correct.
1140 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
1141 EXPECT_EQ(NumCUs, 1u);
1142 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
1144 // Get the compile unit DIE is valid.
1145 auto CUDie = U->getUnitDIE(false);
1146 EXPECT_TRUE(CUDie.isValid());
1147 // CUDie.dump(llvm::outs(), UINT32_MAX);
1152 // Verify the compile unit DIE's children.
1154 for (auto Die : CUDie.children()) {
1156 case 0: A = Die; break;
1157 case 1: B = Die; break;
1161 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A);
1162 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B);
1164 // Verify that A has no children by verifying that the begin and end contain
1165 // invalid DIEs and also that the iterators are equal.
1166 EXPECT_EQ(A.begin(), A.end());
1169 TEST(DWARFDebugInfo, TestChildIteratorsOnInvalidDie) {
1170 // Verify that an invalid DIE has no children.
1172 auto begin = Invalid.begin();
1173 auto end = Invalid.end();
1174 EXPECT_FALSE(begin->isValid());
1175 EXPECT_FALSE(end->isValid());
1176 EXPECT_EQ(begin, end);
1180 TEST(DWARFDebugInfo, TestEmptyChildren) {
1181 // Test a DIE that says it has children in the abbreviation, but actually
1182 // doesn't have any attributes, will not return anything during iteration.
1183 // We do this by making sure the begin and end iterators are equal.
1184 uint16_t Version = 4;
1186 const uint8_t AddrSize = sizeof(void *);
1188 Triple Triple = getHostTripleForAddrSize(AddrSize);
1189 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1190 if (HandleExpectedError(ExpectedDG))
1192 dwarfgen::Generator *DG = ExpectedDG.get().get();
1193 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1195 // Scope to allow us to re-use the same DIE names
1197 // Create a compile unit DIE that has an abbreviation that says it has
1198 // children, but doesn't have any actual attributes. This helps us test
1199 // a DIE that has only one child: a NULL DIE.
1200 auto CUDie = CU.getUnitDIE();
1201 CUDie.setForceChildren();
1204 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1205 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1206 EXPECT_TRUE((bool)Obj);
1207 DWARFContextInMemory DwarfContext(*Obj.get());
1209 // Verify the number of compile units is correct.
1210 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
1211 EXPECT_EQ(NumCUs, 1u);
1212 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
1214 // Get the compile unit DIE is valid.
1215 auto CUDie = U->getUnitDIE(false);
1216 EXPECT_TRUE(CUDie.isValid());
1217 CUDie.dump(llvm::outs(), UINT32_MAX);
1219 // Verify that the CU Die that says it has children, but doesn't, actually
1220 // has begin and end iterators that are equal. We want to make sure we don't
1221 // see the Null DIEs during iteration.
1222 EXPECT_EQ(CUDie.begin(), CUDie.end());
1225 } // end anonymous namespace