1 //===- llvm/unittest/DebugInfo/DWARFFormValueTest.cpp ---------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 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" 20 #include <climits> 21 22 using namespace llvm; 23 using namespace dwarf; 24 25 namespace { 26 27 void initLLVMIfNeeded() { 28 static bool gInitialized = false; 29 if (!gInitialized) { 30 gInitialized = true; 31 InitializeAllTargets(); 32 InitializeAllTargetMCs(); 33 InitializeAllAsmPrinters(); 34 InitializeAllAsmParsers(); 35 } 36 } 37 38 Triple getHostTripleForAddrSize(uint8_t AddrSize) { 39 Triple PT(Triple::normalize(LLVM_HOST_TRIPLE)); 40 41 if (AddrSize == 8 && PT.isArch32Bit()) 42 return PT.get64BitArchVariant(); 43 if (AddrSize == 4 && PT.isArch64Bit()) 44 return PT.get32BitArchVariant(); 45 return PT; 46 } 47 48 /// Take any llvm::Expected and check and handle any errors. 49 /// 50 /// \param Expected a llvm::Excepted instance to check. 51 /// \returns true if there were errors, false otherwise. 52 template <typename T> 53 static bool HandleExpectedError(T &Expected) { 54 std::string ErrorMsg; 55 handleAllErrors(Expected.takeError(), [&](const llvm::ErrorInfoBase &EI) { 56 ErrorMsg = EI.message(); 57 }); 58 if (!ErrorMsg.empty()) { 59 ::testing::AssertionFailure() << "error: " << ErrorMsg; 60 return true; 61 } 62 return false; 63 } 64 65 template <uint16_t Version, class AddrType, class RefAddrType> 66 void TestAllForms() { 67 // Test that we can decode all DW_FORM values correctly. 68 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"; 87 initLLVMIfNeeded(); 88 Triple Triple = getHostTripleForAddrSize(AddrSize); 89 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 90 if (HandleExpectedError(ExpectedDG)) 91 return; 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; 96 97 //---------------------------------------------------------------------- 98 // Test address forms 99 //---------------------------------------------------------------------- 100 const auto Attr_DW_FORM_addr = static_cast<dwarf::Attribute>(Attr++); 101 CUDie.addAttribute(Attr_DW_FORM_addr, DW_FORM_addr, AddrValue); 102 103 //---------------------------------------------------------------------- 104 // Test block forms 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); 108 109 const auto Attr_DW_FORM_block1 = static_cast<dwarf::Attribute>(Attr++); 110 CUDie.addAttribute(Attr_DW_FORM_block1, DW_FORM_block1, BlockData, BlockSize); 111 112 const auto Attr_DW_FORM_block2 = static_cast<dwarf::Attribute>(Attr++); 113 CUDie.addAttribute(Attr_DW_FORM_block2, DW_FORM_block2, BlockData, BlockSize); 114 115 const auto Attr_DW_FORM_block4 = static_cast<dwarf::Attribute>(Attr++); 116 CUDie.addAttribute(Attr_DW_FORM_block4, DW_FORM_block4, BlockData, BlockSize); 117 118 //---------------------------------------------------------------------- 119 // Test data forms 120 //---------------------------------------------------------------------- 121 const auto Attr_DW_FORM_data1 = static_cast<dwarf::Attribute>(Attr++); 122 CUDie.addAttribute(Attr_DW_FORM_data1, DW_FORM_data1, Data1); 123 124 const auto Attr_DW_FORM_data2 = static_cast<dwarf::Attribute>(Attr++); 125 CUDie.addAttribute(Attr_DW_FORM_data2, DW_FORM_data2, Data2); 126 127 const auto Attr_DW_FORM_data4 = static_cast<dwarf::Attribute>(Attr++); 128 CUDie.addAttribute(Attr_DW_FORM_data4, DW_FORM_data4, Data4); 129 130 const auto Attr_DW_FORM_data8 = static_cast<dwarf::Attribute>(Attr++); 131 CUDie.addAttribute(Attr_DW_FORM_data8, DW_FORM_data8, Data8); 132 133 //---------------------------------------------------------------------- 134 // Test string forms 135 //---------------------------------------------------------------------- 136 const auto Attr_DW_FORM_string = static_cast<dwarf::Attribute>(Attr++); 137 CUDie.addAttribute(Attr_DW_FORM_string, DW_FORM_string, StringValue); 138 139 const auto Attr_DW_FORM_strp = static_cast<dwarf::Attribute>(Attr++); 140 CUDie.addAttribute(Attr_DW_FORM_strp, DW_FORM_strp, StrpValue); 141 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); 147 148 const auto Attr_DW_FORM_ref1 = static_cast<dwarf::Attribute>(Attr++); 149 CUDie.addAttribute(Attr_DW_FORM_ref1, DW_FORM_ref1, Data1); 150 151 const auto Attr_DW_FORM_ref2 = static_cast<dwarf::Attribute>(Attr++); 152 CUDie.addAttribute(Attr_DW_FORM_ref2, DW_FORM_ref2, Data2); 153 154 const auto Attr_DW_FORM_ref4 = static_cast<dwarf::Attribute>(Attr++); 155 CUDie.addAttribute(Attr_DW_FORM_ref4, DW_FORM_ref4, Data4); 156 157 const auto Attr_DW_FORM_ref8 = static_cast<dwarf::Attribute>(Attr++); 158 CUDie.addAttribute(Attr_DW_FORM_ref8, DW_FORM_ref8, Data8); 159 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); 162 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]); 165 166 //---------------------------------------------------------------------- 167 // Test flag forms 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); 171 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); 174 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); 177 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); 183 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]); 189 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, 195 Dwarf32Values[0]); 196 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, 199 Dwarf32Values[1]); 200 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); 206 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()); 220 221 //---------------------------------------------------------------------- 222 // Test address forms 223 //---------------------------------------------------------------------- 224 EXPECT_EQ(DieDG.getAttributeValueAsAddress(Attr_DW_FORM_addr, 0), 225 AddrValue); 226 227 //---------------------------------------------------------------------- 228 // Test block forms 229 //---------------------------------------------------------------------- 230 Optional<DWARFFormValue> FormValue; 231 ArrayRef<uint8_t> ExtractedBlockData; 232 Optional<ArrayRef<uint8_t>> BlockDataOpt; 233 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); 241 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); 249 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); 257 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); 265 266 //---------------------------------------------------------------------- 267 // Test data forms 268 //---------------------------------------------------------------------- 269 EXPECT_EQ( 270 DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data1, 0), 271 Data1); 272 EXPECT_EQ( 273 DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data2, 0), 274 Data2); 275 EXPECT_EQ( 276 DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data4, 0), 277 Data4); 278 EXPECT_EQ( 279 DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data8, 0), 280 Data8); 281 282 //---------------------------------------------------------------------- 283 // Test string forms 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); 289 290 const char *ExtractedStrpValue = 291 DieDG.getAttributeValueAsString(Attr_DW_FORM_strp, nullptr); 292 EXPECT_TRUE(ExtractedStrpValue != nullptr); 293 EXPECT_TRUE(strcmp(StrpValue, ExtractedStrpValue) == 0); 294 295 //---------------------------------------------------------------------- 296 // Test reference forms 297 //---------------------------------------------------------------------- 298 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_addr, 0), 299 RefAddr); 300 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref1, 0), 301 Data1); 302 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref2, 0), 303 Data2); 304 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref4, 0), 305 Data4); 306 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref8, 0), 307 Data8); 308 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_sig8, 0), 309 Data8_2); 310 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_udata, 0), 311 UData[0]); 312 313 //---------------------------------------------------------------------- 314 // Test flag forms 315 //---------------------------------------------------------------------- 316 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant( 317 Attr_DW_FORM_flag_true, 0ULL), 318 1ULL); 319 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant( 320 Attr_DW_FORM_flag_false, 1ULL), 321 0ULL); 322 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant( 323 Attr_DW_FORM_flag_present, 0ULL), 324 1ULL); 325 326 // TODO: test Attr_DW_FORM_implicit_const extraction 327 328 //---------------------------------------------------------------------- 329 // Test SLEB128 based forms 330 //---------------------------------------------------------------------- 331 EXPECT_EQ(DieDG.getAttributeValueAsSignedConstant(Attr_DW_FORM_sdata, 0), 332 SData); 333 334 //---------------------------------------------------------------------- 335 // Test ULEB128 based forms 336 //---------------------------------------------------------------------- 337 EXPECT_EQ( 338 DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_udata, 0), 339 UData[0]); 340 341 //---------------------------------------------------------------------- 342 // Test DWARF32/DWARF64 forms 343 //---------------------------------------------------------------------- 344 EXPECT_EQ( 345 DieDG.getAttributeValueAsReference(Attr_DW_FORM_GNU_ref_alt, 0), 346 Dwarf32Values[0]); 347 EXPECT_EQ( 348 DieDG.getAttributeValueAsSectionOffset(Attr_DW_FORM_sec_offset, 0), 349 Dwarf32Values[1]); 350 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); 355 } 356 357 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4AllForms) { 358 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 359 // addresses. 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>(); 364 } 365 366 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8AllForms) { 367 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 368 // addresses. 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>(); 373 } 374 375 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4AllForms) { 376 // Test that we can decode all forms for DWARF32, version 3, with 4 byte 377 // addresses. 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>(); 382 } 383 384 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8AllForms) { 385 // Test that we can decode all forms for DWARF32, version 3, with 8 byte 386 // addresses. 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>(); 391 } 392 393 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4AllForms) { 394 // Test that we can decode all forms for DWARF32, version 4, with 4 byte 395 // addresses. 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>(); 400 } 401 402 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8AllForms) { 403 // Test that we can decode all forms for DWARF32, version 4, with 8 byte 404 // addresses. 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>(); 409 } 410 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 414 // 8 byte addresses. 415 416 const uint8_t AddrSize = sizeof(AddrType); 417 initLLVMIfNeeded(); 418 Triple Triple = getHostTripleForAddrSize(AddrSize); 419 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 420 if (HandleExpectedError(ExpectedDG)) 421 return; 422 dwarfgen::Generator *DG = ExpectedDG.get().get(); 423 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 424 dwarfgen::DIE CUDie = CU.getUnitDIE(); 425 426 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c"); 427 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 428 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); 433 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); 438 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); 443 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()); 449 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); 454 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); 459 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); 464 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); 469 470 // Verify our formal parameter has a NULL tag sibling. 471 auto NullDieDG = ArgcDieDG.getSibling(); 472 EXPECT_TRUE(NullDieDG.isValid()); 473 if (NullDieDG) { 474 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null); 475 EXPECT_TRUE(!NullDieDG.getSibling().isValid()); 476 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid()); 477 } 478 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); 483 484 // Verify the sibling of our subprogram is our integer base is a NULL tag. 485 NullDieDG = IntDieDG.getSibling(); 486 EXPECT_TRUE(NullDieDG.isValid()); 487 if (NullDieDG) { 488 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null); 489 EXPECT_TRUE(!NullDieDG.getSibling().isValid()); 490 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid()); 491 } 492 } 493 494 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Children) { 495 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 496 // addresses. 497 typedef uint32_t AddrType; 498 TestChildren<2, AddrType>(); 499 } 500 501 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Children) { 502 // Test that we can decode all forms for DWARF32, version 2, with 8 byte 503 // addresses. 504 typedef uint64_t AddrType; 505 TestChildren<2, AddrType>(); 506 } 507 508 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Children) { 509 // Test that we can decode all forms for DWARF32, version 3, with 4 byte 510 // addresses. 511 typedef uint32_t AddrType; 512 TestChildren<3, AddrType>(); 513 } 514 515 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Children) { 516 // Test that we can decode all forms for DWARF32, version 3, with 8 byte 517 // addresses. 518 typedef uint64_t AddrType; 519 TestChildren<3, AddrType>(); 520 } 521 522 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Children) { 523 // Test that we can decode all forms for DWARF32, version 4, with 4 byte 524 // addresses. 525 typedef uint32_t AddrType; 526 TestChildren<4, AddrType>(); 527 } 528 529 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Children) { 530 // Test that we can decode all forms for DWARF32, version 4, with 8 byte 531 // addresses. 532 typedef uint64_t AddrType; 533 TestChildren<4, AddrType>(); 534 } 535 536 template <uint16_t Version, class AddrType> void TestReferences() { 537 // Test that we can decode DW_FORM_refXXX values correctly in DWARF. 538 539 const uint8_t AddrSize = sizeof(AddrType); 540 initLLVMIfNeeded(); 541 Triple Triple = getHostTripleForAddrSize(AddrSize); 542 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 543 if (HandleExpectedError(ExpectedDG)) 544 return; 545 dwarfgen::Generator *DG = ExpectedDG.get().get(); 546 dwarfgen::CompileUnit &CU1 = DG->addCompileUnit(); 547 dwarfgen::CompileUnit &CU2 = DG->addCompileUnit(); 548 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); 552 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); 557 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); 561 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); 565 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); 569 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); 573 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); 577 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); 581 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); 586 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); 590 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); 594 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); 598 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); 602 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); 606 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); 611 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); 616 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()); 622 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); 628 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); 633 634 auto Unit2DieDG = U2->getUnitDIE(false); 635 EXPECT_TRUE(Unit2DieDG.isValid()); 636 // Unit2DieDG.dump(llvm::outs(), UINT32_MAX); 637 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); 642 EXPECT_EQ( 643 CU1TypeDieDG.getAttributeValueAsUnsignedConstant(DW_AT_encoding, 0), 644 DW_ATE_signed); 645 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); 650 EXPECT_EQ( 651 CU2TypeDieDG.getAttributeValueAsUnsignedConstant(DW_AT_encoding, 0), 652 DW_ATE_float); 653 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()); 668 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()); 676 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()); 684 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); 690 EXPECT_EQ( 691 CU1RefAddrDieDG.getAttributeValueAsReference(DW_AT_type, -1ULL), 692 CU1TypeDieDG.getOffset()); 693 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, 700 -1ULL), 701 CU2TypeDieDG.getOffset()); 702 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()); 717 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()); 725 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()); 733 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); 739 EXPECT_EQ( 740 CU2RefAddrDieDG.getAttributeValueAsReference(DW_AT_type, -1ULL), 741 CU2TypeDieDG.getOffset()); 742 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, 749 -1ULL), 750 CU1TypeDieDG.getOffset()); 751 } 752 753 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4References) { 754 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 755 // addresses. 756 typedef uint32_t AddrType; 757 TestReferences<2, AddrType>(); 758 } 759 760 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8References) { 761 // Test that we can decode all forms for DWARF32, version 2, with 8 byte 762 // addresses. 763 typedef uint64_t AddrType; 764 TestReferences<2, AddrType>(); 765 } 766 767 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4References) { 768 // Test that we can decode all forms for DWARF32, version 3, with 4 byte 769 // addresses. 770 typedef uint32_t AddrType; 771 TestReferences<3, AddrType>(); 772 } 773 774 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8References) { 775 // Test that we can decode all forms for DWARF32, version 3, with 8 byte 776 // addresses. 777 typedef uint64_t AddrType; 778 TestReferences<3, AddrType>(); 779 } 780 781 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4References) { 782 // Test that we can decode all forms for DWARF32, version 4, with 4 byte 783 // addresses. 784 typedef uint32_t AddrType; 785 TestReferences<4, AddrType>(); 786 } 787 788 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8References) { 789 // Test that we can decode all forms for DWARF32, version 4, with 8 byte 790 // addresses. 791 typedef uint64_t AddrType; 792 TestReferences<4, AddrType>(); 793 } 794 795 template <uint16_t Version, class AddrType> void TestAddresses() { 796 // Test the DWARF APIs related to accessing the DW_AT_low_pc and 797 // DW_AT_high_pc. 798 const uint8_t AddrSize = sizeof(AddrType); 799 const bool SupportsHighPCAsOffset = Version >= 4; 800 initLLVMIfNeeded(); 801 Triple Triple = getHostTripleForAddrSize(AddrSize); 802 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 803 if (HandleExpectedError(ExpectedDG)) 804 return; 805 dwarfgen::Generator *DG = ExpectedDG.get().get(); 806 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 807 dwarfgen::DIE CUDie = CU.getUnitDIE(); 808 809 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c"); 810 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 811 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"); 815 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); 823 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, 831 ActualHighPCOffset); 832 else 833 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_addr, ActualHighPC); 834 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()); 840 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); 845 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); 850 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)); 870 871 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)); 888 889 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); 904 } else { 905 EXPECT_TRUE((bool)OptU64); 906 EXPECT_EQ(OptU64.getValue(), ActualHighPC); 907 } 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( 911 DW_AT_high_pc); 912 if (SupportsHighPCAsOffset) { 913 EXPECT_TRUE((bool)OptU64); 914 EXPECT_EQ(OptU64.getValue(), ActualHighPCOffset); 915 } else { 916 EXPECT_FALSE((bool)OptU64); 917 } 918 919 OptU64 = SubprogramDieLowHighPC.getHighPC(ActualLowPC); 920 EXPECT_TRUE((bool)OptU64); 921 EXPECT_EQ(OptU64.getValue(), ActualHighPC); 922 923 EXPECT_TRUE(SubprogramDieLowHighPC.getLowAndHighPC(LowPC, HighPC)); 924 EXPECT_EQ(LowPC, ActualLowPC); 925 EXPECT_EQ(HighPC, ActualHighPC); 926 } 927 928 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Addresses) { 929 // Test that we can decode address values in DWARF32, version 2, with 4 byte 930 // addresses. 931 typedef uint32_t AddrType; 932 TestAddresses<2, AddrType>(); 933 } 934 935 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Addresses) { 936 // Test that we can decode address values in DWARF32, version 2, with 8 byte 937 // addresses. 938 typedef uint64_t AddrType; 939 TestAddresses<2, AddrType>(); 940 } 941 942 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Addresses) { 943 // Test that we can decode address values in DWARF32, version 3, with 4 byte 944 // addresses. 945 typedef uint32_t AddrType; 946 TestAddresses<3, AddrType>(); 947 } 948 949 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Addresses) { 950 // Test that we can decode address values in DWARF32, version 3, with 8 byte 951 // addresses. 952 typedef uint64_t AddrType; 953 TestAddresses<3, AddrType>(); 954 } 955 956 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Addresses) { 957 // Test that we can decode address values in DWARF32, version 4, with 4 byte 958 // addresses. 959 typedef uint32_t AddrType; 960 TestAddresses<4, AddrType>(); 961 } 962 963 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Addresses) { 964 // Test that we can decode address values in DWARF32, version 4, with 8 byte 965 // addresses. 966 typedef uint64_t AddrType; 967 TestAddresses<4, AddrType>(); 968 } 969 970 TEST(DWARFDebugInfo, TestRelations) { 971 // Test the DWARF APIs related to accessing the DW_AT_low_pc and 972 // DW_AT_high_pc. 973 uint16_t Version = 4; 974 975 const uint8_t AddrSize = sizeof(void *); 976 initLLVMIfNeeded(); 977 Triple Triple = getHostTripleForAddrSize(AddrSize); 978 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 979 if (HandleExpectedError(ExpectedDG)) 980 return; 981 dwarfgen::Generator *DG = ExpectedDG.get().get(); 982 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 983 984 enum class Tag: uint16_t { 985 A = dwarf::DW_TAG_lo_user, 986 B, 987 B1, 988 B2, 989 C, 990 C1 991 }; 992 993 // Scope to allow us to re-use the same DIE names 994 { 995 // Create DWARF tree that looks like: 996 // 997 // CU 998 // A 999 // B 1000 // B1 1001 // B2 1002 // C 1003 // C1 1004 dwarfgen::DIE CUDie = CU.getUnitDIE(); 1005 CUDie.addChild((dwarf::Tag)Tag::A); 1006 dwarfgen::DIE B = CUDie.addChild((dwarf::Tag)Tag::B); 1007 dwarfgen::DIE C = CUDie.addChild((dwarf::Tag)Tag::C); 1008 B.addChild((dwarf::Tag)Tag::B1); 1009 B.addChild((dwarf::Tag)Tag::B2); 1010 C.addChild((dwarf::Tag)Tag::C1); 1011 } 1012 1013 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1014 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1015 EXPECT_TRUE((bool)Obj); 1016 DWARFContextInMemory DwarfContext(*Obj.get()); 1017 1018 // Verify the number of compile units is correct. 1019 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 1020 EXPECT_EQ(NumCUs, 1u); 1021 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1022 1023 // Get the compile unit DIE is valid. 1024 auto CUDie = U->getUnitDIE(false); 1025 EXPECT_TRUE(CUDie.isValid()); 1026 // DieDG.dump(llvm::outs(), U, UINT32_MAX); 1027 1028 // The compile unit doesn't have a parent or a sibling. 1029 auto ParentDie = CUDie.getParent(); 1030 EXPECT_FALSE(ParentDie.isValid()); 1031 auto SiblingDie = CUDie.getSibling(); 1032 EXPECT_FALSE(SiblingDie.isValid()); 1033 1034 // Get the children of the compile unit 1035 auto A = CUDie.getFirstChild(); 1036 auto B = A.getSibling(); 1037 auto C = B.getSibling(); 1038 auto Null = C.getSibling(); 1039 1040 // Verify NULL Die is NULL and has no children or siblings 1041 EXPECT_TRUE(Null.isNULL()); 1042 EXPECT_FALSE(Null.getSibling().isValid()); 1043 EXPECT_FALSE(Null.getFirstChild().isValid()); 1044 1045 // Verify all children of the compile unit DIE are correct. 1046 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A); 1047 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B); 1048 EXPECT_EQ(C.getTag(), (dwarf::Tag)Tag::C); 1049 1050 // Verify who has children 1051 EXPECT_FALSE(A.hasChildren()); 1052 EXPECT_TRUE(B.hasChildren()); 1053 1054 // Make sure the parent of all the children of the compile unit are the 1055 // compile unit. 1056 EXPECT_EQ(A.getParent(), CUDie); 1057 EXPECT_EQ(B.getParent(), CUDie); 1058 EXPECT_EQ(Null.getParent(), CUDie); 1059 1060 EXPECT_FALSE(A.getFirstChild().isValid()); 1061 1062 // Verify the children of the B DIE 1063 auto B1 = B.getFirstChild(); 1064 auto B2 = B1.getSibling(); 1065 EXPECT_TRUE(B2.getSibling().isNULL()); 1066 1067 // Verify all children of the B DIE correctly valid or invalid. 1068 EXPECT_EQ(B1.getTag(), (dwarf::Tag)Tag::B1); 1069 EXPECT_EQ(B2.getTag(), (dwarf::Tag)Tag::B2); 1070 1071 // Make sure the parent of all the children of the B are the B. 1072 EXPECT_EQ(B1.getParent(), B); 1073 EXPECT_EQ(B2.getParent(), B); 1074 } 1075 1076 TEST(DWARFDebugInfo, TestDWARFDie) { 1077 1078 // Make sure a default constructed DWARFDie doesn't have any parent, sibling 1079 // or child; 1080 DWARFDie DefaultDie; 1081 EXPECT_FALSE(DefaultDie.getParent().isValid()); 1082 EXPECT_FALSE(DefaultDie.getFirstChild().isValid()); 1083 EXPECT_FALSE(DefaultDie.getSibling().isValid()); 1084 } 1085 1086 1087 } // end anonymous namespace 1088