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 int64_t ICSData = INT64_MAX; // DW_FORM_implicit_const SData 81 const uint64_t UData[] = {UINT64_MAX - 1, UINT64_MAX - 2, UINT64_MAX - 3, 82 UINT64_MAX - 4, UINT64_MAX - 5, UINT64_MAX - 6, 83 UINT64_MAX - 7, UINT64_MAX - 8, UINT64_MAX - 9}; 84 #define UDATA_1 18446744073709551614ULL 85 const uint32_t Dwarf32Values[] = {1, 2, 3, 4, 5, 6, 7, 8}; 86 const char *StringValue = "Hello"; 87 const char *StrpValue = "World"; 88 initLLVMIfNeeded(); 89 Triple Triple = getHostTripleForAddrSize(AddrSize); 90 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 91 if (HandleExpectedError(ExpectedDG)) 92 return; 93 dwarfgen::Generator *DG = ExpectedDG.get().get(); 94 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 95 dwarfgen::DIE CUDie = CU.getUnitDIE(); 96 uint16_t Attr = DW_AT_lo_user; 97 98 //---------------------------------------------------------------------- 99 // Test address forms 100 //---------------------------------------------------------------------- 101 const auto Attr_DW_FORM_addr = static_cast<dwarf::Attribute>(Attr++); 102 CUDie.addAttribute(Attr_DW_FORM_addr, DW_FORM_addr, AddrValue); 103 104 //---------------------------------------------------------------------- 105 // Test block forms 106 //---------------------------------------------------------------------- 107 const auto Attr_DW_FORM_block = static_cast<dwarf::Attribute>(Attr++); 108 CUDie.addAttribute(Attr_DW_FORM_block, DW_FORM_block, BlockData, BlockSize); 109 110 const auto Attr_DW_FORM_block1 = static_cast<dwarf::Attribute>(Attr++); 111 CUDie.addAttribute(Attr_DW_FORM_block1, DW_FORM_block1, BlockData, BlockSize); 112 113 const auto Attr_DW_FORM_block2 = static_cast<dwarf::Attribute>(Attr++); 114 CUDie.addAttribute(Attr_DW_FORM_block2, DW_FORM_block2, BlockData, BlockSize); 115 116 const auto Attr_DW_FORM_block4 = static_cast<dwarf::Attribute>(Attr++); 117 CUDie.addAttribute(Attr_DW_FORM_block4, DW_FORM_block4, BlockData, BlockSize); 118 119 //---------------------------------------------------------------------- 120 // Test data forms 121 //---------------------------------------------------------------------- 122 const auto Attr_DW_FORM_data1 = static_cast<dwarf::Attribute>(Attr++); 123 CUDie.addAttribute(Attr_DW_FORM_data1, DW_FORM_data1, Data1); 124 125 const auto Attr_DW_FORM_data2 = static_cast<dwarf::Attribute>(Attr++); 126 CUDie.addAttribute(Attr_DW_FORM_data2, DW_FORM_data2, Data2); 127 128 const auto Attr_DW_FORM_data4 = static_cast<dwarf::Attribute>(Attr++); 129 CUDie.addAttribute(Attr_DW_FORM_data4, DW_FORM_data4, Data4); 130 131 const auto Attr_DW_FORM_data8 = static_cast<dwarf::Attribute>(Attr++); 132 CUDie.addAttribute(Attr_DW_FORM_data8, DW_FORM_data8, Data8); 133 134 //---------------------------------------------------------------------- 135 // Test string forms 136 //---------------------------------------------------------------------- 137 const auto Attr_DW_FORM_string = static_cast<dwarf::Attribute>(Attr++); 138 CUDie.addAttribute(Attr_DW_FORM_string, DW_FORM_string, StringValue); 139 140 const auto Attr_DW_FORM_strp = static_cast<dwarf::Attribute>(Attr++); 141 CUDie.addAttribute(Attr_DW_FORM_strp, DW_FORM_strp, StrpValue); 142 143 //---------------------------------------------------------------------- 144 // Test reference forms 145 //---------------------------------------------------------------------- 146 const auto Attr_DW_FORM_ref_addr = static_cast<dwarf::Attribute>(Attr++); 147 CUDie.addAttribute(Attr_DW_FORM_ref_addr, DW_FORM_ref_addr, RefAddr); 148 149 const auto Attr_DW_FORM_ref1 = static_cast<dwarf::Attribute>(Attr++); 150 CUDie.addAttribute(Attr_DW_FORM_ref1, DW_FORM_ref1, Data1); 151 152 const auto Attr_DW_FORM_ref2 = static_cast<dwarf::Attribute>(Attr++); 153 CUDie.addAttribute(Attr_DW_FORM_ref2, DW_FORM_ref2, Data2); 154 155 const auto Attr_DW_FORM_ref4 = static_cast<dwarf::Attribute>(Attr++); 156 CUDie.addAttribute(Attr_DW_FORM_ref4, DW_FORM_ref4, Data4); 157 158 const auto Attr_DW_FORM_ref8 = static_cast<dwarf::Attribute>(Attr++); 159 CUDie.addAttribute(Attr_DW_FORM_ref8, DW_FORM_ref8, Data8); 160 161 const auto Attr_DW_FORM_ref_sig8 = static_cast<dwarf::Attribute>(Attr++); 162 CUDie.addAttribute(Attr_DW_FORM_ref_sig8, DW_FORM_ref_sig8, Data8_2); 163 164 const auto Attr_DW_FORM_ref_udata = static_cast<dwarf::Attribute>(Attr++); 165 CUDie.addAttribute(Attr_DW_FORM_ref_udata, DW_FORM_ref_udata, UData[0]); 166 167 //---------------------------------------------------------------------- 168 // Test flag forms 169 //---------------------------------------------------------------------- 170 const auto Attr_DW_FORM_flag_true = static_cast<dwarf::Attribute>(Attr++); 171 CUDie.addAttribute(Attr_DW_FORM_flag_true, DW_FORM_flag, true); 172 173 const auto Attr_DW_FORM_flag_false = static_cast<dwarf::Attribute>(Attr++); 174 CUDie.addAttribute(Attr_DW_FORM_flag_false, DW_FORM_flag, false); 175 176 const auto Attr_DW_FORM_flag_present = static_cast<dwarf::Attribute>(Attr++); 177 CUDie.addAttribute(Attr_DW_FORM_flag_present, DW_FORM_flag_present); 178 179 //---------------------------------------------------------------------- 180 // Test SLEB128 based forms 181 //---------------------------------------------------------------------- 182 const auto Attr_DW_FORM_sdata = static_cast<dwarf::Attribute>(Attr++); 183 CUDie.addAttribute(Attr_DW_FORM_sdata, DW_FORM_sdata, SData); 184 185 const auto Attr_DW_FORM_implicit_const = 186 static_cast<dwarf::Attribute>(Attr++); 187 if (Version >= 5) 188 CUDie.addAttribute(Attr_DW_FORM_implicit_const, DW_FORM_implicit_const, 189 ICSData); 190 191 //---------------------------------------------------------------------- 192 // Test ULEB128 based forms 193 //---------------------------------------------------------------------- 194 const auto Attr_DW_FORM_udata = static_cast<dwarf::Attribute>(Attr++); 195 CUDie.addAttribute(Attr_DW_FORM_udata, DW_FORM_udata, UData[0]); 196 197 //---------------------------------------------------------------------- 198 // Test DWARF32/DWARF64 forms 199 //---------------------------------------------------------------------- 200 const auto Attr_DW_FORM_GNU_ref_alt = static_cast<dwarf::Attribute>(Attr++); 201 CUDie.addAttribute(Attr_DW_FORM_GNU_ref_alt, DW_FORM_GNU_ref_alt, 202 Dwarf32Values[0]); 203 204 const auto Attr_DW_FORM_sec_offset = static_cast<dwarf::Attribute>(Attr++); 205 CUDie.addAttribute(Attr_DW_FORM_sec_offset, DW_FORM_sec_offset, 206 Dwarf32Values[1]); 207 208 //---------------------------------------------------------------------- 209 // Add an address at the end to make sure we can decode this value 210 //---------------------------------------------------------------------- 211 const auto Attr_Last = static_cast<dwarf::Attribute>(Attr++); 212 CUDie.addAttribute(Attr_Last, DW_FORM_addr, AddrValue); 213 214 //---------------------------------------------------------------------- 215 // Generate the DWARF 216 //---------------------------------------------------------------------- 217 StringRef FileBytes = DG->generate(); 218 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 219 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 220 EXPECT_TRUE((bool)Obj); 221 DWARFContextInMemory DwarfContext(*Obj.get()); 222 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 223 EXPECT_EQ(NumCUs, 1u); 224 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 225 auto DieDG = U->getUnitDIE(false); 226 EXPECT_TRUE(DieDG.isValid()); 227 228 //---------------------------------------------------------------------- 229 // Test address forms 230 //---------------------------------------------------------------------- 231 EXPECT_EQ(DieDG.getAttributeValueAsAddress(Attr_DW_FORM_addr).getValueOr(0), 232 AddrValue); 233 234 //---------------------------------------------------------------------- 235 // Test block forms 236 //---------------------------------------------------------------------- 237 Optional<DWARFFormValue> FormValue; 238 ArrayRef<uint8_t> ExtractedBlockData; 239 Optional<ArrayRef<uint8_t>> BlockDataOpt; 240 241 FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block); 242 EXPECT_TRUE((bool)FormValue); 243 BlockDataOpt = FormValue->getAsBlock(); 244 EXPECT_TRUE(BlockDataOpt.hasValue()); 245 ExtractedBlockData = BlockDataOpt.getValue(); 246 EXPECT_EQ(ExtractedBlockData.size(), BlockSize); 247 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0); 248 249 FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block1); 250 EXPECT_TRUE((bool)FormValue); 251 BlockDataOpt = FormValue->getAsBlock(); 252 EXPECT_TRUE(BlockDataOpt.hasValue()); 253 ExtractedBlockData = BlockDataOpt.getValue(); 254 EXPECT_EQ(ExtractedBlockData.size(), BlockSize); 255 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0); 256 257 FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block2); 258 EXPECT_TRUE((bool)FormValue); 259 BlockDataOpt = FormValue->getAsBlock(); 260 EXPECT_TRUE(BlockDataOpt.hasValue()); 261 ExtractedBlockData = BlockDataOpt.getValue(); 262 EXPECT_EQ(ExtractedBlockData.size(), BlockSize); 263 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0); 264 265 FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block4); 266 EXPECT_TRUE((bool)FormValue); 267 BlockDataOpt = FormValue->getAsBlock(); 268 EXPECT_TRUE(BlockDataOpt.hasValue()); 269 ExtractedBlockData = BlockDataOpt.getValue(); 270 EXPECT_EQ(ExtractedBlockData.size(), BlockSize); 271 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0); 272 273 //---------------------------------------------------------------------- 274 // Test data forms 275 //---------------------------------------------------------------------- 276 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data1) 277 .getValueOr(0), 278 Data1); 279 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data2) 280 .getValueOr(0), 281 Data2); 282 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data4) 283 .getValueOr(0), 284 Data4); 285 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data8) 286 .getValueOr(0), 287 Data8); 288 289 //---------------------------------------------------------------------- 290 // Test string forms 291 //---------------------------------------------------------------------- 292 const char *ExtractedStringValue = 293 DieDG.getAttributeValueAsString(Attr_DW_FORM_string, nullptr); 294 EXPECT_TRUE(ExtractedStringValue != nullptr); 295 EXPECT_TRUE(strcmp(StringValue, ExtractedStringValue) == 0); 296 297 const char *ExtractedStrpValue = 298 DieDG.getAttributeValueAsString(Attr_DW_FORM_strp, nullptr); 299 EXPECT_TRUE(ExtractedStrpValue != nullptr); 300 EXPECT_TRUE(strcmp(StrpValue, ExtractedStrpValue) == 0); 301 302 //---------------------------------------------------------------------- 303 // Test reference forms 304 //---------------------------------------------------------------------- 305 EXPECT_EQ( 306 DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_addr).getValueOr(0), 307 RefAddr); 308 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref1).getValueOr(0), 309 Data1); 310 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref2).getValueOr(0), 311 Data2); 312 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref4).getValueOr(0), 313 Data4); 314 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref8).getValueOr(0), 315 Data8); 316 EXPECT_EQ( 317 DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_sig8).getValueOr(0), 318 Data8_2); 319 EXPECT_EQ( 320 DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_udata).getValueOr(0), 321 UData[0]); 322 323 //---------------------------------------------------------------------- 324 // Test flag forms 325 //---------------------------------------------------------------------- 326 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_flag_true) 327 .getValueOr(0), 328 1ULL); 329 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_flag_false) 330 .getValueOr(1), 331 0ULL); 332 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_flag_present) 333 .getValueOr(0ULL), 334 1ULL); 335 336 //---------------------------------------------------------------------- 337 // Test SLEB128 based forms 338 //---------------------------------------------------------------------- 339 EXPECT_EQ( 340 DieDG.getAttributeValueAsSignedConstant(Attr_DW_FORM_sdata).getValueOr(0), 341 SData); 342 if (Version >= 5) 343 EXPECT_EQ( 344 DieDG.getAttributeValueAsSignedConstant(Attr_DW_FORM_implicit_const) 345 .getValueOr(0), 346 ICSData); 347 348 //---------------------------------------------------------------------- 349 // Test ULEB128 based forms 350 //---------------------------------------------------------------------- 351 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_udata) 352 .getValueOr(0), 353 UData[0]); 354 355 //---------------------------------------------------------------------- 356 // Test DWARF32/DWARF64 forms 357 //---------------------------------------------------------------------- 358 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_GNU_ref_alt) 359 .getValueOr(0), 360 Dwarf32Values[0]); 361 EXPECT_EQ(DieDG.getAttributeValueAsSectionOffset(Attr_DW_FORM_sec_offset) 362 .getValueOr(0), 363 Dwarf32Values[1]); 364 365 //---------------------------------------------------------------------- 366 // Add an address at the end to make sure we can decode this value 367 //---------------------------------------------------------------------- 368 EXPECT_EQ(DieDG.getAttributeValueAsAddress(Attr_Last).getValueOr(0), 369 AddrValue); 370 } 371 372 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4AllForms) { 373 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 374 // addresses. 375 typedef uint32_t AddrType; 376 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2. 377 typedef AddrType RefAddrType; 378 TestAllForms<2, AddrType, RefAddrType>(); 379 } 380 381 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8AllForms) { 382 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 383 // addresses. 384 typedef uint64_t AddrType; 385 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2. 386 typedef AddrType RefAddrType; 387 TestAllForms<2, AddrType, RefAddrType>(); 388 } 389 390 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4AllForms) { 391 // Test that we can decode all forms for DWARF32, version 3, with 4 byte 392 // addresses. 393 typedef uint32_t AddrType; 394 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later. 395 typedef uint32_t RefAddrType; 396 TestAllForms<3, AddrType, RefAddrType>(); 397 } 398 399 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8AllForms) { 400 // Test that we can decode all forms for DWARF32, version 3, with 8 byte 401 // addresses. 402 typedef uint64_t AddrType; 403 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later 404 typedef uint32_t RefAddrType; 405 TestAllForms<3, AddrType, RefAddrType>(); 406 } 407 408 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4AllForms) { 409 // Test that we can decode all forms for DWARF32, version 4, with 4 byte 410 // addresses. 411 typedef uint32_t AddrType; 412 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later 413 typedef uint32_t RefAddrType; 414 TestAllForms<4, AddrType, RefAddrType>(); 415 } 416 417 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8AllForms) { 418 // Test that we can decode all forms for DWARF32, version 4, with 8 byte 419 // addresses. 420 typedef uint64_t AddrType; 421 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later 422 typedef uint32_t RefAddrType; 423 TestAllForms<4, AddrType, RefAddrType>(); 424 } 425 426 TEST(DWARFDebugInfo, TestDWARF32Version5Addr4AllForms) { 427 // Test that we can decode all forms for DWARF32, version 5, with 4 byte 428 // addresses. 429 typedef uint32_t AddrType; 430 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later 431 typedef uint32_t RefAddrType; 432 TestAllForms<5, AddrType, RefAddrType>(); 433 } 434 435 TEST(DWARFDebugInfo, TestDWARF32Version5Addr8AllForms) { 436 // Test that we can decode all forms for DWARF32, version 5, with 8 byte 437 // addresses. 438 typedef uint64_t AddrType; 439 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later 440 typedef uint32_t RefAddrType; 441 TestAllForms<5, AddrType, RefAddrType>(); 442 } 443 444 template <uint16_t Version, class AddrType> void TestChildren() { 445 // Test that we can decode DW_FORM_ref_addr values correctly in DWARF 2 with 446 // 4 byte addresses. DW_FORM_ref_addr values should be 4 bytes when using 447 // 8 byte addresses. 448 449 const uint8_t AddrSize = sizeof(AddrType); 450 initLLVMIfNeeded(); 451 Triple Triple = getHostTripleForAddrSize(AddrSize); 452 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 453 if (HandleExpectedError(ExpectedDG)) 454 return; 455 dwarfgen::Generator *DG = ExpectedDG.get().get(); 456 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 457 dwarfgen::DIE CUDie = CU.getUnitDIE(); 458 459 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c"); 460 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 461 462 dwarfgen::DIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram); 463 SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main"); 464 SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U); 465 SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U); 466 467 dwarfgen::DIE IntDie = CUDie.addChild(DW_TAG_base_type); 468 IntDie.addAttribute(DW_AT_name, DW_FORM_strp, "int"); 469 IntDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed); 470 IntDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4); 471 472 dwarfgen::DIE ArgcDie = SubprogramDie.addChild(DW_TAG_formal_parameter); 473 ArgcDie.addAttribute(DW_AT_name, DW_FORM_strp, "argc"); 474 // ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref4, IntDie); 475 ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, IntDie); 476 477 StringRef FileBytes = DG->generate(); 478 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 479 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 480 EXPECT_TRUE((bool)Obj); 481 DWARFContextInMemory DwarfContext(*Obj.get()); 482 483 // Verify the number of compile units is correct. 484 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 485 EXPECT_EQ(NumCUs, 1u); 486 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 487 488 // Get the compile unit DIE is valid. 489 auto DieDG = U->getUnitDIE(false); 490 EXPECT_TRUE(DieDG.isValid()); 491 // DieDG.dump(llvm::outs(), U, UINT32_MAX); 492 493 // Verify the first child of the compile unit DIE is our subprogram. 494 auto SubprogramDieDG = DieDG.getFirstChild(); 495 EXPECT_TRUE(SubprogramDieDG.isValid()); 496 EXPECT_EQ(SubprogramDieDG.getTag(), DW_TAG_subprogram); 497 498 // Verify the first child of the subprogram is our formal parameter. 499 auto ArgcDieDG = SubprogramDieDG.getFirstChild(); 500 EXPECT_TRUE(ArgcDieDG.isValid()); 501 EXPECT_EQ(ArgcDieDG.getTag(), DW_TAG_formal_parameter); 502 503 // Verify our formal parameter has a NULL tag sibling. 504 auto NullDieDG = ArgcDieDG.getSibling(); 505 EXPECT_TRUE(NullDieDG.isValid()); 506 if (NullDieDG) { 507 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null); 508 EXPECT_TRUE(!NullDieDG.getSibling().isValid()); 509 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid()); 510 } 511 512 // Verify the sibling of our subprogram is our integer base type. 513 auto IntDieDG = SubprogramDieDG.getSibling(); 514 EXPECT_TRUE(IntDieDG.isValid()); 515 EXPECT_EQ(IntDieDG.getTag(), DW_TAG_base_type); 516 517 // Verify the sibling of our subprogram is our integer base is a NULL tag. 518 NullDieDG = IntDieDG.getSibling(); 519 EXPECT_TRUE(NullDieDG.isValid()); 520 if (NullDieDG) { 521 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null); 522 EXPECT_TRUE(!NullDieDG.getSibling().isValid()); 523 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid()); 524 } 525 } 526 527 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Children) { 528 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 529 // addresses. 530 typedef uint32_t AddrType; 531 TestChildren<2, AddrType>(); 532 } 533 534 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Children) { 535 // Test that we can decode all forms for DWARF32, version 2, with 8 byte 536 // addresses. 537 typedef uint64_t AddrType; 538 TestChildren<2, AddrType>(); 539 } 540 541 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Children) { 542 // Test that we can decode all forms for DWARF32, version 3, with 4 byte 543 // addresses. 544 typedef uint32_t AddrType; 545 TestChildren<3, AddrType>(); 546 } 547 548 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Children) { 549 // Test that we can decode all forms for DWARF32, version 3, with 8 byte 550 // addresses. 551 typedef uint64_t AddrType; 552 TestChildren<3, AddrType>(); 553 } 554 555 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Children) { 556 // Test that we can decode all forms for DWARF32, version 4, with 4 byte 557 // addresses. 558 typedef uint32_t AddrType; 559 TestChildren<4, AddrType>(); 560 } 561 562 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Children) { 563 // Test that we can decode all forms for DWARF32, version 4, with 8 byte 564 // addresses. 565 typedef uint64_t AddrType; 566 TestChildren<4, AddrType>(); 567 } 568 569 template <uint16_t Version, class AddrType> void TestReferences() { 570 // Test that we can decode DW_FORM_refXXX values correctly in DWARF. 571 572 const uint8_t AddrSize = sizeof(AddrType); 573 initLLVMIfNeeded(); 574 Triple Triple = getHostTripleForAddrSize(AddrSize); 575 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 576 if (HandleExpectedError(ExpectedDG)) 577 return; 578 dwarfgen::Generator *DG = ExpectedDG.get().get(); 579 dwarfgen::CompileUnit &CU1 = DG->addCompileUnit(); 580 dwarfgen::CompileUnit &CU2 = DG->addCompileUnit(); 581 582 dwarfgen::DIE CU1Die = CU1.getUnitDIE(); 583 CU1Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c"); 584 CU1Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 585 586 dwarfgen::DIE CU1TypeDie = CU1Die.addChild(DW_TAG_base_type); 587 CU1TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "int"); 588 CU1TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed); 589 CU1TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4); 590 591 dwarfgen::DIE CU1Ref1Die = CU1Die.addChild(DW_TAG_variable); 592 CU1Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref1"); 593 CU1Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU1TypeDie); 594 595 dwarfgen::DIE CU1Ref2Die = CU1Die.addChild(DW_TAG_variable); 596 CU1Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref2"); 597 CU1Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU1TypeDie); 598 599 dwarfgen::DIE CU1Ref4Die = CU1Die.addChild(DW_TAG_variable); 600 CU1Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref4"); 601 CU1Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU1TypeDie); 602 603 dwarfgen::DIE CU1Ref8Die = CU1Die.addChild(DW_TAG_variable); 604 CU1Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref8"); 605 CU1Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU1TypeDie); 606 607 dwarfgen::DIE CU1RefAddrDie = CU1Die.addChild(DW_TAG_variable); 608 CU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1RefAddr"); 609 CU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie); 610 611 dwarfgen::DIE CU2Die = CU2.getUnitDIE(); 612 CU2Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/foo.c"); 613 CU2Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 614 615 dwarfgen::DIE CU2TypeDie = CU2Die.addChild(DW_TAG_base_type); 616 CU2TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "float"); 617 CU2TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_float); 618 CU2TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4); 619 620 dwarfgen::DIE CU2Ref1Die = CU2Die.addChild(DW_TAG_variable); 621 CU2Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref1"); 622 CU2Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU2TypeDie); 623 624 dwarfgen::DIE CU2Ref2Die = CU2Die.addChild(DW_TAG_variable); 625 CU2Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref2"); 626 CU2Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU2TypeDie); 627 628 dwarfgen::DIE CU2Ref4Die = CU2Die.addChild(DW_TAG_variable); 629 CU2Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref4"); 630 CU2Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU2TypeDie); 631 632 dwarfgen::DIE CU2Ref8Die = CU2Die.addChild(DW_TAG_variable); 633 CU2Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref8"); 634 CU2Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU2TypeDie); 635 636 dwarfgen::DIE CU2RefAddrDie = CU2Die.addChild(DW_TAG_variable); 637 CU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2RefAddr"); 638 CU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie); 639 640 // Refer to a type in CU1 from CU2 641 dwarfgen::DIE CU2ToCU1RefAddrDie = CU2Die.addChild(DW_TAG_variable); 642 CU2ToCU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2ToCU1RefAddr"); 643 CU2ToCU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie); 644 645 // Refer to a type in CU2 from CU1 646 dwarfgen::DIE CU1ToCU2RefAddrDie = CU1Die.addChild(DW_TAG_variable); 647 CU1ToCU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1ToCU2RefAddr"); 648 CU1ToCU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie); 649 650 StringRef FileBytes = DG->generate(); 651 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 652 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 653 EXPECT_TRUE((bool)Obj); 654 DWARFContextInMemory DwarfContext(*Obj.get()); 655 656 // Verify the number of compile units is correct. 657 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 658 EXPECT_EQ(NumCUs, 2u); 659 DWARFCompileUnit *U1 = DwarfContext.getCompileUnitAtIndex(0); 660 DWARFCompileUnit *U2 = DwarfContext.getCompileUnitAtIndex(1); 661 662 // Get the compile unit DIE is valid. 663 auto Unit1DieDG = U1->getUnitDIE(false); 664 EXPECT_TRUE(Unit1DieDG.isValid()); 665 // Unit1DieDG.dump(llvm::outs(), UINT32_MAX); 666 667 auto Unit2DieDG = U2->getUnitDIE(false); 668 EXPECT_TRUE(Unit2DieDG.isValid()); 669 // Unit2DieDG.dump(llvm::outs(), UINT32_MAX); 670 671 // Verify the first child of the compile unit 1 DIE is our int base type. 672 auto CU1TypeDieDG = Unit1DieDG.getFirstChild(); 673 EXPECT_TRUE(CU1TypeDieDG.isValid()); 674 EXPECT_EQ(CU1TypeDieDG.getTag(), DW_TAG_base_type); 675 EXPECT_EQ(CU1TypeDieDG.getAttributeValueAsUnsignedConstant(DW_AT_encoding) 676 .getValueOr(0), 677 DW_ATE_signed); 678 679 // Verify the first child of the compile unit 2 DIE is our float base type. 680 auto CU2TypeDieDG = Unit2DieDG.getFirstChild(); 681 EXPECT_TRUE(CU2TypeDieDG.isValid()); 682 EXPECT_EQ(CU2TypeDieDG.getTag(), DW_TAG_base_type); 683 EXPECT_EQ(CU2TypeDieDG.getAttributeValueAsUnsignedConstant(DW_AT_encoding) 684 .getValueOr(0), 685 DW_ATE_float); 686 687 // Verify the sibling of the base type DIE is our Ref1 DIE and that its 688 // DW_AT_type points to our base type DIE. 689 auto CU1Ref1DieDG = CU1TypeDieDG.getSibling(); 690 EXPECT_TRUE(CU1Ref1DieDG.isValid()); 691 EXPECT_EQ(CU1Ref1DieDG.getTag(), DW_TAG_variable); 692 EXPECT_EQ( 693 CU1Ref1DieDG.getAttributeValueAsReference(DW_AT_type).getValueOr(-1ULL), 694 CU1TypeDieDG.getOffset()); 695 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our 696 // base type DIE in CU1. 697 auto CU1Ref2DieDG = CU1Ref1DieDG.getSibling(); 698 EXPECT_TRUE(CU1Ref2DieDG.isValid()); 699 EXPECT_EQ(CU1Ref2DieDG.getTag(), DW_TAG_variable); 700 EXPECT_EQ( 701 CU1Ref2DieDG.getAttributeValueAsReference(DW_AT_type).getValueOr(-1ULL), 702 CU1TypeDieDG.getOffset()); 703 704 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our 705 // base type DIE in CU1. 706 auto CU1Ref4DieDG = CU1Ref2DieDG.getSibling(); 707 EXPECT_TRUE(CU1Ref4DieDG.isValid()); 708 EXPECT_EQ(CU1Ref4DieDG.getTag(), DW_TAG_variable); 709 EXPECT_EQ( 710 CU1Ref4DieDG.getAttributeValueAsReference(DW_AT_type).getValueOr(-1ULL), 711 CU1TypeDieDG.getOffset()); 712 713 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our 714 // base type DIE in CU1. 715 auto CU1Ref8DieDG = CU1Ref4DieDG.getSibling(); 716 EXPECT_TRUE(CU1Ref8DieDG.isValid()); 717 EXPECT_EQ(CU1Ref8DieDG.getTag(), DW_TAG_variable); 718 EXPECT_EQ( 719 CU1Ref8DieDG.getAttributeValueAsReference(DW_AT_type).getValueOr(-1ULL), 720 CU1TypeDieDG.getOffset()); 721 722 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our 723 // base type DIE in CU1. 724 auto CU1RefAddrDieDG = CU1Ref8DieDG.getSibling(); 725 EXPECT_TRUE(CU1RefAddrDieDG.isValid()); 726 EXPECT_EQ(CU1RefAddrDieDG.getTag(), DW_TAG_variable); 727 EXPECT_EQ(CU1RefAddrDieDG.getAttributeValueAsReference(DW_AT_type) 728 .getValueOr(-1ULL), 729 CU1TypeDieDG.getOffset()); 730 731 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its 732 // DW_AT_type points to our base type DIE. 733 auto CU1ToCU2RefAddrDieDG = CU1RefAddrDieDG.getSibling(); 734 EXPECT_TRUE(CU1ToCU2RefAddrDieDG.isValid()); 735 EXPECT_EQ(CU1ToCU2RefAddrDieDG.getTag(), DW_TAG_variable); 736 EXPECT_EQ(CU1ToCU2RefAddrDieDG.getAttributeValueAsReference(DW_AT_type) 737 .getValueOr(-1ULL), 738 CU2TypeDieDG.getOffset()); 739 740 // Verify the sibling of the base type DIE is our Ref1 DIE and that its 741 // DW_AT_type points to our base type DIE. 742 auto CU2Ref1DieDG = CU2TypeDieDG.getSibling(); 743 EXPECT_TRUE(CU2Ref1DieDG.isValid()); 744 EXPECT_EQ(CU2Ref1DieDG.getTag(), DW_TAG_variable); 745 EXPECT_EQ( 746 CU2Ref1DieDG.getAttributeValueAsReference(DW_AT_type).getValueOr(-1ULL), 747 CU2TypeDieDG.getOffset()); 748 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our 749 // base type DIE in CU2. 750 auto CU2Ref2DieDG = CU2Ref1DieDG.getSibling(); 751 EXPECT_TRUE(CU2Ref2DieDG.isValid()); 752 EXPECT_EQ(CU2Ref2DieDG.getTag(), DW_TAG_variable); 753 EXPECT_EQ( 754 CU2Ref2DieDG.getAttributeValueAsReference(DW_AT_type).getValueOr(-1ULL), 755 CU2TypeDieDG.getOffset()); 756 757 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our 758 // base type DIE in CU2. 759 auto CU2Ref4DieDG = CU2Ref2DieDG.getSibling(); 760 EXPECT_TRUE(CU2Ref4DieDG.isValid()); 761 EXPECT_EQ(CU2Ref4DieDG.getTag(), DW_TAG_variable); 762 EXPECT_EQ( 763 CU2Ref4DieDG.getAttributeValueAsReference(DW_AT_type).getValueOr(-1ULL), 764 CU2TypeDieDG.getOffset()); 765 766 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our 767 // base type DIE in CU2. 768 auto CU2Ref8DieDG = CU2Ref4DieDG.getSibling(); 769 EXPECT_TRUE(CU2Ref8DieDG.isValid()); 770 EXPECT_EQ(CU2Ref8DieDG.getTag(), DW_TAG_variable); 771 EXPECT_EQ( 772 CU2Ref8DieDG.getAttributeValueAsReference(DW_AT_type).getValueOr(-1ULL), 773 CU2TypeDieDG.getOffset()); 774 775 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our 776 // base type DIE in CU2. 777 auto CU2RefAddrDieDG = CU2Ref8DieDG.getSibling(); 778 EXPECT_TRUE(CU2RefAddrDieDG.isValid()); 779 EXPECT_EQ(CU2RefAddrDieDG.getTag(), DW_TAG_variable); 780 EXPECT_EQ(CU2RefAddrDieDG.getAttributeValueAsReference(DW_AT_type) 781 .getValueOr(-1ULL), 782 CU2TypeDieDG.getOffset()); 783 784 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its 785 // DW_AT_type points to our base type DIE. 786 auto CU2ToCU1RefAddrDieDG = CU2RefAddrDieDG.getSibling(); 787 EXPECT_TRUE(CU2ToCU1RefAddrDieDG.isValid()); 788 EXPECT_EQ(CU2ToCU1RefAddrDieDG.getTag(), DW_TAG_variable); 789 EXPECT_EQ(CU2ToCU1RefAddrDieDG.getAttributeValueAsReference(DW_AT_type) 790 .getValueOr(-1ULL), 791 CU1TypeDieDG.getOffset()); 792 } 793 794 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4References) { 795 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 796 // addresses. 797 typedef uint32_t AddrType; 798 TestReferences<2, AddrType>(); 799 } 800 801 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8References) { 802 // Test that we can decode all forms for DWARF32, version 2, with 8 byte 803 // addresses. 804 typedef uint64_t AddrType; 805 TestReferences<2, AddrType>(); 806 } 807 808 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4References) { 809 // Test that we can decode all forms for DWARF32, version 3, with 4 byte 810 // addresses. 811 typedef uint32_t AddrType; 812 TestReferences<3, AddrType>(); 813 } 814 815 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8References) { 816 // Test that we can decode all forms for DWARF32, version 3, with 8 byte 817 // addresses. 818 typedef uint64_t AddrType; 819 TestReferences<3, AddrType>(); 820 } 821 822 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4References) { 823 // Test that we can decode all forms for DWARF32, version 4, with 4 byte 824 // addresses. 825 typedef uint32_t AddrType; 826 TestReferences<4, AddrType>(); 827 } 828 829 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8References) { 830 // Test that we can decode all forms for DWARF32, version 4, with 8 byte 831 // addresses. 832 typedef uint64_t AddrType; 833 TestReferences<4, AddrType>(); 834 } 835 836 template <uint16_t Version, class AddrType> void TestAddresses() { 837 // Test the DWARF APIs related to accessing the DW_AT_low_pc and 838 // DW_AT_high_pc. 839 const uint8_t AddrSize = sizeof(AddrType); 840 const bool SupportsHighPCAsOffset = Version >= 4; 841 initLLVMIfNeeded(); 842 Triple Triple = getHostTripleForAddrSize(AddrSize); 843 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 844 if (HandleExpectedError(ExpectedDG)) 845 return; 846 dwarfgen::Generator *DG = ExpectedDG.get().get(); 847 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 848 dwarfgen::DIE CUDie = CU.getUnitDIE(); 849 850 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c"); 851 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 852 853 // Create a subprogram DIE with no low or high PC. 854 dwarfgen::DIE SubprogramNoPC = CUDie.addChild(DW_TAG_subprogram); 855 SubprogramNoPC.addAttribute(DW_AT_name, DW_FORM_strp, "no_pc"); 856 857 // Create a subprogram DIE with a low PC only. 858 dwarfgen::DIE SubprogramLowPC = CUDie.addChild(DW_TAG_subprogram); 859 SubprogramLowPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_pc"); 860 const uint64_t ActualLowPC = 0x1000; 861 const uint64_t ActualHighPC = 0x2000; 862 const uint64_t ActualHighPCOffset = ActualHighPC - ActualLowPC; 863 SubprogramLowPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC); 864 865 // Create a subprogram DIE with a low and high PC. 866 dwarfgen::DIE SubprogramLowHighPC = CUDie.addChild(DW_TAG_subprogram); 867 SubprogramLowHighPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_high_pc"); 868 SubprogramLowHighPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC); 869 // Encode the high PC as an offset from the low PC if supported. 870 if (SupportsHighPCAsOffset) 871 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_data4, 872 ActualHighPCOffset); 873 else 874 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_addr, ActualHighPC); 875 876 StringRef FileBytes = DG->generate(); 877 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 878 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 879 EXPECT_TRUE((bool)Obj); 880 DWARFContextInMemory DwarfContext(*Obj.get()); 881 882 // Verify the number of compile units is correct. 883 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 884 EXPECT_EQ(NumCUs, 1u); 885 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 886 887 // Get the compile unit DIE is valid. 888 auto DieDG = U->getUnitDIE(false); 889 EXPECT_TRUE(DieDG.isValid()); 890 // DieDG.dump(llvm::outs(), U, UINT32_MAX); 891 892 uint64_t LowPC, HighPC; 893 Optional<uint64_t> OptU64; 894 // Verify the that our subprogram with no PC value fails appropriately when 895 // asked for any PC values. 896 auto SubprogramDieNoPC = DieDG.getFirstChild(); 897 EXPECT_TRUE(SubprogramDieNoPC.isValid()); 898 EXPECT_EQ(SubprogramDieNoPC.getTag(), DW_TAG_subprogram); 899 OptU64 = SubprogramDieNoPC.getAttributeValueAsAddress(DW_AT_low_pc); 900 EXPECT_FALSE((bool)OptU64); 901 OptU64 = SubprogramDieNoPC.getAttributeValueAsAddress(DW_AT_high_pc); 902 EXPECT_FALSE((bool)OptU64); 903 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC)); 904 OptU64 = SubprogramDieNoPC.getAttributeValueAsAddress(DW_AT_high_pc); 905 EXPECT_FALSE((bool)OptU64); 906 OptU64 = SubprogramDieNoPC.getAttributeValueAsUnsignedConstant(DW_AT_high_pc); 907 EXPECT_FALSE((bool)OptU64); 908 OptU64 = SubprogramDieNoPC.getHighPC(ActualLowPC); 909 EXPECT_FALSE((bool)OptU64); 910 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC)); 911 912 913 // Verify the that our subprogram with only a low PC value succeeds when 914 // we ask for the Low PC, but fails appropriately when asked for the high PC 915 // or both low and high PC values. 916 auto SubprogramDieLowPC = SubprogramDieNoPC.getSibling(); 917 EXPECT_TRUE(SubprogramDieLowPC.isValid()); 918 EXPECT_EQ(SubprogramDieLowPC.getTag(), DW_TAG_subprogram); 919 OptU64 = SubprogramDieLowPC.getAttributeValueAsAddress(DW_AT_low_pc); 920 EXPECT_TRUE((bool)OptU64); 921 EXPECT_EQ(OptU64.getValue(), ActualLowPC); 922 OptU64 = SubprogramDieLowPC.getAttributeValueAsAddress(DW_AT_high_pc); 923 EXPECT_FALSE((bool)OptU64); 924 OptU64 = SubprogramDieLowPC.getAttributeValueAsUnsignedConstant(DW_AT_high_pc); 925 EXPECT_FALSE((bool)OptU64); 926 OptU64 = SubprogramDieLowPC.getHighPC(ActualLowPC); 927 EXPECT_FALSE((bool)OptU64); 928 EXPECT_FALSE(SubprogramDieLowPC.getLowAndHighPC(LowPC, HighPC)); 929 930 931 // Verify the that our subprogram with only a low PC value succeeds when 932 // we ask for the Low PC, but fails appropriately when asked for the high PC 933 // or both low and high PC values. 934 auto SubprogramDieLowHighPC = SubprogramDieLowPC.getSibling(); 935 EXPECT_TRUE(SubprogramDieLowHighPC.isValid()); 936 EXPECT_EQ(SubprogramDieLowHighPC.getTag(), DW_TAG_subprogram); 937 OptU64 = SubprogramDieLowHighPC.getAttributeValueAsAddress(DW_AT_low_pc); 938 EXPECT_TRUE((bool)OptU64); 939 EXPECT_EQ(OptU64.getValue(), ActualLowPC); 940 // Get the high PC as an address. This should succeed if the high PC was 941 // encoded as an address and fail if the high PC was encoded as an offset. 942 OptU64 = SubprogramDieLowHighPC.getAttributeValueAsAddress(DW_AT_high_pc); 943 if (SupportsHighPCAsOffset) { 944 EXPECT_FALSE((bool)OptU64); 945 } else { 946 EXPECT_TRUE((bool)OptU64); 947 EXPECT_EQ(OptU64.getValue(), ActualHighPC); 948 } 949 // Get the high PC as an unsigned constant. This should succeed if the high PC 950 // was encoded as an offset and fail if the high PC was encoded as an address. 951 OptU64 = SubprogramDieLowHighPC.getAttributeValueAsUnsignedConstant( 952 DW_AT_high_pc); 953 if (SupportsHighPCAsOffset) { 954 EXPECT_TRUE((bool)OptU64); 955 EXPECT_EQ(OptU64.getValue(), ActualHighPCOffset); 956 } else { 957 EXPECT_FALSE((bool)OptU64); 958 } 959 960 OptU64 = SubprogramDieLowHighPC.getHighPC(ActualLowPC); 961 EXPECT_TRUE((bool)OptU64); 962 EXPECT_EQ(OptU64.getValue(), ActualHighPC); 963 964 EXPECT_TRUE(SubprogramDieLowHighPC.getLowAndHighPC(LowPC, HighPC)); 965 EXPECT_EQ(LowPC, ActualLowPC); 966 EXPECT_EQ(HighPC, ActualHighPC); 967 } 968 969 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Addresses) { 970 // Test that we can decode address values in DWARF32, version 2, with 4 byte 971 // addresses. 972 typedef uint32_t AddrType; 973 TestAddresses<2, AddrType>(); 974 } 975 976 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Addresses) { 977 // Test that we can decode address values in DWARF32, version 2, with 8 byte 978 // addresses. 979 typedef uint64_t AddrType; 980 TestAddresses<2, AddrType>(); 981 } 982 983 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Addresses) { 984 // Test that we can decode address values in DWARF32, version 3, with 4 byte 985 // addresses. 986 typedef uint32_t AddrType; 987 TestAddresses<3, AddrType>(); 988 } 989 990 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Addresses) { 991 // Test that we can decode address values in DWARF32, version 3, with 8 byte 992 // addresses. 993 typedef uint64_t AddrType; 994 TestAddresses<3, AddrType>(); 995 } 996 997 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Addresses) { 998 // Test that we can decode address values in DWARF32, version 4, with 4 byte 999 // addresses. 1000 typedef uint32_t AddrType; 1001 TestAddresses<4, AddrType>(); 1002 } 1003 1004 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Addresses) { 1005 // Test that we can decode address values in DWARF32, version 4, with 8 byte 1006 // addresses. 1007 typedef uint64_t AddrType; 1008 TestAddresses<4, AddrType>(); 1009 } 1010 1011 TEST(DWARFDebugInfo, TestRelations) { 1012 // Test the DWARF APIs related to accessing the DW_AT_low_pc and 1013 // DW_AT_high_pc. 1014 uint16_t Version = 4; 1015 1016 const uint8_t AddrSize = sizeof(void *); 1017 initLLVMIfNeeded(); 1018 Triple Triple = getHostTripleForAddrSize(AddrSize); 1019 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1020 if (HandleExpectedError(ExpectedDG)) 1021 return; 1022 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1023 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1024 1025 enum class Tag: uint16_t { 1026 A = dwarf::DW_TAG_lo_user, 1027 B, 1028 C, 1029 C1, 1030 C2, 1031 D, 1032 D1 1033 }; 1034 1035 // Scope to allow us to re-use the same DIE names 1036 { 1037 // Create DWARF tree that looks like: 1038 // 1039 // CU 1040 // A 1041 // B 1042 // C 1043 // C1 1044 // C2 1045 // D 1046 // D1 1047 dwarfgen::DIE CUDie = CU.getUnitDIE(); 1048 dwarfgen::DIE A = CUDie.addChild((dwarf::Tag)Tag::A); 1049 A.addChild((dwarf::Tag)Tag::B); 1050 dwarfgen::DIE C = A.addChild((dwarf::Tag)Tag::C); 1051 dwarfgen::DIE D = A.addChild((dwarf::Tag)Tag::D); 1052 C.addChild((dwarf::Tag)Tag::C1); 1053 C.addChild((dwarf::Tag)Tag::C2); 1054 D.addChild((dwarf::Tag)Tag::D1); 1055 } 1056 1057 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1058 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1059 EXPECT_TRUE((bool)Obj); 1060 DWARFContextInMemory DwarfContext(*Obj.get()); 1061 1062 // Verify the number of compile units is correct. 1063 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 1064 EXPECT_EQ(NumCUs, 1u); 1065 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1066 1067 // Get the compile unit DIE is valid. 1068 auto CUDie = U->getUnitDIE(false); 1069 EXPECT_TRUE(CUDie.isValid()); 1070 // CUDie.dump(llvm::outs(), UINT32_MAX); 1071 1072 // The compile unit doesn't have a parent or a sibling. 1073 auto ParentDie = CUDie.getParent(); 1074 EXPECT_FALSE(ParentDie.isValid()); 1075 auto SiblingDie = CUDie.getSibling(); 1076 EXPECT_FALSE(SiblingDie.isValid()); 1077 1078 // Get the children of the compile unit 1079 auto A = CUDie.getFirstChild(); 1080 auto B = A.getFirstChild(); 1081 auto C = B.getSibling(); 1082 auto D = C.getSibling(); 1083 auto Null = D.getSibling(); 1084 1085 // Verify NULL Die is NULL and has no children or siblings 1086 EXPECT_TRUE(Null.isNULL()); 1087 EXPECT_FALSE(Null.getSibling().isValid()); 1088 EXPECT_FALSE(Null.getFirstChild().isValid()); 1089 1090 // Verify all children of the compile unit DIE are correct. 1091 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A); 1092 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B); 1093 EXPECT_EQ(C.getTag(), (dwarf::Tag)Tag::C); 1094 EXPECT_EQ(D.getTag(), (dwarf::Tag)Tag::D); 1095 1096 // Verify who has children 1097 EXPECT_TRUE(A.hasChildren()); 1098 EXPECT_FALSE(B.hasChildren()); 1099 EXPECT_TRUE(C.hasChildren()); 1100 EXPECT_TRUE(D.hasChildren()); 1101 1102 // Make sure the parent of all the children of the compile unit are the 1103 // compile unit. 1104 EXPECT_EQ(A.getParent(), CUDie); 1105 1106 // Make sure the parent of all the children of A are the A. 1107 // B is the first child in A, so we need to verify we can get the previous 1108 // DIE as the parent. 1109 EXPECT_EQ(B.getParent(), A); 1110 // C is the second child in A, so we need to make sure we can backup across 1111 // other DIE (B) at the same level to get the correct parent. 1112 EXPECT_EQ(C.getParent(), A); 1113 // D is the third child of A. We need to verify we can backup across other DIE 1114 // (B and C) including DIE that have children (D) to get the correct parent. 1115 EXPECT_EQ(D.getParent(), A); 1116 1117 // Verify that a DIE with no children returns an invalid DWARFDie. 1118 EXPECT_FALSE(B.getFirstChild().isValid()); 1119 1120 // Verify the children of the B DIE 1121 auto C1 = C.getFirstChild(); 1122 auto C2 = C1.getSibling(); 1123 EXPECT_TRUE(C2.getSibling().isNULL()); 1124 1125 // Verify all children of the B DIE correctly valid or invalid. 1126 EXPECT_EQ(C1.getTag(), (dwarf::Tag)Tag::C1); 1127 EXPECT_EQ(C2.getTag(), (dwarf::Tag)Tag::C2); 1128 1129 // Make sure the parent of all the children of the B are the B. 1130 EXPECT_EQ(C1.getParent(), C); 1131 EXPECT_EQ(C2.getParent(), C); 1132 } 1133 1134 TEST(DWARFDebugInfo, TestDWARFDie) { 1135 1136 // Make sure a default constructed DWARFDie doesn't have any parent, sibling 1137 // or child; 1138 DWARFDie DefaultDie; 1139 EXPECT_FALSE(DefaultDie.getParent().isValid()); 1140 EXPECT_FALSE(DefaultDie.getFirstChild().isValid()); 1141 EXPECT_FALSE(DefaultDie.getSibling().isValid()); 1142 } 1143 1144 TEST(DWARFDebugInfo, TestChildIterators) { 1145 // Test the DWARF APIs related to iterating across the children of a DIE using 1146 // the DWARFDie::iterator class. 1147 uint16_t Version = 4; 1148 1149 const uint8_t AddrSize = sizeof(void *); 1150 initLLVMIfNeeded(); 1151 Triple Triple = getHostTripleForAddrSize(AddrSize); 1152 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1153 if (HandleExpectedError(ExpectedDG)) 1154 return; 1155 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1156 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1157 1158 enum class Tag: uint16_t { 1159 A = dwarf::DW_TAG_lo_user, 1160 B, 1161 }; 1162 1163 // Scope to allow us to re-use the same DIE names 1164 { 1165 // Create DWARF tree that looks like: 1166 // 1167 // CU 1168 // A 1169 // B 1170 auto CUDie = CU.getUnitDIE(); 1171 CUDie.addChild((dwarf::Tag)Tag::A); 1172 CUDie.addChild((dwarf::Tag)Tag::B); 1173 } 1174 1175 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1176 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1177 EXPECT_TRUE((bool)Obj); 1178 DWARFContextInMemory DwarfContext(*Obj.get()); 1179 1180 // Verify the number of compile units is correct. 1181 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 1182 EXPECT_EQ(NumCUs, 1u); 1183 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1184 1185 // Get the compile unit DIE is valid. 1186 auto CUDie = U->getUnitDIE(false); 1187 EXPECT_TRUE(CUDie.isValid()); 1188 // CUDie.dump(llvm::outs(), UINT32_MAX); 1189 uint32_t Index; 1190 DWARFDie A; 1191 DWARFDie B; 1192 1193 // Verify the compile unit DIE's children. 1194 Index = 0; 1195 for (auto Die : CUDie.children()) { 1196 switch (Index++) { 1197 case 0: A = Die; break; 1198 case 1: B = Die; break; 1199 } 1200 } 1201 1202 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A); 1203 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B); 1204 1205 // Verify that A has no children by verifying that the begin and end contain 1206 // invalid DIEs and also that the iterators are equal. 1207 EXPECT_EQ(A.begin(), A.end()); 1208 } 1209 1210 TEST(DWARFDebugInfo, TestChildIteratorsOnInvalidDie) { 1211 // Verify that an invalid DIE has no children. 1212 DWARFDie Invalid; 1213 auto begin = Invalid.begin(); 1214 auto end = Invalid.end(); 1215 EXPECT_FALSE(begin->isValid()); 1216 EXPECT_FALSE(end->isValid()); 1217 EXPECT_EQ(begin, end); 1218 } 1219 1220 1221 TEST(DWARFDebugInfo, TestEmptyChildren) { 1222 // Test a DIE that says it has children in the abbreviation, but actually 1223 // doesn't have any attributes, will not return anything during iteration. 1224 // We do this by making sure the begin and end iterators are equal. 1225 uint16_t Version = 4; 1226 1227 const uint8_t AddrSize = sizeof(void *); 1228 initLLVMIfNeeded(); 1229 Triple Triple = getHostTripleForAddrSize(AddrSize); 1230 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1231 if (HandleExpectedError(ExpectedDG)) 1232 return; 1233 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1234 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1235 1236 // Scope to allow us to re-use the same DIE names 1237 { 1238 // Create a compile unit DIE that has an abbreviation that says it has 1239 // children, but doesn't have any actual attributes. This helps us test 1240 // a DIE that has only one child: a NULL DIE. 1241 auto CUDie = CU.getUnitDIE(); 1242 CUDie.setForceChildren(); 1243 } 1244 1245 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1246 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1247 EXPECT_TRUE((bool)Obj); 1248 DWARFContextInMemory DwarfContext(*Obj.get()); 1249 1250 // Verify the number of compile units is correct. 1251 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 1252 EXPECT_EQ(NumCUs, 1u); 1253 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1254 1255 // Get the compile unit DIE is valid. 1256 auto CUDie = U->getUnitDIE(false); 1257 EXPECT_TRUE(CUDie.isValid()); 1258 CUDie.dump(llvm::outs(), UINT32_MAX); 1259 1260 // Verify that the CU Die that says it has children, but doesn't, actually 1261 // has begin and end iterators that are equal. We want to make sure we don't 1262 // see the Null DIEs during iteration. 1263 EXPECT_EQ(CUDie.begin(), CUDie.end()); 1264 } 1265 1266 } // end anonymous namespace 1267