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/ADT/ArrayRef.h" 12 #include "llvm/ADT/Optional.h" 13 #include "llvm/ADT/SmallString.h" 14 #include "llvm/ADT/StringRef.h" 15 #include "llvm/ADT/Triple.h" 16 #include "llvm/BinaryFormat/Dwarf.h" 17 #include "llvm/Config/llvm-config.h" 18 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h" 19 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 20 #include "llvm/DebugInfo/DWARF/DWARFDie.h" 21 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" 22 #include "llvm/Object/ObjectFile.h" 23 #include "llvm/ObjectYAML/DWARFEmitter.h" 24 #include "llvm/ObjectYAML/DWARFYAML.h" 25 #include "llvm/Support/Error.h" 26 #include "llvm/Support/MemoryBuffer.h" 27 #include "llvm/Support/TargetSelect.h" 28 #include "gtest/gtest.h" 29 #include <climits> 30 #include <cstdint> 31 #include <cstring> 32 #include <string> 33 34 using namespace llvm; 35 using namespace dwarf; 36 37 namespace { 38 39 void initLLVMIfNeeded() { 40 static bool gInitialized = false; 41 if (!gInitialized) { 42 gInitialized = true; 43 InitializeAllTargets(); 44 InitializeAllTargetMCs(); 45 InitializeAllAsmPrinters(); 46 InitializeAllAsmParsers(); 47 } 48 } 49 50 Triple getHostTripleForAddrSize(uint8_t AddrSize) { 51 Triple PT(Triple::normalize(LLVM_HOST_TRIPLE)); 52 53 if (AddrSize == 8 && PT.isArch32Bit()) 54 return PT.get64BitArchVariant(); 55 if (AddrSize == 4 && PT.isArch64Bit()) 56 return PT.get32BitArchVariant(); 57 return PT; 58 } 59 60 /// Take any llvm::Expected and check and handle any errors. 61 /// 62 /// \param Expected a llvm::Excepted instance to check. 63 /// \returns true if there were errors, false otherwise. 64 template <typename T> 65 static bool HandleExpectedError(T &Expected) { 66 std::string ErrorMsg; 67 handleAllErrors(Expected.takeError(), [&](const ErrorInfoBase &EI) { 68 ErrorMsg = EI.message(); 69 }); 70 if (!ErrorMsg.empty()) { 71 ::testing::AssertionFailure() << "error: " << ErrorMsg; 72 return true; 73 } 74 return false; 75 } 76 77 template <uint16_t Version, class AddrType, class RefAddrType> 78 void TestAllForms() { 79 // Test that we can decode all DW_FORM values correctly. 80 81 const uint8_t AddrSize = sizeof(AddrType); 82 const AddrType AddrValue = (AddrType)0x0123456789abcdefULL; 83 const uint8_t BlockData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; 84 const uint32_t BlockSize = sizeof(BlockData); 85 const RefAddrType RefAddr = 0x12345678; 86 const uint8_t Data1 = 0x01U; 87 const uint16_t Data2 = 0x2345U; 88 const uint32_t Data4 = 0x6789abcdU; 89 const uint64_t Data8 = 0x0011223344556677ULL; 90 const uint64_t Data8_2 = 0xAABBCCDDEEFF0011ULL; 91 const int64_t SData = INT64_MIN; 92 const int64_t ICSData = INT64_MAX; // DW_FORM_implicit_const SData 93 const uint64_t UData[] = {UINT64_MAX - 1, UINT64_MAX - 2, UINT64_MAX - 3, 94 UINT64_MAX - 4, UINT64_MAX - 5, UINT64_MAX - 6, 95 UINT64_MAX - 7, UINT64_MAX - 8, UINT64_MAX - 9}; 96 #define UDATA_1 18446744073709551614ULL 97 const uint32_t Dwarf32Values[] = {1, 2, 3, 4, 5, 6, 7, 8}; 98 const char *StringValue = "Hello"; 99 const char *StrpValue = "World"; 100 initLLVMIfNeeded(); 101 Triple Triple = getHostTripleForAddrSize(AddrSize); 102 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 103 if (HandleExpectedError(ExpectedDG)) 104 return; 105 dwarfgen::Generator *DG = ExpectedDG.get().get(); 106 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 107 dwarfgen::DIE CUDie = CU.getUnitDIE(); 108 uint16_t Attr = DW_AT_lo_user; 109 110 //---------------------------------------------------------------------- 111 // Test address forms 112 //---------------------------------------------------------------------- 113 const auto Attr_DW_FORM_addr = static_cast<dwarf::Attribute>(Attr++); 114 CUDie.addAttribute(Attr_DW_FORM_addr, DW_FORM_addr, AddrValue); 115 116 //---------------------------------------------------------------------- 117 // Test block forms 118 //---------------------------------------------------------------------- 119 const auto Attr_DW_FORM_block = static_cast<dwarf::Attribute>(Attr++); 120 CUDie.addAttribute(Attr_DW_FORM_block, DW_FORM_block, BlockData, BlockSize); 121 122 const auto Attr_DW_FORM_block1 = static_cast<dwarf::Attribute>(Attr++); 123 CUDie.addAttribute(Attr_DW_FORM_block1, DW_FORM_block1, BlockData, BlockSize); 124 125 const auto Attr_DW_FORM_block2 = static_cast<dwarf::Attribute>(Attr++); 126 CUDie.addAttribute(Attr_DW_FORM_block2, DW_FORM_block2, BlockData, BlockSize); 127 128 const auto Attr_DW_FORM_block4 = static_cast<dwarf::Attribute>(Attr++); 129 CUDie.addAttribute(Attr_DW_FORM_block4, DW_FORM_block4, BlockData, BlockSize); 130 131 //---------------------------------------------------------------------- 132 // Test data forms 133 //---------------------------------------------------------------------- 134 const auto Attr_DW_FORM_data1 = static_cast<dwarf::Attribute>(Attr++); 135 CUDie.addAttribute(Attr_DW_FORM_data1, DW_FORM_data1, Data1); 136 137 const auto Attr_DW_FORM_data2 = static_cast<dwarf::Attribute>(Attr++); 138 CUDie.addAttribute(Attr_DW_FORM_data2, DW_FORM_data2, Data2); 139 140 const auto Attr_DW_FORM_data4 = static_cast<dwarf::Attribute>(Attr++); 141 CUDie.addAttribute(Attr_DW_FORM_data4, DW_FORM_data4, Data4); 142 143 const auto Attr_DW_FORM_data8 = static_cast<dwarf::Attribute>(Attr++); 144 CUDie.addAttribute(Attr_DW_FORM_data8, DW_FORM_data8, Data8); 145 146 //---------------------------------------------------------------------- 147 // Test string forms 148 //---------------------------------------------------------------------- 149 const auto Attr_DW_FORM_string = static_cast<dwarf::Attribute>(Attr++); 150 CUDie.addAttribute(Attr_DW_FORM_string, DW_FORM_string, StringValue); 151 152 const auto Attr_DW_FORM_strp = static_cast<dwarf::Attribute>(Attr++); 153 CUDie.addAttribute(Attr_DW_FORM_strp, DW_FORM_strp, StrpValue); 154 155 //---------------------------------------------------------------------- 156 // Test reference forms 157 //---------------------------------------------------------------------- 158 const auto Attr_DW_FORM_ref_addr = static_cast<dwarf::Attribute>(Attr++); 159 CUDie.addAttribute(Attr_DW_FORM_ref_addr, DW_FORM_ref_addr, RefAddr); 160 161 const auto Attr_DW_FORM_ref1 = static_cast<dwarf::Attribute>(Attr++); 162 CUDie.addAttribute(Attr_DW_FORM_ref1, DW_FORM_ref1, Data1); 163 164 const auto Attr_DW_FORM_ref2 = static_cast<dwarf::Attribute>(Attr++); 165 CUDie.addAttribute(Attr_DW_FORM_ref2, DW_FORM_ref2, Data2); 166 167 const auto Attr_DW_FORM_ref4 = static_cast<dwarf::Attribute>(Attr++); 168 CUDie.addAttribute(Attr_DW_FORM_ref4, DW_FORM_ref4, Data4); 169 170 const auto Attr_DW_FORM_ref8 = static_cast<dwarf::Attribute>(Attr++); 171 CUDie.addAttribute(Attr_DW_FORM_ref8, DW_FORM_ref8, Data8); 172 173 const auto Attr_DW_FORM_ref_sig8 = static_cast<dwarf::Attribute>(Attr++); 174 if (Version >= 4) 175 CUDie.addAttribute(Attr_DW_FORM_ref_sig8, DW_FORM_ref_sig8, Data8_2); 176 177 const auto Attr_DW_FORM_ref_udata = static_cast<dwarf::Attribute>(Attr++); 178 CUDie.addAttribute(Attr_DW_FORM_ref_udata, DW_FORM_ref_udata, UData[0]); 179 180 //---------------------------------------------------------------------- 181 // Test flag forms 182 //---------------------------------------------------------------------- 183 const auto Attr_DW_FORM_flag_true = static_cast<dwarf::Attribute>(Attr++); 184 CUDie.addAttribute(Attr_DW_FORM_flag_true, DW_FORM_flag, true); 185 186 const auto Attr_DW_FORM_flag_false = static_cast<dwarf::Attribute>(Attr++); 187 CUDie.addAttribute(Attr_DW_FORM_flag_false, DW_FORM_flag, false); 188 189 const auto Attr_DW_FORM_flag_present = static_cast<dwarf::Attribute>(Attr++); 190 if (Version >= 4) 191 CUDie.addAttribute(Attr_DW_FORM_flag_present, DW_FORM_flag_present); 192 193 //---------------------------------------------------------------------- 194 // Test SLEB128 based forms 195 //---------------------------------------------------------------------- 196 const auto Attr_DW_FORM_sdata = static_cast<dwarf::Attribute>(Attr++); 197 CUDie.addAttribute(Attr_DW_FORM_sdata, DW_FORM_sdata, SData); 198 199 const auto Attr_DW_FORM_implicit_const = 200 static_cast<dwarf::Attribute>(Attr++); 201 if (Version >= 5) 202 CUDie.addAttribute(Attr_DW_FORM_implicit_const, DW_FORM_implicit_const, 203 ICSData); 204 205 //---------------------------------------------------------------------- 206 // Test ULEB128 based forms 207 //---------------------------------------------------------------------- 208 const auto Attr_DW_FORM_udata = static_cast<dwarf::Attribute>(Attr++); 209 CUDie.addAttribute(Attr_DW_FORM_udata, DW_FORM_udata, UData[0]); 210 211 //---------------------------------------------------------------------- 212 // Test DWARF32/DWARF64 forms 213 //---------------------------------------------------------------------- 214 const auto Attr_DW_FORM_GNU_ref_alt = static_cast<dwarf::Attribute>(Attr++); 215 CUDie.addAttribute(Attr_DW_FORM_GNU_ref_alt, DW_FORM_GNU_ref_alt, 216 Dwarf32Values[0]); 217 218 const auto Attr_DW_FORM_sec_offset = static_cast<dwarf::Attribute>(Attr++); 219 if (Version >= 4) 220 CUDie.addAttribute(Attr_DW_FORM_sec_offset, DW_FORM_sec_offset, 221 Dwarf32Values[1]); 222 223 //---------------------------------------------------------------------- 224 // Add an address at the end to make sure we can decode this value 225 //---------------------------------------------------------------------- 226 const auto Attr_Last = static_cast<dwarf::Attribute>(Attr++); 227 CUDie.addAttribute(Attr_Last, DW_FORM_addr, AddrValue); 228 229 //---------------------------------------------------------------------- 230 // Generate the DWARF 231 //---------------------------------------------------------------------- 232 StringRef FileBytes = DG->generate(); 233 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 234 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 235 EXPECT_TRUE((bool)Obj); 236 DWARFContextInMemory DwarfContext(*Obj.get()); 237 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 238 EXPECT_EQ(NumCUs, 1u); 239 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 240 auto DieDG = U->getUnitDIE(false); 241 EXPECT_TRUE(DieDG.isValid()); 242 243 //---------------------------------------------------------------------- 244 // Test address forms 245 //---------------------------------------------------------------------- 246 EXPECT_EQ(AddrValue, toAddress(DieDG.find(Attr_DW_FORM_addr), 0)); 247 248 //---------------------------------------------------------------------- 249 // Test block forms 250 //---------------------------------------------------------------------- 251 Optional<DWARFFormValue> FormValue; 252 ArrayRef<uint8_t> ExtractedBlockData; 253 Optional<ArrayRef<uint8_t>> BlockDataOpt; 254 255 FormValue = DieDG.find(Attr_DW_FORM_block); 256 EXPECT_TRUE((bool)FormValue); 257 BlockDataOpt = FormValue->getAsBlock(); 258 EXPECT_TRUE(BlockDataOpt.hasValue()); 259 ExtractedBlockData = BlockDataOpt.getValue(); 260 EXPECT_EQ(ExtractedBlockData.size(), BlockSize); 261 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0); 262 263 FormValue = DieDG.find(Attr_DW_FORM_block1); 264 EXPECT_TRUE((bool)FormValue); 265 BlockDataOpt = FormValue->getAsBlock(); 266 EXPECT_TRUE(BlockDataOpt.hasValue()); 267 ExtractedBlockData = BlockDataOpt.getValue(); 268 EXPECT_EQ(ExtractedBlockData.size(), BlockSize); 269 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0); 270 271 FormValue = DieDG.find(Attr_DW_FORM_block2); 272 EXPECT_TRUE((bool)FormValue); 273 BlockDataOpt = FormValue->getAsBlock(); 274 EXPECT_TRUE(BlockDataOpt.hasValue()); 275 ExtractedBlockData = BlockDataOpt.getValue(); 276 EXPECT_EQ(ExtractedBlockData.size(), BlockSize); 277 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0); 278 279 FormValue = DieDG.find(Attr_DW_FORM_block4); 280 EXPECT_TRUE((bool)FormValue); 281 BlockDataOpt = FormValue->getAsBlock(); 282 EXPECT_TRUE(BlockDataOpt.hasValue()); 283 ExtractedBlockData = BlockDataOpt.getValue(); 284 EXPECT_EQ(ExtractedBlockData.size(), BlockSize); 285 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0); 286 287 //---------------------------------------------------------------------- 288 // Test data forms 289 //---------------------------------------------------------------------- 290 EXPECT_EQ(Data1, toUnsigned(DieDG.find(Attr_DW_FORM_data1), 0)); 291 EXPECT_EQ(Data2, toUnsigned(DieDG.find(Attr_DW_FORM_data2), 0)); 292 EXPECT_EQ(Data4, toUnsigned(DieDG.find(Attr_DW_FORM_data4), 0)); 293 EXPECT_EQ(Data8, toUnsigned(DieDG.find(Attr_DW_FORM_data8), 0)); 294 295 //---------------------------------------------------------------------- 296 // Test string forms 297 //---------------------------------------------------------------------- 298 auto ExtractedStringValue = toString(DieDG.find(Attr_DW_FORM_string)); 299 EXPECT_TRUE((bool)ExtractedStringValue); 300 EXPECT_TRUE(strcmp(StringValue, *ExtractedStringValue) == 0); 301 302 auto ExtractedStrpValue = toString(DieDG.find(Attr_DW_FORM_strp)); 303 EXPECT_TRUE((bool)ExtractedStrpValue); 304 EXPECT_TRUE(strcmp(StrpValue, *ExtractedStrpValue) == 0); 305 306 //---------------------------------------------------------------------- 307 // Test reference forms 308 //---------------------------------------------------------------------- 309 EXPECT_EQ(RefAddr, toReference(DieDG.find(Attr_DW_FORM_ref_addr), 0)); 310 EXPECT_EQ(Data1, toReference(DieDG.find(Attr_DW_FORM_ref1), 0)); 311 EXPECT_EQ(Data2, toReference(DieDG.find(Attr_DW_FORM_ref2), 0)); 312 EXPECT_EQ(Data4, toReference(DieDG.find(Attr_DW_FORM_ref4), 0)); 313 EXPECT_EQ(Data8, toReference(DieDG.find(Attr_DW_FORM_ref8), 0)); 314 if (Version >= 4) { 315 EXPECT_EQ(Data8_2, toReference(DieDG.find(Attr_DW_FORM_ref_sig8), 0)); 316 } 317 EXPECT_EQ(UData[0], toReference(DieDG.find(Attr_DW_FORM_ref_udata), 0)); 318 319 //---------------------------------------------------------------------- 320 // Test flag forms 321 //---------------------------------------------------------------------- 322 EXPECT_EQ(1ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_true), 0)); 323 EXPECT_EQ(0ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_false), 1)); 324 if (Version >= 4) { 325 EXPECT_EQ(1ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_present), 0)); 326 } 327 328 //---------------------------------------------------------------------- 329 // Test SLEB128 based forms 330 //---------------------------------------------------------------------- 331 EXPECT_EQ(SData, toSigned(DieDG.find(Attr_DW_FORM_sdata), 0)); 332 if (Version >= 5) { 333 EXPECT_EQ(ICSData, toSigned(DieDG.find(Attr_DW_FORM_implicit_const), 0)); 334 } 335 336 //---------------------------------------------------------------------- 337 // Test ULEB128 based forms 338 //---------------------------------------------------------------------- 339 EXPECT_EQ(UData[0], toUnsigned(DieDG.find(Attr_DW_FORM_udata), 0)); 340 341 //---------------------------------------------------------------------- 342 // Test DWARF32/DWARF64 forms 343 //---------------------------------------------------------------------- 344 EXPECT_EQ(Dwarf32Values[0], 345 toReference(DieDG.find(Attr_DW_FORM_GNU_ref_alt), 0)); 346 if (Version >= 4) { 347 EXPECT_EQ(Dwarf32Values[1], 348 toSectionOffset(DieDG.find(Attr_DW_FORM_sec_offset), 0)); 349 } 350 351 //---------------------------------------------------------------------- 352 // Add an address at the end to make sure we can decode this value 353 //---------------------------------------------------------------------- 354 EXPECT_EQ(AddrValue, toAddress(DieDG.find(Attr_Last), 0)); 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 TEST(DWARFDebugInfo, TestDWARF32Version5Addr4AllForms) { 412 // Test that we can decode all forms for DWARF32, version 5, with 4 byte 413 // addresses. 414 typedef uint32_t AddrType; 415 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later 416 typedef uint32_t RefAddrType; 417 TestAllForms<5, AddrType, RefAddrType>(); 418 } 419 420 TEST(DWARFDebugInfo, TestDWARF32Version5Addr8AllForms) { 421 // Test that we can decode all forms for DWARF32, version 5, with 8 byte 422 // addresses. 423 typedef uint64_t AddrType; 424 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later 425 typedef uint32_t RefAddrType; 426 TestAllForms<5, AddrType, RefAddrType>(); 427 } 428 429 template <uint16_t Version, class AddrType> void TestChildren() { 430 // Test that we can decode DW_FORM_ref_addr values correctly in DWARF 2 with 431 // 4 byte addresses. DW_FORM_ref_addr values should be 4 bytes when using 432 // 8 byte addresses. 433 434 const uint8_t AddrSize = sizeof(AddrType); 435 initLLVMIfNeeded(); 436 Triple Triple = getHostTripleForAddrSize(AddrSize); 437 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 438 if (HandleExpectedError(ExpectedDG)) 439 return; 440 dwarfgen::Generator *DG = ExpectedDG.get().get(); 441 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 442 dwarfgen::DIE CUDie = CU.getUnitDIE(); 443 444 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c"); 445 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 446 447 dwarfgen::DIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram); 448 SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main"); 449 SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U); 450 SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U); 451 452 dwarfgen::DIE IntDie = CUDie.addChild(DW_TAG_base_type); 453 IntDie.addAttribute(DW_AT_name, DW_FORM_strp, "int"); 454 IntDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed); 455 IntDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4); 456 457 dwarfgen::DIE ArgcDie = SubprogramDie.addChild(DW_TAG_formal_parameter); 458 ArgcDie.addAttribute(DW_AT_name, DW_FORM_strp, "argc"); 459 // ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref4, IntDie); 460 ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, IntDie); 461 462 StringRef FileBytes = DG->generate(); 463 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 464 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 465 EXPECT_TRUE((bool)Obj); 466 DWARFContextInMemory DwarfContext(*Obj.get()); 467 468 // Verify the number of compile units is correct. 469 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 470 EXPECT_EQ(NumCUs, 1u); 471 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 472 473 // Get the compile unit DIE is valid. 474 auto DieDG = U->getUnitDIE(false); 475 EXPECT_TRUE(DieDG.isValid()); 476 477 // Verify the first child of the compile unit DIE is our subprogram. 478 auto SubprogramDieDG = DieDG.getFirstChild(); 479 EXPECT_TRUE(SubprogramDieDG.isValid()); 480 EXPECT_EQ(SubprogramDieDG.getTag(), DW_TAG_subprogram); 481 482 // Verify the first child of the subprogram is our formal parameter. 483 auto ArgcDieDG = SubprogramDieDG.getFirstChild(); 484 EXPECT_TRUE(ArgcDieDG.isValid()); 485 EXPECT_EQ(ArgcDieDG.getTag(), DW_TAG_formal_parameter); 486 487 // Verify our formal parameter has a NULL tag sibling. 488 auto NullDieDG = ArgcDieDG.getSibling(); 489 EXPECT_TRUE(NullDieDG.isValid()); 490 if (NullDieDG) { 491 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null); 492 EXPECT_TRUE(!NullDieDG.getSibling().isValid()); 493 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid()); 494 } 495 496 // Verify the sibling of our subprogram is our integer base type. 497 auto IntDieDG = SubprogramDieDG.getSibling(); 498 EXPECT_TRUE(IntDieDG.isValid()); 499 EXPECT_EQ(IntDieDG.getTag(), DW_TAG_base_type); 500 501 // Verify the sibling of our subprogram is our integer base is a NULL tag. 502 NullDieDG = IntDieDG.getSibling(); 503 EXPECT_TRUE(NullDieDG.isValid()); 504 if (NullDieDG) { 505 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null); 506 EXPECT_TRUE(!NullDieDG.getSibling().isValid()); 507 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid()); 508 } 509 } 510 511 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Children) { 512 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 513 // addresses. 514 typedef uint32_t AddrType; 515 TestChildren<2, AddrType>(); 516 } 517 518 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Children) { 519 // Test that we can decode all forms for DWARF32, version 2, with 8 byte 520 // addresses. 521 typedef uint64_t AddrType; 522 TestChildren<2, AddrType>(); 523 } 524 525 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Children) { 526 // Test that we can decode all forms for DWARF32, version 3, with 4 byte 527 // addresses. 528 typedef uint32_t AddrType; 529 TestChildren<3, AddrType>(); 530 } 531 532 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Children) { 533 // Test that we can decode all forms for DWARF32, version 3, with 8 byte 534 // addresses. 535 typedef uint64_t AddrType; 536 TestChildren<3, AddrType>(); 537 } 538 539 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Children) { 540 // Test that we can decode all forms for DWARF32, version 4, with 4 byte 541 // addresses. 542 typedef uint32_t AddrType; 543 TestChildren<4, AddrType>(); 544 } 545 546 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Children) { 547 // Test that we can decode all forms for DWARF32, version 4, with 8 byte 548 // addresses. 549 typedef uint64_t AddrType; 550 TestChildren<4, AddrType>(); 551 } 552 553 template <uint16_t Version, class AddrType> void TestReferences() { 554 // Test that we can decode DW_FORM_refXXX values correctly in DWARF. 555 556 const uint8_t AddrSize = sizeof(AddrType); 557 initLLVMIfNeeded(); 558 Triple Triple = getHostTripleForAddrSize(AddrSize); 559 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 560 if (HandleExpectedError(ExpectedDG)) 561 return; 562 dwarfgen::Generator *DG = ExpectedDG.get().get(); 563 dwarfgen::CompileUnit &CU1 = DG->addCompileUnit(); 564 dwarfgen::CompileUnit &CU2 = DG->addCompileUnit(); 565 566 dwarfgen::DIE CU1Die = CU1.getUnitDIE(); 567 CU1Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c"); 568 CU1Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 569 570 dwarfgen::DIE CU1TypeDie = CU1Die.addChild(DW_TAG_base_type); 571 CU1TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "int"); 572 CU1TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed); 573 CU1TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4); 574 575 dwarfgen::DIE CU1Ref1Die = CU1Die.addChild(DW_TAG_variable); 576 CU1Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref1"); 577 CU1Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU1TypeDie); 578 579 dwarfgen::DIE CU1Ref2Die = CU1Die.addChild(DW_TAG_variable); 580 CU1Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref2"); 581 CU1Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU1TypeDie); 582 583 dwarfgen::DIE CU1Ref4Die = CU1Die.addChild(DW_TAG_variable); 584 CU1Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref4"); 585 CU1Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU1TypeDie); 586 587 dwarfgen::DIE CU1Ref8Die = CU1Die.addChild(DW_TAG_variable); 588 CU1Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref8"); 589 CU1Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU1TypeDie); 590 591 dwarfgen::DIE CU1RefAddrDie = CU1Die.addChild(DW_TAG_variable); 592 CU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1RefAddr"); 593 CU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie); 594 595 dwarfgen::DIE CU2Die = CU2.getUnitDIE(); 596 CU2Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/foo.c"); 597 CU2Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 598 599 dwarfgen::DIE CU2TypeDie = CU2Die.addChild(DW_TAG_base_type); 600 CU2TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "float"); 601 CU2TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_float); 602 CU2TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4); 603 604 dwarfgen::DIE CU2Ref1Die = CU2Die.addChild(DW_TAG_variable); 605 CU2Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref1"); 606 CU2Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU2TypeDie); 607 608 dwarfgen::DIE CU2Ref2Die = CU2Die.addChild(DW_TAG_variable); 609 CU2Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref2"); 610 CU2Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU2TypeDie); 611 612 dwarfgen::DIE CU2Ref4Die = CU2Die.addChild(DW_TAG_variable); 613 CU2Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref4"); 614 CU2Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU2TypeDie); 615 616 dwarfgen::DIE CU2Ref8Die = CU2Die.addChild(DW_TAG_variable); 617 CU2Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref8"); 618 CU2Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU2TypeDie); 619 620 dwarfgen::DIE CU2RefAddrDie = CU2Die.addChild(DW_TAG_variable); 621 CU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2RefAddr"); 622 CU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie); 623 624 // Refer to a type in CU1 from CU2 625 dwarfgen::DIE CU2ToCU1RefAddrDie = CU2Die.addChild(DW_TAG_variable); 626 CU2ToCU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2ToCU1RefAddr"); 627 CU2ToCU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie); 628 629 // Refer to a type in CU2 from CU1 630 dwarfgen::DIE CU1ToCU2RefAddrDie = CU1Die.addChild(DW_TAG_variable); 631 CU1ToCU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1ToCU2RefAddr"); 632 CU1ToCU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie); 633 634 StringRef FileBytes = DG->generate(); 635 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 636 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 637 EXPECT_TRUE((bool)Obj); 638 DWARFContextInMemory DwarfContext(*Obj.get()); 639 640 // Verify the number of compile units is correct. 641 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 642 EXPECT_EQ(NumCUs, 2u); 643 DWARFCompileUnit *U1 = DwarfContext.getCompileUnitAtIndex(0); 644 DWARFCompileUnit *U2 = DwarfContext.getCompileUnitAtIndex(1); 645 646 // Get the compile unit DIE is valid. 647 auto Unit1DieDG = U1->getUnitDIE(false); 648 EXPECT_TRUE(Unit1DieDG.isValid()); 649 650 auto Unit2DieDG = U2->getUnitDIE(false); 651 EXPECT_TRUE(Unit2DieDG.isValid()); 652 653 // Verify the first child of the compile unit 1 DIE is our int base type. 654 auto CU1TypeDieDG = Unit1DieDG.getFirstChild(); 655 EXPECT_TRUE(CU1TypeDieDG.isValid()); 656 EXPECT_EQ(CU1TypeDieDG.getTag(), DW_TAG_base_type); 657 EXPECT_EQ(DW_ATE_signed, toUnsigned(CU1TypeDieDG.find(DW_AT_encoding), 0)); 658 659 // Verify the first child of the compile unit 2 DIE is our float base type. 660 auto CU2TypeDieDG = Unit2DieDG.getFirstChild(); 661 EXPECT_TRUE(CU2TypeDieDG.isValid()); 662 EXPECT_EQ(CU2TypeDieDG.getTag(), DW_TAG_base_type); 663 EXPECT_EQ(DW_ATE_float, toUnsigned(CU2TypeDieDG.find(DW_AT_encoding), 0)); 664 665 // Verify the sibling of the base type DIE is our Ref1 DIE and that its 666 // DW_AT_type points to our base type DIE. 667 auto CU1Ref1DieDG = CU1TypeDieDG.getSibling(); 668 EXPECT_TRUE(CU1Ref1DieDG.isValid()); 669 EXPECT_EQ(CU1Ref1DieDG.getTag(), DW_TAG_variable); 670 EXPECT_EQ(CU1TypeDieDG.getOffset(), 671 toReference(CU1Ref1DieDG.find(DW_AT_type), -1ULL)); 672 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our 673 // base type DIE in CU1. 674 auto CU1Ref2DieDG = CU1Ref1DieDG.getSibling(); 675 EXPECT_TRUE(CU1Ref2DieDG.isValid()); 676 EXPECT_EQ(CU1Ref2DieDG.getTag(), DW_TAG_variable); 677 EXPECT_EQ(CU1TypeDieDG.getOffset(), 678 toReference(CU1Ref2DieDG.find(DW_AT_type), -1ULL)); 679 680 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our 681 // base type DIE in CU1. 682 auto CU1Ref4DieDG = CU1Ref2DieDG.getSibling(); 683 EXPECT_TRUE(CU1Ref4DieDG.isValid()); 684 EXPECT_EQ(CU1Ref4DieDG.getTag(), DW_TAG_variable); 685 EXPECT_EQ(CU1TypeDieDG.getOffset(), 686 toReference(CU1Ref4DieDG.find(DW_AT_type), -1ULL)); 687 688 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our 689 // base type DIE in CU1. 690 auto CU1Ref8DieDG = CU1Ref4DieDG.getSibling(); 691 EXPECT_TRUE(CU1Ref8DieDG.isValid()); 692 EXPECT_EQ(CU1Ref8DieDG.getTag(), DW_TAG_variable); 693 EXPECT_EQ(CU1TypeDieDG.getOffset(), 694 toReference(CU1Ref8DieDG.find(DW_AT_type), -1ULL)); 695 696 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our 697 // base type DIE in CU1. 698 auto CU1RefAddrDieDG = CU1Ref8DieDG.getSibling(); 699 EXPECT_TRUE(CU1RefAddrDieDG.isValid()); 700 EXPECT_EQ(CU1RefAddrDieDG.getTag(), DW_TAG_variable); 701 EXPECT_EQ(CU1TypeDieDG.getOffset(), 702 toReference(CU1RefAddrDieDG.find(DW_AT_type), -1ULL)); 703 704 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its 705 // DW_AT_type points to our base type DIE. 706 auto CU1ToCU2RefAddrDieDG = CU1RefAddrDieDG.getSibling(); 707 EXPECT_TRUE(CU1ToCU2RefAddrDieDG.isValid()); 708 EXPECT_EQ(CU1ToCU2RefAddrDieDG.getTag(), DW_TAG_variable); 709 EXPECT_EQ(CU2TypeDieDG.getOffset(), 710 toReference(CU1ToCU2RefAddrDieDG.find(DW_AT_type), -1ULL)); 711 712 // Verify the sibling of the base type DIE is our Ref1 DIE and that its 713 // DW_AT_type points to our base type DIE. 714 auto CU2Ref1DieDG = CU2TypeDieDG.getSibling(); 715 EXPECT_TRUE(CU2Ref1DieDG.isValid()); 716 EXPECT_EQ(CU2Ref1DieDG.getTag(), DW_TAG_variable); 717 EXPECT_EQ(CU2TypeDieDG.getOffset(), 718 toReference(CU2Ref1DieDG.find(DW_AT_type), -1ULL)); 719 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our 720 // base type DIE in CU2. 721 auto CU2Ref2DieDG = CU2Ref1DieDG.getSibling(); 722 EXPECT_TRUE(CU2Ref2DieDG.isValid()); 723 EXPECT_EQ(CU2Ref2DieDG.getTag(), DW_TAG_variable); 724 EXPECT_EQ(CU2TypeDieDG.getOffset(), 725 toReference(CU2Ref2DieDG.find(DW_AT_type), -1ULL)); 726 727 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our 728 // base type DIE in CU2. 729 auto CU2Ref4DieDG = CU2Ref2DieDG.getSibling(); 730 EXPECT_TRUE(CU2Ref4DieDG.isValid()); 731 EXPECT_EQ(CU2Ref4DieDG.getTag(), DW_TAG_variable); 732 EXPECT_EQ(CU2TypeDieDG.getOffset(), 733 toReference(CU2Ref4DieDG.find(DW_AT_type), -1ULL)); 734 735 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our 736 // base type DIE in CU2. 737 auto CU2Ref8DieDG = CU2Ref4DieDG.getSibling(); 738 EXPECT_TRUE(CU2Ref8DieDG.isValid()); 739 EXPECT_EQ(CU2Ref8DieDG.getTag(), DW_TAG_variable); 740 EXPECT_EQ(CU2TypeDieDG.getOffset(), 741 toReference(CU2Ref8DieDG.find(DW_AT_type), -1ULL)); 742 743 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our 744 // base type DIE in CU2. 745 auto CU2RefAddrDieDG = CU2Ref8DieDG.getSibling(); 746 EXPECT_TRUE(CU2RefAddrDieDG.isValid()); 747 EXPECT_EQ(CU2RefAddrDieDG.getTag(), DW_TAG_variable); 748 EXPECT_EQ(CU2TypeDieDG.getOffset(), 749 toReference(CU2RefAddrDieDG.find(DW_AT_type), -1ULL)); 750 751 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its 752 // DW_AT_type points to our base type DIE. 753 auto CU2ToCU1RefAddrDieDG = CU2RefAddrDieDG.getSibling(); 754 EXPECT_TRUE(CU2ToCU1RefAddrDieDG.isValid()); 755 EXPECT_EQ(CU2ToCU1RefAddrDieDG.getTag(), DW_TAG_variable); 756 EXPECT_EQ(CU1TypeDieDG.getOffset(), 757 toReference(CU2ToCU1RefAddrDieDG.find(DW_AT_type), -1ULL)); 758 } 759 760 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4References) { 761 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 762 // addresses. 763 typedef uint32_t AddrType; 764 TestReferences<2, AddrType>(); 765 } 766 767 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8References) { 768 // Test that we can decode all forms for DWARF32, version 2, with 8 byte 769 // addresses. 770 typedef uint64_t AddrType; 771 TestReferences<2, AddrType>(); 772 } 773 774 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4References) { 775 // Test that we can decode all forms for DWARF32, version 3, with 4 byte 776 // addresses. 777 typedef uint32_t AddrType; 778 TestReferences<3, AddrType>(); 779 } 780 781 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8References) { 782 // Test that we can decode all forms for DWARF32, version 3, with 8 byte 783 // addresses. 784 typedef uint64_t AddrType; 785 TestReferences<3, AddrType>(); 786 } 787 788 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4References) { 789 // Test that we can decode all forms for DWARF32, version 4, with 4 byte 790 // addresses. 791 typedef uint32_t AddrType; 792 TestReferences<4, AddrType>(); 793 } 794 795 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8References) { 796 // Test that we can decode all forms for DWARF32, version 4, with 8 byte 797 // addresses. 798 typedef uint64_t AddrType; 799 TestReferences<4, AddrType>(); 800 } 801 802 template <uint16_t Version, class AddrType> void TestAddresses() { 803 // Test the DWARF APIs related to accessing the DW_AT_low_pc and 804 // DW_AT_high_pc. 805 const uint8_t AddrSize = sizeof(AddrType); 806 const bool SupportsHighPCAsOffset = Version >= 4; 807 initLLVMIfNeeded(); 808 Triple Triple = getHostTripleForAddrSize(AddrSize); 809 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 810 if (HandleExpectedError(ExpectedDG)) 811 return; 812 dwarfgen::Generator *DG = ExpectedDG.get().get(); 813 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 814 dwarfgen::DIE CUDie = CU.getUnitDIE(); 815 816 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c"); 817 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 818 819 // Create a subprogram DIE with no low or high PC. 820 dwarfgen::DIE SubprogramNoPC = CUDie.addChild(DW_TAG_subprogram); 821 SubprogramNoPC.addAttribute(DW_AT_name, DW_FORM_strp, "no_pc"); 822 823 // Create a subprogram DIE with a low PC only. 824 dwarfgen::DIE SubprogramLowPC = CUDie.addChild(DW_TAG_subprogram); 825 SubprogramLowPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_pc"); 826 const uint64_t ActualLowPC = 0x1000; 827 const uint64_t ActualHighPC = 0x2000; 828 const uint64_t ActualHighPCOffset = ActualHighPC - ActualLowPC; 829 SubprogramLowPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC); 830 831 // Create a subprogram DIE with a low and high PC. 832 dwarfgen::DIE SubprogramLowHighPC = CUDie.addChild(DW_TAG_subprogram); 833 SubprogramLowHighPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_high_pc"); 834 SubprogramLowHighPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC); 835 // Encode the high PC as an offset from the low PC if supported. 836 if (SupportsHighPCAsOffset) 837 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_data4, 838 ActualHighPCOffset); 839 else 840 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_addr, ActualHighPC); 841 842 StringRef FileBytes = DG->generate(); 843 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 844 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 845 EXPECT_TRUE((bool)Obj); 846 DWARFContextInMemory DwarfContext(*Obj.get()); 847 848 // Verify the number of compile units is correct. 849 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 850 EXPECT_EQ(NumCUs, 1u); 851 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 852 853 // Get the compile unit DIE is valid. 854 auto DieDG = U->getUnitDIE(false); 855 EXPECT_TRUE(DieDG.isValid()); 856 857 uint64_t LowPC, HighPC, SectionIndex; 858 Optional<uint64_t> OptU64; 859 // Verify the that our subprogram with no PC value fails appropriately when 860 // asked for any PC values. 861 auto SubprogramDieNoPC = DieDG.getFirstChild(); 862 EXPECT_TRUE(SubprogramDieNoPC.isValid()); 863 EXPECT_EQ(SubprogramDieNoPC.getTag(), DW_TAG_subprogram); 864 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_low_pc)); 865 EXPECT_FALSE((bool)OptU64); 866 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_high_pc)); 867 EXPECT_FALSE((bool)OptU64); 868 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC, SectionIndex)); 869 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_high_pc)); 870 EXPECT_FALSE((bool)OptU64); 871 OptU64 = toUnsigned(SubprogramDieNoPC.find(DW_AT_high_pc)); 872 EXPECT_FALSE((bool)OptU64); 873 OptU64 = SubprogramDieNoPC.getHighPC(ActualLowPC); 874 EXPECT_FALSE((bool)OptU64); 875 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC, SectionIndex)); 876 877 // Verify the that our subprogram with only a low PC value succeeds when 878 // we ask for the Low PC, but fails appropriately when asked for the high PC 879 // or both low and high PC values. 880 auto SubprogramDieLowPC = SubprogramDieNoPC.getSibling(); 881 EXPECT_TRUE(SubprogramDieLowPC.isValid()); 882 EXPECT_EQ(SubprogramDieLowPC.getTag(), DW_TAG_subprogram); 883 OptU64 = toAddress(SubprogramDieLowPC.find(DW_AT_low_pc)); 884 EXPECT_TRUE((bool)OptU64); 885 EXPECT_EQ(OptU64.getValue(), ActualLowPC); 886 OptU64 = toAddress(SubprogramDieLowPC.find(DW_AT_high_pc)); 887 EXPECT_FALSE((bool)OptU64); 888 OptU64 = toUnsigned(SubprogramDieLowPC.find(DW_AT_high_pc)); 889 EXPECT_FALSE((bool)OptU64); 890 OptU64 = SubprogramDieLowPC.getHighPC(ActualLowPC); 891 EXPECT_FALSE((bool)OptU64); 892 EXPECT_FALSE(SubprogramDieLowPC.getLowAndHighPC(LowPC, HighPC, SectionIndex)); 893 894 // Verify the that our subprogram with only a low PC value succeeds when 895 // we ask for the Low PC, but fails appropriately when asked for the high PC 896 // or both low and high PC values. 897 auto SubprogramDieLowHighPC = SubprogramDieLowPC.getSibling(); 898 EXPECT_TRUE(SubprogramDieLowHighPC.isValid()); 899 EXPECT_EQ(SubprogramDieLowHighPC.getTag(), DW_TAG_subprogram); 900 OptU64 = toAddress(SubprogramDieLowHighPC.find(DW_AT_low_pc)); 901 EXPECT_TRUE((bool)OptU64); 902 EXPECT_EQ(OptU64.getValue(), ActualLowPC); 903 // Get the high PC as an address. This should succeed if the high PC was 904 // encoded as an address and fail if the high PC was encoded as an offset. 905 OptU64 = toAddress(SubprogramDieLowHighPC.find(DW_AT_high_pc)); 906 if (SupportsHighPCAsOffset) { 907 EXPECT_FALSE((bool)OptU64); 908 } else { 909 EXPECT_TRUE((bool)OptU64); 910 EXPECT_EQ(OptU64.getValue(), ActualHighPC); 911 } 912 // Get the high PC as an unsigned constant. This should succeed if the high PC 913 // was encoded as an offset and fail if the high PC was encoded as an address. 914 OptU64 = toUnsigned(SubprogramDieLowHighPC.find(DW_AT_high_pc)); 915 if (SupportsHighPCAsOffset) { 916 EXPECT_TRUE((bool)OptU64); 917 EXPECT_EQ(OptU64.getValue(), ActualHighPCOffset); 918 } else { 919 EXPECT_FALSE((bool)OptU64); 920 } 921 922 OptU64 = SubprogramDieLowHighPC.getHighPC(ActualLowPC); 923 EXPECT_TRUE((bool)OptU64); 924 EXPECT_EQ(OptU64.getValue(), ActualHighPC); 925 926 EXPECT_TRUE(SubprogramDieLowHighPC.getLowAndHighPC(LowPC, HighPC, SectionIndex)); 927 EXPECT_EQ(LowPC, ActualLowPC); 928 EXPECT_EQ(HighPC, ActualHighPC); 929 } 930 931 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Addresses) { 932 // Test that we can decode address values in DWARF32, version 2, with 4 byte 933 // addresses. 934 typedef uint32_t AddrType; 935 TestAddresses<2, AddrType>(); 936 } 937 938 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Addresses) { 939 // Test that we can decode address values in DWARF32, version 2, with 8 byte 940 // addresses. 941 typedef uint64_t AddrType; 942 TestAddresses<2, AddrType>(); 943 } 944 945 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Addresses) { 946 // Test that we can decode address values in DWARF32, version 3, with 4 byte 947 // addresses. 948 typedef uint32_t AddrType; 949 TestAddresses<3, AddrType>(); 950 } 951 952 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Addresses) { 953 // Test that we can decode address values in DWARF32, version 3, with 8 byte 954 // addresses. 955 typedef uint64_t AddrType; 956 TestAddresses<3, AddrType>(); 957 } 958 959 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Addresses) { 960 // Test that we can decode address values in DWARF32, version 4, with 4 byte 961 // addresses. 962 typedef uint32_t AddrType; 963 TestAddresses<4, AddrType>(); 964 } 965 966 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Addresses) { 967 // Test that we can decode address values in DWARF32, version 4, with 8 byte 968 // addresses. 969 typedef uint64_t AddrType; 970 TestAddresses<4, AddrType>(); 971 } 972 973 TEST(DWARFDebugInfo, TestRelations) { 974 // Test the DWARF APIs related to accessing the DW_AT_low_pc and 975 // DW_AT_high_pc. 976 uint16_t Version = 4; 977 978 const uint8_t AddrSize = sizeof(void *); 979 initLLVMIfNeeded(); 980 Triple Triple = getHostTripleForAddrSize(AddrSize); 981 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 982 if (HandleExpectedError(ExpectedDG)) 983 return; 984 dwarfgen::Generator *DG = ExpectedDG.get().get(); 985 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 986 987 enum class Tag: uint16_t { 988 A = dwarf::DW_TAG_lo_user, 989 B, 990 C, 991 C1, 992 C2, 993 D, 994 D1 995 }; 996 997 // Scope to allow us to re-use the same DIE names 998 { 999 // Create DWARF tree that looks like: 1000 // 1001 // CU 1002 // A 1003 // B 1004 // C 1005 // C1 1006 // C2 1007 // D 1008 // D1 1009 dwarfgen::DIE CUDie = CU.getUnitDIE(); 1010 dwarfgen::DIE A = CUDie.addChild((dwarf::Tag)Tag::A); 1011 A.addChild((dwarf::Tag)Tag::B); 1012 dwarfgen::DIE C = A.addChild((dwarf::Tag)Tag::C); 1013 dwarfgen::DIE D = A.addChild((dwarf::Tag)Tag::D); 1014 C.addChild((dwarf::Tag)Tag::C1); 1015 C.addChild((dwarf::Tag)Tag::C2); 1016 D.addChild((dwarf::Tag)Tag::D1); 1017 } 1018 1019 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1020 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1021 EXPECT_TRUE((bool)Obj); 1022 DWARFContextInMemory DwarfContext(*Obj.get()); 1023 1024 // Verify the number of compile units is correct. 1025 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 1026 EXPECT_EQ(NumCUs, 1u); 1027 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1028 1029 // Get the compile unit DIE is valid. 1030 auto CUDie = U->getUnitDIE(false); 1031 EXPECT_TRUE(CUDie.isValid()); 1032 1033 // The compile unit doesn't have a parent or a sibling. 1034 auto ParentDie = CUDie.getParent(); 1035 EXPECT_FALSE(ParentDie.isValid()); 1036 auto SiblingDie = CUDie.getSibling(); 1037 EXPECT_FALSE(SiblingDie.isValid()); 1038 1039 // Get the children of the compile unit 1040 auto A = CUDie.getFirstChild(); 1041 auto B = A.getFirstChild(); 1042 auto C = B.getSibling(); 1043 auto D = C.getSibling(); 1044 auto Null = D.getSibling(); 1045 1046 // Verify NULL Die is NULL and has no children or siblings 1047 EXPECT_TRUE(Null.isNULL()); 1048 EXPECT_FALSE(Null.getSibling().isValid()); 1049 EXPECT_FALSE(Null.getFirstChild().isValid()); 1050 1051 // Verify all children of the compile unit DIE are correct. 1052 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A); 1053 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B); 1054 EXPECT_EQ(C.getTag(), (dwarf::Tag)Tag::C); 1055 EXPECT_EQ(D.getTag(), (dwarf::Tag)Tag::D); 1056 1057 // Verify who has children 1058 EXPECT_TRUE(A.hasChildren()); 1059 EXPECT_FALSE(B.hasChildren()); 1060 EXPECT_TRUE(C.hasChildren()); 1061 EXPECT_TRUE(D.hasChildren()); 1062 1063 // Make sure the parent of all the children of the compile unit are the 1064 // compile unit. 1065 EXPECT_EQ(A.getParent(), CUDie); 1066 1067 // Make sure the parent of all the children of A are the A. 1068 // B is the first child in A, so we need to verify we can get the previous 1069 // DIE as the parent. 1070 EXPECT_EQ(B.getParent(), A); 1071 // C is the second child in A, so we need to make sure we can backup across 1072 // other DIE (B) at the same level to get the correct parent. 1073 EXPECT_EQ(C.getParent(), A); 1074 // D is the third child of A. We need to verify we can backup across other DIE 1075 // (B and C) including DIE that have children (D) to get the correct parent. 1076 EXPECT_EQ(D.getParent(), A); 1077 1078 // Verify that a DIE with no children returns an invalid DWARFDie. 1079 EXPECT_FALSE(B.getFirstChild().isValid()); 1080 1081 // Verify the children of the B DIE 1082 auto C1 = C.getFirstChild(); 1083 auto C2 = C1.getSibling(); 1084 EXPECT_TRUE(C2.getSibling().isNULL()); 1085 1086 // Verify all children of the B DIE correctly valid or invalid. 1087 EXPECT_EQ(C1.getTag(), (dwarf::Tag)Tag::C1); 1088 EXPECT_EQ(C2.getTag(), (dwarf::Tag)Tag::C2); 1089 1090 // Make sure the parent of all the children of the B are the B. 1091 EXPECT_EQ(C1.getParent(), C); 1092 EXPECT_EQ(C2.getParent(), C); 1093 } 1094 1095 TEST(DWARFDebugInfo, TestDWARFDie) { 1096 // Make sure a default constructed DWARFDie doesn't have any parent, sibling 1097 // or child; 1098 DWARFDie DefaultDie; 1099 EXPECT_FALSE(DefaultDie.getParent().isValid()); 1100 EXPECT_FALSE(DefaultDie.getFirstChild().isValid()); 1101 EXPECT_FALSE(DefaultDie.getSibling().isValid()); 1102 } 1103 1104 TEST(DWARFDebugInfo, TestChildIterators) { 1105 // Test the DWARF APIs related to iterating across the children of a DIE using 1106 // the DWARFDie::iterator class. 1107 uint16_t Version = 4; 1108 1109 const uint8_t AddrSize = sizeof(void *); 1110 initLLVMIfNeeded(); 1111 Triple Triple = getHostTripleForAddrSize(AddrSize); 1112 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1113 if (HandleExpectedError(ExpectedDG)) 1114 return; 1115 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1116 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1117 1118 enum class Tag: uint16_t { 1119 A = dwarf::DW_TAG_lo_user, 1120 B, 1121 }; 1122 1123 // Scope to allow us to re-use the same DIE names 1124 { 1125 // Create DWARF tree that looks like: 1126 // 1127 // CU 1128 // A 1129 // B 1130 auto CUDie = CU.getUnitDIE(); 1131 CUDie.addChild((dwarf::Tag)Tag::A); 1132 CUDie.addChild((dwarf::Tag)Tag::B); 1133 } 1134 1135 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1136 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1137 EXPECT_TRUE((bool)Obj); 1138 DWARFContextInMemory DwarfContext(*Obj.get()); 1139 1140 // Verify the number of compile units is correct. 1141 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 1142 EXPECT_EQ(NumCUs, 1u); 1143 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1144 1145 // Get the compile unit DIE is valid. 1146 auto CUDie = U->getUnitDIE(false); 1147 EXPECT_TRUE(CUDie.isValid()); 1148 uint32_t Index; 1149 DWARFDie A; 1150 DWARFDie B; 1151 1152 // Verify the compile unit DIE's children. 1153 Index = 0; 1154 for (auto Die : CUDie.children()) { 1155 switch (Index++) { 1156 case 0: A = Die; break; 1157 case 1: B = Die; break; 1158 } 1159 } 1160 1161 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A); 1162 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B); 1163 1164 // Verify that A has no children by verifying that the begin and end contain 1165 // invalid DIEs and also that the iterators are equal. 1166 EXPECT_EQ(A.begin(), A.end()); 1167 } 1168 1169 TEST(DWARFDebugInfo, TestChildIteratorsOnInvalidDie) { 1170 // Verify that an invalid DIE has no children. 1171 DWARFDie Invalid; 1172 auto begin = Invalid.begin(); 1173 auto end = Invalid.end(); 1174 EXPECT_FALSE(begin->isValid()); 1175 EXPECT_FALSE(end->isValid()); 1176 EXPECT_EQ(begin, end); 1177 } 1178 1179 TEST(DWARFDebugInfo, TestEmptyChildren) { 1180 const char *yamldata = "debug_abbrev:\n" 1181 " - Code: 0x00000001\n" 1182 " Tag: DW_TAG_compile_unit\n" 1183 " Children: DW_CHILDREN_yes\n" 1184 " Attributes:\n" 1185 "debug_info:\n" 1186 " - Length:\n" 1187 " TotalLength: 9\n" 1188 " Version: 4\n" 1189 " AbbrOffset: 0\n" 1190 " AddrSize: 8\n" 1191 " Entries:\n" 1192 " - AbbrCode: 0x00000001\n" 1193 " Values:\n" 1194 " - AbbrCode: 0x00000000\n" 1195 " Values:\n"; 1196 1197 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata)); 1198 ASSERT_TRUE((bool)ErrOrSections); 1199 DWARFContextInMemory DwarfContext(*ErrOrSections, 8); 1200 1201 // Verify the number of compile units is correct. 1202 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 1203 EXPECT_EQ(NumCUs, 1u); 1204 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1205 1206 // Get the compile unit DIE is valid. 1207 auto CUDie = U->getUnitDIE(false); 1208 EXPECT_TRUE(CUDie.isValid()); 1209 1210 // Verify that the CU Die that says it has children, but doesn't, actually 1211 // has begin and end iterators that are equal. We want to make sure we don't 1212 // see the Null DIEs during iteration. 1213 EXPECT_EQ(CUDie.begin(), CUDie.end()); 1214 } 1215 1216 TEST(DWARFDebugInfo, TestAttributeIterators) { 1217 // Test the DWARF APIs related to iterating across all attribute values in a 1218 // a DWARFDie. 1219 uint16_t Version = 4; 1220 1221 const uint8_t AddrSize = sizeof(void *); 1222 initLLVMIfNeeded(); 1223 Triple Triple = getHostTripleForAddrSize(AddrSize); 1224 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1225 if (HandleExpectedError(ExpectedDG)) 1226 return; 1227 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1228 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1229 const uint64_t CULowPC = 0x1000; 1230 StringRef CUPath("/tmp/main.c"); 1231 1232 // Scope to allow us to re-use the same DIE names 1233 { 1234 auto CUDie = CU.getUnitDIE(); 1235 // Encode an attribute value before an attribute with no data. 1236 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, CUPath.data()); 1237 // Encode an attribute value with no data in .debug_info/types to ensure 1238 // the iteration works correctly. 1239 CUDie.addAttribute(DW_AT_declaration, DW_FORM_flag_present); 1240 // Encode an attribute value after an attribute with no data. 1241 CUDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, CULowPC); 1242 } 1243 1244 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1245 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1246 EXPECT_TRUE((bool)Obj); 1247 DWARFContextInMemory DwarfContext(*Obj.get()); 1248 1249 // Verify the number of compile units is correct. 1250 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 1251 EXPECT_EQ(NumCUs, 1u); 1252 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1253 1254 // Get the compile unit DIE is valid. 1255 auto CUDie = U->getUnitDIE(false); 1256 EXPECT_TRUE(CUDie.isValid()); 1257 1258 auto R = CUDie.attributes(); 1259 auto I = R.begin(); 1260 auto E = R.end(); 1261 1262 ASSERT_NE(E, I); 1263 EXPECT_EQ(I->Attr, DW_AT_name); 1264 auto ActualCUPath = I->Value.getAsCString(); 1265 EXPECT_EQ(CUPath, *ActualCUPath); 1266 1267 ASSERT_NE(E, ++I); 1268 EXPECT_EQ(I->Attr, DW_AT_declaration); 1269 EXPECT_EQ(1ull, *I->Value.getAsUnsignedConstant()); 1270 1271 ASSERT_NE(E, ++I); 1272 EXPECT_EQ(I->Attr, DW_AT_low_pc); 1273 EXPECT_EQ(CULowPC, *I->Value.getAsAddress()); 1274 1275 EXPECT_EQ(E, ++I); 1276 } 1277 1278 TEST(DWARFDebugInfo, TestFindRecurse) { 1279 uint16_t Version = 4; 1280 1281 const uint8_t AddrSize = sizeof(void *); 1282 initLLVMIfNeeded(); 1283 Triple Triple = getHostTripleForAddrSize(AddrSize); 1284 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1285 if (HandleExpectedError(ExpectedDG)) 1286 return; 1287 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1288 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1289 1290 StringRef SpecDieName = "spec"; 1291 StringRef SpecLinkageName = "spec_linkage"; 1292 StringRef AbsDieName = "abs"; 1293 // Scope to allow us to re-use the same DIE names 1294 { 1295 auto CUDie = CU.getUnitDIE(); 1296 auto FuncSpecDie = CUDie.addChild(DW_TAG_subprogram); 1297 auto FuncAbsDie = CUDie.addChild(DW_TAG_subprogram); 1298 auto FuncDie = CUDie.addChild(DW_TAG_subprogram); 1299 auto VarAbsDie = CUDie.addChild(DW_TAG_variable); 1300 auto VarDie = CUDie.addChild(DW_TAG_variable); 1301 FuncSpecDie.addAttribute(DW_AT_name, DW_FORM_strp, SpecDieName); 1302 FuncAbsDie.addAttribute(DW_AT_linkage_name, DW_FORM_strp, SpecLinkageName); 1303 FuncAbsDie.addAttribute(DW_AT_specification, DW_FORM_ref4, FuncSpecDie); 1304 FuncDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, FuncAbsDie); 1305 VarAbsDie.addAttribute(DW_AT_name, DW_FORM_strp, AbsDieName); 1306 VarDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, VarAbsDie); 1307 } 1308 1309 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1310 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1311 EXPECT_TRUE((bool)Obj); 1312 DWARFContextInMemory DwarfContext(*Obj.get()); 1313 1314 // Verify the number of compile units is correct. 1315 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 1316 EXPECT_EQ(NumCUs, 1u); 1317 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1318 1319 // Get the compile unit DIE is valid. 1320 auto CUDie = U->getUnitDIE(false); 1321 EXPECT_TRUE(CUDie.isValid()); 1322 1323 auto FuncSpecDie = CUDie.getFirstChild(); 1324 auto FuncAbsDie = FuncSpecDie.getSibling(); 1325 auto FuncDie = FuncAbsDie.getSibling(); 1326 auto VarAbsDie = FuncDie.getSibling(); 1327 auto VarDie = VarAbsDie.getSibling(); 1328 1329 // Make sure we can't extract the name from the specification die when using 1330 // DWARFDie::find() since it won't check the DW_AT_specification DIE. 1331 EXPECT_FALSE(FuncDie.find(DW_AT_name)); 1332 1333 // Make sure we can extract the name from the specification die when using 1334 // DWARFDie::findRecursively() since it should recurse through the 1335 // DW_AT_specification DIE. 1336 auto NameOpt = FuncDie.findRecursively(DW_AT_name); 1337 EXPECT_TRUE(NameOpt); 1338 // Test the dwarf::toString() helper function. 1339 auto StringOpt = toString(NameOpt); 1340 EXPECT_TRUE(StringOpt); 1341 EXPECT_EQ(SpecDieName, StringOpt.getValueOr(nullptr)); 1342 // Test the dwarf::toString() helper function with a default value specified. 1343 EXPECT_EQ(SpecDieName, toString(NameOpt, nullptr)); 1344 1345 auto LinkageNameOpt = FuncDie.findRecursively(DW_AT_linkage_name); 1346 EXPECT_EQ(SpecLinkageName, toString(LinkageNameOpt).getValueOr(nullptr)); 1347 1348 // Make sure we can't extract the name from the abstract origin die when using 1349 // DWARFDie::find() since it won't check the DW_AT_abstract_origin DIE. 1350 EXPECT_FALSE(VarDie.find(DW_AT_name)); 1351 1352 // Make sure we can extract the name from the abstract origin die when using 1353 // DWARFDie::findRecursively() since it should recurse through the 1354 // DW_AT_abstract_origin DIE. 1355 NameOpt = VarDie.findRecursively(DW_AT_name); 1356 EXPECT_TRUE(NameOpt); 1357 // Test the dwarf::toString() helper function. 1358 StringOpt = toString(NameOpt); 1359 EXPECT_TRUE(StringOpt); 1360 EXPECT_EQ(AbsDieName, StringOpt.getValueOr(nullptr)); 1361 } 1362 1363 TEST(DWARFDebugInfo, TestDwarfToFunctions) { 1364 // Test all of the dwarf::toXXX functions that take a 1365 // Optional<DWARFFormValue> and extract the values from it. 1366 DWARFFormValue FormVal; 1367 uint64_t InvalidU64 = 0xBADBADBADBADBADB; 1368 int64_t InvalidS64 = 0xBADBADBADBADBADB; 1369 // First test that we don't get valid values back when using an optional with 1370 // no value. 1371 Optional<DWARFFormValue> FormValOpt; 1372 EXPECT_FALSE(toString(FormValOpt).hasValue()); 1373 EXPECT_FALSE(toUnsigned(FormValOpt).hasValue()); 1374 EXPECT_FALSE(toReference(FormValOpt).hasValue()); 1375 EXPECT_FALSE(toSigned(FormValOpt).hasValue()); 1376 EXPECT_FALSE(toAddress(FormValOpt).hasValue()); 1377 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue()); 1378 EXPECT_FALSE(toBlock(FormValOpt).hasValue()); 1379 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr)); 1380 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt, InvalidU64)); 1381 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64)); 1382 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64)); 1383 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64)); 1384 EXPECT_EQ(InvalidS64, toSigned(FormValOpt, InvalidS64)); 1385 1386 // Test successful and unsuccessful address decoding. 1387 uint64_t Address = 0x100000000ULL; 1388 FormVal.setForm(DW_FORM_addr); 1389 FormVal.setUValue(Address); 1390 FormValOpt = FormVal; 1391 1392 EXPECT_FALSE(toString(FormValOpt).hasValue()); 1393 EXPECT_FALSE(toUnsigned(FormValOpt).hasValue()); 1394 EXPECT_FALSE(toReference(FormValOpt).hasValue()); 1395 EXPECT_FALSE(toSigned(FormValOpt).hasValue()); 1396 EXPECT_TRUE(toAddress(FormValOpt).hasValue()); 1397 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue()); 1398 EXPECT_FALSE(toBlock(FormValOpt).hasValue()); 1399 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr)); 1400 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt, InvalidU64)); 1401 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64)); 1402 EXPECT_EQ(Address, toAddress(FormValOpt, InvalidU64)); 1403 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64)); 1404 EXPECT_EQ(InvalidS64, toSigned(FormValOpt, InvalidU64)); 1405 1406 // Test successful and unsuccessful unsigned constant decoding. 1407 uint64_t UData8 = 0x1020304050607080ULL; 1408 FormVal.setForm(DW_FORM_udata); 1409 FormVal.setUValue(UData8); 1410 FormValOpt = FormVal; 1411 1412 EXPECT_FALSE(toString(FormValOpt).hasValue()); 1413 EXPECT_TRUE(toUnsigned(FormValOpt).hasValue()); 1414 EXPECT_FALSE(toReference(FormValOpt).hasValue()); 1415 EXPECT_TRUE(toSigned(FormValOpt).hasValue()); 1416 EXPECT_FALSE(toAddress(FormValOpt).hasValue()); 1417 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue()); 1418 EXPECT_FALSE(toBlock(FormValOpt).hasValue()); 1419 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr)); 1420 EXPECT_EQ(UData8, toUnsigned(FormValOpt, InvalidU64)); 1421 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64)); 1422 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64)); 1423 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64)); 1424 EXPECT_EQ((int64_t)UData8, toSigned(FormValOpt, InvalidU64)); 1425 1426 // Test successful and unsuccessful reference decoding. 1427 uint32_t RefData = 0x11223344U; 1428 FormVal.setForm(DW_FORM_ref_addr); 1429 FormVal.setUValue(RefData); 1430 FormValOpt = FormVal; 1431 1432 EXPECT_FALSE(toString(FormValOpt).hasValue()); 1433 EXPECT_FALSE(toUnsigned(FormValOpt).hasValue()); 1434 EXPECT_TRUE(toReference(FormValOpt).hasValue()); 1435 EXPECT_FALSE(toSigned(FormValOpt).hasValue()); 1436 EXPECT_FALSE(toAddress(FormValOpt).hasValue()); 1437 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue()); 1438 EXPECT_FALSE(toBlock(FormValOpt).hasValue()); 1439 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr)); 1440 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt, InvalidU64)); 1441 EXPECT_EQ(RefData, toReference(FormValOpt, InvalidU64)); 1442 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64)); 1443 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64)); 1444 EXPECT_EQ(InvalidS64, toSigned(FormValOpt, InvalidU64)); 1445 1446 // Test successful and unsuccessful signed constant decoding. 1447 int64_t SData8 = 0x1020304050607080ULL; 1448 FormVal.setForm(DW_FORM_udata); 1449 FormVal.setSValue(SData8); 1450 FormValOpt = FormVal; 1451 1452 EXPECT_FALSE(toString(FormValOpt).hasValue()); 1453 EXPECT_TRUE(toUnsigned(FormValOpt).hasValue()); 1454 EXPECT_FALSE(toReference(FormValOpt).hasValue()); 1455 EXPECT_TRUE(toSigned(FormValOpt).hasValue()); 1456 EXPECT_FALSE(toAddress(FormValOpt).hasValue()); 1457 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue()); 1458 EXPECT_FALSE(toBlock(FormValOpt).hasValue()); 1459 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr)); 1460 EXPECT_EQ((uint64_t)SData8, toUnsigned(FormValOpt, InvalidU64)); 1461 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64)); 1462 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64)); 1463 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64)); 1464 EXPECT_EQ(SData8, toSigned(FormValOpt, InvalidU64)); 1465 1466 // Test successful and unsuccessful block decoding. 1467 uint8_t Data[] = { 2, 3, 4 }; 1468 ArrayRef<uint8_t> Array(Data); 1469 FormVal.setForm(DW_FORM_block1); 1470 FormVal.setBlockValue(Array); 1471 FormValOpt = FormVal; 1472 1473 EXPECT_FALSE(toString(FormValOpt).hasValue()); 1474 EXPECT_FALSE(toUnsigned(FormValOpt).hasValue()); 1475 EXPECT_FALSE(toReference(FormValOpt).hasValue()); 1476 EXPECT_FALSE(toSigned(FormValOpt).hasValue()); 1477 EXPECT_FALSE(toAddress(FormValOpt).hasValue()); 1478 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue()); 1479 auto BlockOpt = toBlock(FormValOpt); 1480 EXPECT_TRUE(BlockOpt.hasValue()); 1481 EXPECT_EQ(*BlockOpt, Array); 1482 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr)); 1483 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt, InvalidU64)); 1484 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64)); 1485 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64)); 1486 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64)); 1487 EXPECT_EQ(InvalidS64, toSigned(FormValOpt, InvalidU64)); 1488 1489 // Test 1490 } 1491 1492 TEST(DWARFDebugInfo, TestFindAttrs) { 1493 // Test the DWARFDie::find() and DWARFDie::findRecursively() that take an 1494 // ArrayRef<dwarf::Attribute> value to make sure they work correctly. 1495 uint16_t Version = 4; 1496 1497 const uint8_t AddrSize = sizeof(void *); 1498 initLLVMIfNeeded(); 1499 Triple Triple = getHostTripleForAddrSize(AddrSize); 1500 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1501 if (HandleExpectedError(ExpectedDG)) 1502 return; 1503 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1504 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1505 1506 StringRef DieMangled("_Z3fooi"); 1507 // Scope to allow us to re-use the same DIE names 1508 { 1509 auto CUDie = CU.getUnitDIE(); 1510 auto FuncSpecDie = CUDie.addChild(DW_TAG_subprogram); 1511 auto FuncDie = CUDie.addChild(DW_TAG_subprogram); 1512 FuncSpecDie.addAttribute(DW_AT_MIPS_linkage_name, DW_FORM_strp, DieMangled); 1513 FuncDie.addAttribute(DW_AT_specification, DW_FORM_ref4, FuncSpecDie); 1514 } 1515 1516 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1517 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1518 EXPECT_TRUE((bool)Obj); 1519 DWARFContextInMemory DwarfContext(*Obj.get()); 1520 1521 // Verify the number of compile units is correct. 1522 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 1523 EXPECT_EQ(NumCUs, 1u); 1524 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1525 1526 // Get the compile unit DIE is valid. 1527 auto CUDie = U->getUnitDIE(false); 1528 EXPECT_TRUE(CUDie.isValid()); 1529 1530 auto FuncSpecDie = CUDie.getFirstChild(); 1531 auto FuncDie = FuncSpecDie.getSibling(); 1532 1533 // Make sure that passing in an empty attribute list behave correctly. 1534 EXPECT_FALSE(FuncDie.find(ArrayRef<dwarf::Attribute>()).hasValue()); 1535 1536 // Make sure that passing in a list of attribute that are not contained 1537 // in the DIE returns nothing. 1538 EXPECT_FALSE(FuncDie.find({DW_AT_low_pc, DW_AT_entry_pc}).hasValue()); 1539 1540 const dwarf::Attribute Attrs[] = {DW_AT_linkage_name, 1541 DW_AT_MIPS_linkage_name}; 1542 1543 // Make sure we can't extract the linkage name attributes when using 1544 // DWARFDie::find() since it won't check the DW_AT_specification DIE. 1545 EXPECT_FALSE(FuncDie.find(Attrs).hasValue()); 1546 1547 // Make sure we can extract the name from the specification die when using 1548 // DWARFDie::findRecursively() since it should recurse through the 1549 // DW_AT_specification DIE. 1550 auto NameOpt = FuncDie.findRecursively(Attrs); 1551 EXPECT_TRUE(NameOpt.hasValue()); 1552 EXPECT_EQ(DieMangled, toString(NameOpt, "")); 1553 } 1554 1555 TEST(DWARFDebugInfo, TestImplicitConstAbbrevs) { 1556 uint16_t Version = 5; 1557 1558 const uint8_t AddrSize = sizeof(void *); 1559 initLLVMIfNeeded(); 1560 Triple Triple = getHostTripleForAddrSize(AddrSize); 1561 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1562 if (HandleExpectedError(ExpectedDG)) 1563 return; 1564 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1565 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1566 dwarfgen::DIE CUDie = CU.getUnitDIE(); 1567 const dwarf::Attribute Attr = DW_AT_lo_user; 1568 const int64_t Val1 = 42; 1569 const int64_t Val2 = 43; 1570 1571 auto FirstVal1DIE = CUDie.addChild(DW_TAG_class_type); 1572 FirstVal1DIE.addAttribute(Attr, DW_FORM_implicit_const, Val1); 1573 1574 auto SecondVal1DIE = CUDie.addChild(DW_TAG_class_type); 1575 SecondVal1DIE.addAttribute(Attr, DW_FORM_implicit_const, Val1); 1576 1577 auto Val2DIE = CUDie.addChild(DW_TAG_class_type); 1578 Val2DIE.addAttribute(Attr, DW_FORM_implicit_const, Val2); 1579 1580 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1581 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1582 EXPECT_TRUE((bool)Obj); 1583 DWARFContextInMemory DwarfContext(*Obj.get()); 1584 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1585 EXPECT_TRUE((bool)U); 1586 1587 const auto *Abbrevs = U->getAbbreviations(); 1588 EXPECT_TRUE((bool)Abbrevs); 1589 1590 // Let's find implicit_const abbrevs and verify, 1591 // that there are exactly two of them and both of them 1592 // can be dumped correctly. 1593 typedef decltype(Abbrevs->begin()) AbbrevIt; 1594 AbbrevIt Val1Abbrev = Abbrevs->end(); 1595 AbbrevIt Val2Abbrev = Abbrevs->end(); 1596 for(auto it = Abbrevs->begin(); it != Abbrevs->end(); ++it) { 1597 if (it->getNumAttributes() == 0) 1598 continue; // root abbrev for DW_TAG_compile_unit 1599 1600 auto A = it->getAttrByIndex(0); 1601 EXPECT_EQ(A, Attr); 1602 1603 auto FormValue = it->getAttributeValue(/* offset */ 0, A, *U); 1604 EXPECT_TRUE((bool)FormValue); 1605 EXPECT_EQ(FormValue->getForm(), dwarf::DW_FORM_implicit_const); 1606 1607 const auto V = FormValue->getAsSignedConstant(); 1608 EXPECT_TRUE((bool)V); 1609 1610 auto VerifyAbbrevDump = [&V](AbbrevIt it) { 1611 std::string S; 1612 llvm::raw_string_ostream OS(S); 1613 it->dump(OS); 1614 auto FormPos = OS.str().find("DW_FORM_implicit_const"); 1615 EXPECT_NE(FormPos, std::string::npos); 1616 auto ValPos = S.find_first_of("-0123456789", FormPos); 1617 EXPECT_NE(ValPos, std::string::npos); 1618 int64_t Val = std::atoll(S.substr(ValPos).c_str()); 1619 EXPECT_EQ(Val, *V); 1620 }; 1621 1622 switch(*V) { 1623 case Val1: 1624 EXPECT_EQ(Val1Abbrev, Abbrevs->end()); 1625 Val1Abbrev = it; 1626 VerifyAbbrevDump(it); 1627 break; 1628 case Val2: 1629 EXPECT_EQ(Val2Abbrev, Abbrevs->end()); 1630 Val2Abbrev = it; 1631 VerifyAbbrevDump(it); 1632 break; 1633 default: 1634 FAIL() << "Unexpected attribute value: " << *V; 1635 } 1636 } 1637 1638 // Now let's make sure that two Val1-DIEs refer to the same abbrev, 1639 // and Val2-DIE refers to another one. 1640 auto DieDG = U->getUnitDIE(false); 1641 auto it = DieDG.begin(); 1642 std::multimap<int64_t, decltype(it->getAbbreviationDeclarationPtr())> DIEs; 1643 const DWARFAbbreviationDeclaration *AbbrevPtrVal1 = nullptr; 1644 const DWARFAbbreviationDeclaration *AbbrevPtrVal2 = nullptr; 1645 for (; it != DieDG.end(); ++it) { 1646 const auto *AbbrevPtr = it->getAbbreviationDeclarationPtr(); 1647 EXPECT_TRUE((bool)AbbrevPtr); 1648 auto FormValue = it->find(Attr); 1649 EXPECT_TRUE((bool)FormValue); 1650 const auto V = FormValue->getAsSignedConstant(); 1651 EXPECT_TRUE((bool)V); 1652 switch(*V) { 1653 case Val1: 1654 AbbrevPtrVal1 = AbbrevPtr; 1655 break; 1656 case Val2: 1657 AbbrevPtrVal2 = AbbrevPtr; 1658 break; 1659 default: 1660 FAIL() << "Unexpected attribute value: " << *V; 1661 } 1662 DIEs.insert(std::make_pair(*V, AbbrevPtr)); 1663 } 1664 EXPECT_EQ(DIEs.count(Val1), 2u); 1665 EXPECT_EQ(DIEs.count(Val2), 1u); 1666 auto Val1Range = DIEs.equal_range(Val1); 1667 for (auto it = Val1Range.first; it != Val1Range.second; ++it) 1668 EXPECT_EQ(it->second, AbbrevPtrVal1); 1669 EXPECT_EQ(DIEs.find(Val2)->second, AbbrevPtrVal2); 1670 } 1671 1672 void VerifyError(DWARFContext &DwarfContext, StringRef Error) { 1673 SmallString<1024> Str; 1674 raw_svector_ostream Strm(Str); 1675 EXPECT_FALSE(DwarfContext.verify(Strm, DIDT_All)); 1676 EXPECT_TRUE(Str.str().contains(Error)); 1677 } 1678 1679 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidCURef) { 1680 // Create a single compile unit with a single function that has a DW_AT_type 1681 // that is CU relative. The CU offset is not valid becuase it is larger than 1682 // the compile unit itself. 1683 1684 const char *yamldata = R"( 1685 debug_str: 1686 - '' 1687 - /tmp/main.c 1688 - main 1689 debug_abbrev: 1690 - Code: 0x00000001 1691 Tag: DW_TAG_compile_unit 1692 Children: DW_CHILDREN_yes 1693 Attributes: 1694 - Attribute: DW_AT_name 1695 Form: DW_FORM_strp 1696 - Code: 0x00000002 1697 Tag: DW_TAG_subprogram 1698 Children: DW_CHILDREN_no 1699 Attributes: 1700 - Attribute: DW_AT_name 1701 Form: DW_FORM_strp 1702 - Attribute: DW_AT_type 1703 Form: DW_FORM_ref4 1704 debug_info: 1705 - Length: 1706 TotalLength: 22 1707 Version: 4 1708 AbbrOffset: 0 1709 AddrSize: 8 1710 Entries: 1711 - AbbrCode: 0x00000001 1712 Values: 1713 - Value: 0x0000000000000001 1714 - AbbrCode: 0x00000002 1715 Values: 1716 - Value: 0x000000000000000D 1717 - Value: 0x0000000000001234 1718 - AbbrCode: 0x00000000 1719 Values: 1720 )"; 1721 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata)); 1722 ASSERT_TRUE((bool)ErrOrSections); 1723 DWARFContextInMemory DwarfContext(*ErrOrSections, 8); 1724 VerifyError(DwarfContext, "error: DW_FORM_ref4 CU offset 0x00001234 is " 1725 "invalid (must be less than CU size of " 1726 "0x0000001a):"); 1727 } 1728 1729 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidRefAddr) { 1730 // Create a single compile unit with a single function that has an invalid 1731 // DW_AT_type with an invalid .debug_info offset in its DW_FORM_ref_addr. 1732 const char *yamldata = R"( 1733 debug_str: 1734 - '' 1735 - /tmp/main.c 1736 - main 1737 debug_abbrev: 1738 - Code: 0x00000001 1739 Tag: DW_TAG_compile_unit 1740 Children: DW_CHILDREN_yes 1741 Attributes: 1742 - Attribute: DW_AT_name 1743 Form: DW_FORM_strp 1744 - Code: 0x00000002 1745 Tag: DW_TAG_subprogram 1746 Children: DW_CHILDREN_no 1747 Attributes: 1748 - Attribute: DW_AT_name 1749 Form: DW_FORM_strp 1750 - Attribute: DW_AT_type 1751 Form: DW_FORM_ref_addr 1752 debug_info: 1753 - Length: 1754 TotalLength: 22 1755 Version: 4 1756 AbbrOffset: 0 1757 AddrSize: 8 1758 Entries: 1759 - AbbrCode: 0x00000001 1760 Values: 1761 - Value: 0x0000000000000001 1762 - AbbrCode: 0x00000002 1763 Values: 1764 - Value: 0x000000000000000D 1765 - Value: 0x0000000000001234 1766 - AbbrCode: 0x00000000 1767 Values: 1768 )"; 1769 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata)); 1770 ASSERT_TRUE((bool)ErrOrSections); 1771 DWARFContextInMemory DwarfContext(*ErrOrSections, 8); 1772 VerifyError(DwarfContext, 1773 "error: DW_FORM_ref_addr offset beyond .debug_info bounds:"); 1774 } 1775 1776 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidRanges) { 1777 // Create a single compile unit with a DW_AT_ranges whose section offset 1778 // isn't valid. 1779 const char *yamldata = R"( 1780 debug_str: 1781 - '' 1782 - /tmp/main.c 1783 debug_abbrev: 1784 - Code: 0x00000001 1785 Tag: DW_TAG_compile_unit 1786 Children: DW_CHILDREN_no 1787 Attributes: 1788 - Attribute: DW_AT_name 1789 Form: DW_FORM_strp 1790 - Attribute: DW_AT_ranges 1791 Form: DW_FORM_sec_offset 1792 debug_info: 1793 - Length: 1794 TotalLength: 16 1795 Version: 4 1796 AbbrOffset: 0 1797 AddrSize: 8 1798 Entries: 1799 - AbbrCode: 0x00000001 1800 Values: 1801 - Value: 0x0000000000000001 1802 - Value: 0x0000000000001000 1803 1804 )"; 1805 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata)); 1806 ASSERT_TRUE((bool)ErrOrSections); 1807 DWARFContextInMemory DwarfContext(*ErrOrSections, 8); 1808 VerifyError(DwarfContext, 1809 "error: DW_AT_ranges offset is beyond .debug_ranges bounds:"); 1810 } 1811 1812 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidStmtList) { 1813 // Create a single compile unit with a DW_AT_stmt_list whose section offset 1814 // isn't valid. 1815 const char *yamldata = R"( 1816 debug_str: 1817 - '' 1818 - /tmp/main.c 1819 debug_abbrev: 1820 - Code: 0x00000001 1821 Tag: DW_TAG_compile_unit 1822 Children: DW_CHILDREN_no 1823 Attributes: 1824 - Attribute: DW_AT_name 1825 Form: DW_FORM_strp 1826 - Attribute: DW_AT_stmt_list 1827 Form: DW_FORM_sec_offset 1828 debug_info: 1829 - Length: 1830 TotalLength: 16 1831 Version: 4 1832 AbbrOffset: 0 1833 AddrSize: 8 1834 Entries: 1835 - AbbrCode: 0x00000001 1836 Values: 1837 - Value: 0x0000000000000001 1838 - Value: 0x0000000000001000 1839 1840 )"; 1841 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata)); 1842 ASSERT_TRUE((bool)ErrOrSections); 1843 DWARFContextInMemory DwarfContext(*ErrOrSections, 8); 1844 VerifyError( 1845 DwarfContext, 1846 "error: DW_AT_stmt_list offset is beyond .debug_line bounds: 0x00001000"); 1847 } 1848 1849 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidStrp) { 1850 // Create a single compile unit with a single function that has an invalid 1851 // DW_FORM_strp for the DW_AT_name. 1852 const char *yamldata = R"( 1853 debug_str: 1854 - '' 1855 debug_abbrev: 1856 - Code: 0x00000001 1857 Tag: DW_TAG_compile_unit 1858 Children: DW_CHILDREN_no 1859 Attributes: 1860 - Attribute: DW_AT_name 1861 Form: DW_FORM_strp 1862 debug_info: 1863 - Length: 1864 TotalLength: 12 1865 Version: 4 1866 AbbrOffset: 0 1867 AddrSize: 8 1868 Entries: 1869 - AbbrCode: 0x00000001 1870 Values: 1871 - Value: 0x0000000000001234 1872 )"; 1873 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata)); 1874 ASSERT_TRUE((bool)ErrOrSections); 1875 DWARFContextInMemory DwarfContext(*ErrOrSections, 8); 1876 VerifyError(DwarfContext, 1877 "error: DW_FORM_strp offset beyond .debug_str bounds:"); 1878 } 1879 1880 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidRefAddrBetween) { 1881 // Create a single compile unit with a single function that has a DW_AT_type 1882 // with a valid .debug_info offset, but the offset is between two DIEs. 1883 const char *yamldata = R"( 1884 debug_str: 1885 - '' 1886 - /tmp/main.c 1887 - main 1888 debug_abbrev: 1889 - Code: 0x00000001 1890 Tag: DW_TAG_compile_unit 1891 Children: DW_CHILDREN_yes 1892 Attributes: 1893 - Attribute: DW_AT_name 1894 Form: DW_FORM_strp 1895 - Code: 0x00000002 1896 Tag: DW_TAG_subprogram 1897 Children: DW_CHILDREN_no 1898 Attributes: 1899 - Attribute: DW_AT_name 1900 Form: DW_FORM_strp 1901 - Attribute: DW_AT_type 1902 Form: DW_FORM_ref_addr 1903 debug_info: 1904 - Length: 1905 TotalLength: 22 1906 Version: 4 1907 AbbrOffset: 0 1908 AddrSize: 8 1909 Entries: 1910 - AbbrCode: 0x00000001 1911 Values: 1912 - Value: 0x0000000000000001 1913 - AbbrCode: 0x00000002 1914 Values: 1915 - Value: 0x000000000000000D 1916 - Value: 0x0000000000000011 1917 - AbbrCode: 0x00000000 1918 Values: 1919 )"; 1920 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata)); 1921 ASSERT_TRUE((bool)ErrOrSections); 1922 DWARFContextInMemory DwarfContext(*ErrOrSections, 8); 1923 VerifyError( 1924 DwarfContext, 1925 "error: invalid DIE reference 0x00000011. Offset is in between DIEs:"); 1926 } 1927 1928 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidLineSequence) { 1929 // Create a single compile unit whose line table has a sequence in it where 1930 // the address decreases. 1931 StringRef yamldata = R"( 1932 debug_str: 1933 - '' 1934 - /tmp/main.c 1935 debug_abbrev: 1936 - Code: 0x00000001 1937 Tag: DW_TAG_compile_unit 1938 Children: DW_CHILDREN_no 1939 Attributes: 1940 - Attribute: DW_AT_name 1941 Form: DW_FORM_strp 1942 - Attribute: DW_AT_stmt_list 1943 Form: DW_FORM_sec_offset 1944 debug_info: 1945 - Length: 1946 TotalLength: 16 1947 Version: 4 1948 AbbrOffset: 0 1949 AddrSize: 8 1950 Entries: 1951 - AbbrCode: 0x00000001 1952 Values: 1953 - Value: 0x0000000000000001 1954 - Value: 0x0000000000000000 1955 debug_line: 1956 - Length: 1957 TotalLength: 68 1958 Version: 2 1959 PrologueLength: 34 1960 MinInstLength: 1 1961 DefaultIsStmt: 1 1962 LineBase: 251 1963 LineRange: 14 1964 OpcodeBase: 13 1965 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ] 1966 IncludeDirs: 1967 - /tmp 1968 Files: 1969 - Name: main.c 1970 DirIdx: 1 1971 ModTime: 0 1972 Length: 0 1973 Opcodes: 1974 - Opcode: DW_LNS_extended_op 1975 ExtLen: 9 1976 SubOpcode: DW_LNE_set_address 1977 Data: 4112 1978 - Opcode: DW_LNS_advance_line 1979 SData: 9 1980 Data: 4112 1981 - Opcode: DW_LNS_copy 1982 Data: 4112 1983 - Opcode: DW_LNS_advance_pc 1984 Data: 18446744073709551600 1985 - Opcode: DW_LNS_extended_op 1986 ExtLen: 1 1987 SubOpcode: DW_LNE_end_sequence 1988 Data: 18446744073709551600 1989 )"; 1990 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata); 1991 ASSERT_TRUE((bool)ErrOrSections); 1992 DWARFContextInMemory DwarfContext(*ErrOrSections, 8); 1993 VerifyError(DwarfContext, "error: .debug_line[0x00000000] row[1] decreases " 1994 "in address from previous row:"); 1995 } 1996 1997 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidLineFileIndex) { 1998 // Create a single compile unit whose line table has a line table row with 1999 // an invalid file index. 2000 StringRef yamldata = R"( 2001 debug_str: 2002 - '' 2003 - /tmp/main.c 2004 debug_abbrev: 2005 - Code: 0x00000001 2006 Tag: DW_TAG_compile_unit 2007 Children: DW_CHILDREN_no 2008 Attributes: 2009 - Attribute: DW_AT_name 2010 Form: DW_FORM_strp 2011 - Attribute: DW_AT_stmt_list 2012 Form: DW_FORM_sec_offset 2013 debug_info: 2014 - Length: 2015 TotalLength: 16 2016 Version: 4 2017 AbbrOffset: 0 2018 AddrSize: 8 2019 Entries: 2020 - AbbrCode: 0x00000001 2021 Values: 2022 - Value: 0x0000000000000001 2023 - Value: 0x0000000000000000 2024 debug_line: 2025 - Length: 2026 TotalLength: 61 2027 Version: 2 2028 PrologueLength: 34 2029 MinInstLength: 1 2030 DefaultIsStmt: 1 2031 LineBase: 251 2032 LineRange: 14 2033 OpcodeBase: 13 2034 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ] 2035 IncludeDirs: 2036 - /tmp 2037 Files: 2038 - Name: main.c 2039 DirIdx: 1 2040 ModTime: 0 2041 Length: 0 2042 Opcodes: 2043 - Opcode: DW_LNS_extended_op 2044 ExtLen: 9 2045 SubOpcode: DW_LNE_set_address 2046 Data: 4096 2047 - Opcode: DW_LNS_advance_line 2048 SData: 9 2049 Data: 4096 2050 - Opcode: DW_LNS_copy 2051 Data: 4096 2052 - Opcode: DW_LNS_advance_pc 2053 Data: 16 2054 - Opcode: DW_LNS_set_file 2055 Data: 5 2056 - Opcode: DW_LNS_extended_op 2057 ExtLen: 1 2058 SubOpcode: DW_LNE_end_sequence 2059 Data: 5 2060 )"; 2061 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata); 2062 ASSERT_TRUE((bool)ErrOrSections); 2063 DWARFContextInMemory DwarfContext(*ErrOrSections, 8); 2064 VerifyError(DwarfContext, "error: .debug_line[0x00000000][1] has invalid " 2065 "file index 5 (valid values are [1,1]):"); 2066 } 2067 2068 TEST(DWARFDebugInfo, TestDwarfVerifyCUDontShareLineTable) { 2069 // Create a two compile units where both compile units share the same 2070 // DW_AT_stmt_list value and verify we report the error correctly. 2071 StringRef yamldata = R"( 2072 debug_str: 2073 - '' 2074 - /tmp/main.c 2075 - /tmp/foo.c 2076 debug_abbrev: 2077 - Code: 0x00000001 2078 Tag: DW_TAG_compile_unit 2079 Children: DW_CHILDREN_no 2080 Attributes: 2081 - Attribute: DW_AT_name 2082 Form: DW_FORM_strp 2083 - Attribute: DW_AT_stmt_list 2084 Form: DW_FORM_sec_offset 2085 debug_info: 2086 - Length: 2087 TotalLength: 16 2088 Version: 4 2089 AbbrOffset: 0 2090 AddrSize: 8 2091 Entries: 2092 - AbbrCode: 0x00000001 2093 Values: 2094 - Value: 0x0000000000000001 2095 - Value: 0x0000000000000000 2096 - Length: 2097 TotalLength: 16 2098 Version: 4 2099 AbbrOffset: 0 2100 AddrSize: 8 2101 Entries: 2102 - AbbrCode: 0x00000001 2103 Values: 2104 - Value: 0x000000000000000D 2105 - Value: 0x0000000000000000 2106 debug_line: 2107 - Length: 2108 TotalLength: 60 2109 Version: 2 2110 PrologueLength: 34 2111 MinInstLength: 1 2112 DefaultIsStmt: 1 2113 LineBase: 251 2114 LineRange: 14 2115 OpcodeBase: 13 2116 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ] 2117 IncludeDirs: 2118 - /tmp 2119 Files: 2120 - Name: main.c 2121 DirIdx: 1 2122 ModTime: 0 2123 Length: 0 2124 Opcodes: 2125 - Opcode: DW_LNS_extended_op 2126 ExtLen: 9 2127 SubOpcode: DW_LNE_set_address 2128 Data: 4096 2129 - Opcode: DW_LNS_advance_line 2130 SData: 9 2131 Data: 4096 2132 - Opcode: DW_LNS_copy 2133 Data: 4096 2134 - Opcode: DW_LNS_advance_pc 2135 Data: 256 2136 - Opcode: DW_LNS_extended_op 2137 ExtLen: 1 2138 SubOpcode: DW_LNE_end_sequence 2139 Data: 256 2140 )"; 2141 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata); 2142 ASSERT_TRUE((bool)ErrOrSections); 2143 DWARFContextInMemory DwarfContext(*ErrOrSections, 8); 2144 VerifyError(DwarfContext, "error: two compile unit DIEs, 0x0000000b and " 2145 "0x0000001f, have the same DW_AT_stmt_list section " 2146 "offset:"); 2147 } 2148 2149 } // end anonymous namespace 2150