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/StringRef.h" 14 #include "llvm/ADT/Triple.h" 15 #include "llvm/Config/llvm-config.h" 16 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h" 17 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 18 #include "llvm/DebugInfo/DWARF/DWARFDie.h" 19 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" 20 #include "llvm/Object/ObjectFile.h" 21 #include "llvm/ObjectYAML/DWARFYAML.h" 22 #include "llvm/ObjectYAML/DWARFEmitter.h" 23 #include "llvm/Support/Dwarf.h" 24 #include "llvm/Support/Error.h" 25 #include "llvm/Support/MemoryBuffer.h" 26 #include "llvm/Support/TargetSelect.h" 27 #include "gtest/gtest.h" 28 #include <climits> 29 #include <cstdint> 30 #include <cstring> 31 #include <string> 32 33 using namespace llvm; 34 using namespace dwarf; 35 36 namespace { 37 38 void initLLVMIfNeeded() { 39 static bool gInitialized = false; 40 if (!gInitialized) { 41 gInitialized = true; 42 InitializeAllTargets(); 43 InitializeAllTargetMCs(); 44 InitializeAllAsmPrinters(); 45 InitializeAllAsmParsers(); 46 } 47 } 48 49 Triple getHostTripleForAddrSize(uint8_t AddrSize) { 50 Triple PT(Triple::normalize(LLVM_HOST_TRIPLE)); 51 52 if (AddrSize == 8 && PT.isArch32Bit()) 53 return PT.get64BitArchVariant(); 54 if (AddrSize == 4 && PT.isArch64Bit()) 55 return PT.get32BitArchVariant(); 56 return PT; 57 } 58 59 /// Take any llvm::Expected and check and handle any errors. 60 /// 61 /// \param Expected a llvm::Excepted instance to check. 62 /// \returns true if there were errors, false otherwise. 63 template <typename T> 64 static bool HandleExpectedError(T &Expected) { 65 std::string ErrorMsg; 66 handleAllErrors(Expected.takeError(), [&](const ErrorInfoBase &EI) { 67 ErrorMsg = EI.message(); 68 }); 69 if (!ErrorMsg.empty()) { 70 ::testing::AssertionFailure() << "error: " << ErrorMsg; 71 return true; 72 } 73 return false; 74 } 75 76 template <uint16_t Version, class AddrType, class RefAddrType> 77 void TestAllForms() { 78 // Test that we can decode all DW_FORM values correctly. 79 80 const uint8_t AddrSize = sizeof(AddrType); 81 const AddrType AddrValue = (AddrType)0x0123456789abcdefULL; 82 const uint8_t BlockData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; 83 const uint32_t BlockSize = sizeof(BlockData); 84 const RefAddrType RefAddr = 0x12345678; 85 const uint8_t Data1 = 0x01U; 86 const uint16_t Data2 = 0x2345U; 87 const uint32_t Data4 = 0x6789abcdU; 88 const uint64_t Data8 = 0x0011223344556677ULL; 89 const uint64_t Data8_2 = 0xAABBCCDDEEFF0011ULL; 90 const int64_t SData = INT64_MIN; 91 const int64_t ICSData = INT64_MAX; // DW_FORM_implicit_const SData 92 const uint64_t UData[] = {UINT64_MAX - 1, UINT64_MAX - 2, UINT64_MAX - 3, 93 UINT64_MAX - 4, UINT64_MAX - 5, UINT64_MAX - 6, 94 UINT64_MAX - 7, UINT64_MAX - 8, UINT64_MAX - 9}; 95 #define UDATA_1 18446744073709551614ULL 96 const uint32_t Dwarf32Values[] = {1, 2, 3, 4, 5, 6, 7, 8}; 97 const char *StringValue = "Hello"; 98 const char *StrpValue = "World"; 99 initLLVMIfNeeded(); 100 Triple Triple = getHostTripleForAddrSize(AddrSize); 101 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 102 if (HandleExpectedError(ExpectedDG)) 103 return; 104 dwarfgen::Generator *DG = ExpectedDG.get().get(); 105 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 106 dwarfgen::DIE CUDie = CU.getUnitDIE(); 107 uint16_t Attr = DW_AT_lo_user; 108 109 //---------------------------------------------------------------------- 110 // Test address forms 111 //---------------------------------------------------------------------- 112 const auto Attr_DW_FORM_addr = static_cast<dwarf::Attribute>(Attr++); 113 CUDie.addAttribute(Attr_DW_FORM_addr, DW_FORM_addr, AddrValue); 114 115 //---------------------------------------------------------------------- 116 // Test block forms 117 //---------------------------------------------------------------------- 118 const auto Attr_DW_FORM_block = static_cast<dwarf::Attribute>(Attr++); 119 CUDie.addAttribute(Attr_DW_FORM_block, DW_FORM_block, BlockData, BlockSize); 120 121 const auto Attr_DW_FORM_block1 = static_cast<dwarf::Attribute>(Attr++); 122 CUDie.addAttribute(Attr_DW_FORM_block1, DW_FORM_block1, BlockData, BlockSize); 123 124 const auto Attr_DW_FORM_block2 = static_cast<dwarf::Attribute>(Attr++); 125 CUDie.addAttribute(Attr_DW_FORM_block2, DW_FORM_block2, BlockData, BlockSize); 126 127 const auto Attr_DW_FORM_block4 = static_cast<dwarf::Attribute>(Attr++); 128 CUDie.addAttribute(Attr_DW_FORM_block4, DW_FORM_block4, BlockData, BlockSize); 129 130 //---------------------------------------------------------------------- 131 // Test data forms 132 //---------------------------------------------------------------------- 133 const auto Attr_DW_FORM_data1 = static_cast<dwarf::Attribute>(Attr++); 134 CUDie.addAttribute(Attr_DW_FORM_data1, DW_FORM_data1, Data1); 135 136 const auto Attr_DW_FORM_data2 = static_cast<dwarf::Attribute>(Attr++); 137 CUDie.addAttribute(Attr_DW_FORM_data2, DW_FORM_data2, Data2); 138 139 const auto Attr_DW_FORM_data4 = static_cast<dwarf::Attribute>(Attr++); 140 CUDie.addAttribute(Attr_DW_FORM_data4, DW_FORM_data4, Data4); 141 142 const auto Attr_DW_FORM_data8 = static_cast<dwarf::Attribute>(Attr++); 143 CUDie.addAttribute(Attr_DW_FORM_data8, DW_FORM_data8, Data8); 144 145 //---------------------------------------------------------------------- 146 // Test string forms 147 //---------------------------------------------------------------------- 148 const auto Attr_DW_FORM_string = static_cast<dwarf::Attribute>(Attr++); 149 CUDie.addAttribute(Attr_DW_FORM_string, DW_FORM_string, StringValue); 150 151 const auto Attr_DW_FORM_strp = static_cast<dwarf::Attribute>(Attr++); 152 CUDie.addAttribute(Attr_DW_FORM_strp, DW_FORM_strp, StrpValue); 153 154 //---------------------------------------------------------------------- 155 // Test reference forms 156 //---------------------------------------------------------------------- 157 const auto Attr_DW_FORM_ref_addr = static_cast<dwarf::Attribute>(Attr++); 158 CUDie.addAttribute(Attr_DW_FORM_ref_addr, DW_FORM_ref_addr, RefAddr); 159 160 const auto Attr_DW_FORM_ref1 = static_cast<dwarf::Attribute>(Attr++); 161 CUDie.addAttribute(Attr_DW_FORM_ref1, DW_FORM_ref1, Data1); 162 163 const auto Attr_DW_FORM_ref2 = static_cast<dwarf::Attribute>(Attr++); 164 CUDie.addAttribute(Attr_DW_FORM_ref2, DW_FORM_ref2, Data2); 165 166 const auto Attr_DW_FORM_ref4 = static_cast<dwarf::Attribute>(Attr++); 167 CUDie.addAttribute(Attr_DW_FORM_ref4, DW_FORM_ref4, Data4); 168 169 const auto Attr_DW_FORM_ref8 = static_cast<dwarf::Attribute>(Attr++); 170 CUDie.addAttribute(Attr_DW_FORM_ref8, DW_FORM_ref8, Data8); 171 172 const auto Attr_DW_FORM_ref_sig8 = static_cast<dwarf::Attribute>(Attr++); 173 CUDie.addAttribute(Attr_DW_FORM_ref_sig8, DW_FORM_ref_sig8, Data8_2); 174 175 const auto Attr_DW_FORM_ref_udata = static_cast<dwarf::Attribute>(Attr++); 176 CUDie.addAttribute(Attr_DW_FORM_ref_udata, DW_FORM_ref_udata, UData[0]); 177 178 //---------------------------------------------------------------------- 179 // Test flag forms 180 //---------------------------------------------------------------------- 181 const auto Attr_DW_FORM_flag_true = static_cast<dwarf::Attribute>(Attr++); 182 CUDie.addAttribute(Attr_DW_FORM_flag_true, DW_FORM_flag, true); 183 184 const auto Attr_DW_FORM_flag_false = static_cast<dwarf::Attribute>(Attr++); 185 CUDie.addAttribute(Attr_DW_FORM_flag_false, DW_FORM_flag, false); 186 187 const auto Attr_DW_FORM_flag_present = static_cast<dwarf::Attribute>(Attr++); 188 CUDie.addAttribute(Attr_DW_FORM_flag_present, DW_FORM_flag_present); 189 190 //---------------------------------------------------------------------- 191 // Test SLEB128 based forms 192 //---------------------------------------------------------------------- 193 const auto Attr_DW_FORM_sdata = static_cast<dwarf::Attribute>(Attr++); 194 CUDie.addAttribute(Attr_DW_FORM_sdata, DW_FORM_sdata, SData); 195 196 const auto Attr_DW_FORM_implicit_const = 197 static_cast<dwarf::Attribute>(Attr++); 198 if (Version >= 5) 199 CUDie.addAttribute(Attr_DW_FORM_implicit_const, DW_FORM_implicit_const, 200 ICSData); 201 202 //---------------------------------------------------------------------- 203 // Test ULEB128 based forms 204 //---------------------------------------------------------------------- 205 const auto Attr_DW_FORM_udata = static_cast<dwarf::Attribute>(Attr++); 206 CUDie.addAttribute(Attr_DW_FORM_udata, DW_FORM_udata, UData[0]); 207 208 //---------------------------------------------------------------------- 209 // Test DWARF32/DWARF64 forms 210 //---------------------------------------------------------------------- 211 const auto Attr_DW_FORM_GNU_ref_alt = static_cast<dwarf::Attribute>(Attr++); 212 CUDie.addAttribute(Attr_DW_FORM_GNU_ref_alt, DW_FORM_GNU_ref_alt, 213 Dwarf32Values[0]); 214 215 const auto Attr_DW_FORM_sec_offset = static_cast<dwarf::Attribute>(Attr++); 216 CUDie.addAttribute(Attr_DW_FORM_sec_offset, DW_FORM_sec_offset, 217 Dwarf32Values[1]); 218 219 //---------------------------------------------------------------------- 220 // Add an address at the end to make sure we can decode this value 221 //---------------------------------------------------------------------- 222 const auto Attr_Last = static_cast<dwarf::Attribute>(Attr++); 223 CUDie.addAttribute(Attr_Last, DW_FORM_addr, AddrValue); 224 225 //---------------------------------------------------------------------- 226 // Generate the DWARF 227 //---------------------------------------------------------------------- 228 StringRef FileBytes = DG->generate(); 229 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 230 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 231 EXPECT_TRUE((bool)Obj); 232 DWARFContextInMemory DwarfContext(*Obj.get()); 233 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 234 EXPECT_EQ(NumCUs, 1u); 235 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 236 auto DieDG = U->getUnitDIE(false); 237 EXPECT_TRUE(DieDG.isValid()); 238 239 //---------------------------------------------------------------------- 240 // Test address forms 241 //---------------------------------------------------------------------- 242 EXPECT_EQ(AddrValue, toAddress(DieDG.find(Attr_DW_FORM_addr), 0)); 243 244 //---------------------------------------------------------------------- 245 // Test block forms 246 //---------------------------------------------------------------------- 247 Optional<DWARFFormValue> FormValue; 248 ArrayRef<uint8_t> ExtractedBlockData; 249 Optional<ArrayRef<uint8_t>> BlockDataOpt; 250 251 FormValue = DieDG.find(Attr_DW_FORM_block); 252 EXPECT_TRUE((bool)FormValue); 253 BlockDataOpt = FormValue->getAsBlock(); 254 EXPECT_TRUE(BlockDataOpt.hasValue()); 255 ExtractedBlockData = BlockDataOpt.getValue(); 256 EXPECT_EQ(ExtractedBlockData.size(), BlockSize); 257 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0); 258 259 FormValue = DieDG.find(Attr_DW_FORM_block1); 260 EXPECT_TRUE((bool)FormValue); 261 BlockDataOpt = FormValue->getAsBlock(); 262 EXPECT_TRUE(BlockDataOpt.hasValue()); 263 ExtractedBlockData = BlockDataOpt.getValue(); 264 EXPECT_EQ(ExtractedBlockData.size(), BlockSize); 265 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0); 266 267 FormValue = DieDG.find(Attr_DW_FORM_block2); 268 EXPECT_TRUE((bool)FormValue); 269 BlockDataOpt = FormValue->getAsBlock(); 270 EXPECT_TRUE(BlockDataOpt.hasValue()); 271 ExtractedBlockData = BlockDataOpt.getValue(); 272 EXPECT_EQ(ExtractedBlockData.size(), BlockSize); 273 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0); 274 275 FormValue = DieDG.find(Attr_DW_FORM_block4); 276 EXPECT_TRUE((bool)FormValue); 277 BlockDataOpt = FormValue->getAsBlock(); 278 EXPECT_TRUE(BlockDataOpt.hasValue()); 279 ExtractedBlockData = BlockDataOpt.getValue(); 280 EXPECT_EQ(ExtractedBlockData.size(), BlockSize); 281 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0); 282 283 //---------------------------------------------------------------------- 284 // Test data forms 285 //---------------------------------------------------------------------- 286 EXPECT_EQ(Data1, toUnsigned(DieDG.find(Attr_DW_FORM_data1), 0)); 287 EXPECT_EQ(Data2, toUnsigned(DieDG.find(Attr_DW_FORM_data2), 0)); 288 EXPECT_EQ(Data4, toUnsigned(DieDG.find(Attr_DW_FORM_data4), 0)); 289 EXPECT_EQ(Data8, toUnsigned(DieDG.find(Attr_DW_FORM_data8), 0)); 290 291 //---------------------------------------------------------------------- 292 // Test string forms 293 //---------------------------------------------------------------------- 294 auto ExtractedStringValue = toString(DieDG.find(Attr_DW_FORM_string)); 295 EXPECT_TRUE((bool)ExtractedStringValue); 296 EXPECT_TRUE(strcmp(StringValue, *ExtractedStringValue) == 0); 297 298 auto ExtractedStrpValue = toString(DieDG.find(Attr_DW_FORM_strp)); 299 EXPECT_TRUE((bool)ExtractedStrpValue); 300 EXPECT_TRUE(strcmp(StrpValue, *ExtractedStrpValue) == 0); 301 302 //---------------------------------------------------------------------- 303 // Test reference forms 304 //---------------------------------------------------------------------- 305 EXPECT_EQ(RefAddr, toReference(DieDG.find(Attr_DW_FORM_ref_addr), 0)); 306 EXPECT_EQ(Data1, toReference(DieDG.find(Attr_DW_FORM_ref1), 0)); 307 EXPECT_EQ(Data2, toReference(DieDG.find(Attr_DW_FORM_ref2), 0)); 308 EXPECT_EQ(Data4, toReference(DieDG.find(Attr_DW_FORM_ref4), 0)); 309 EXPECT_EQ(Data8, toReference(DieDG.find(Attr_DW_FORM_ref8), 0)); 310 EXPECT_EQ(Data8_2, toReference(DieDG.find(Attr_DW_FORM_ref_sig8), 0)); 311 EXPECT_EQ(UData[0], toReference(DieDG.find(Attr_DW_FORM_ref_udata), 0)); 312 313 //---------------------------------------------------------------------- 314 // Test flag forms 315 //---------------------------------------------------------------------- 316 EXPECT_EQ(1ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_true), 0)); 317 EXPECT_EQ(0ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_false), 1)); 318 EXPECT_EQ(1ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_present), 0)); 319 320 //---------------------------------------------------------------------- 321 // Test SLEB128 based forms 322 //---------------------------------------------------------------------- 323 EXPECT_EQ(SData, toSigned(DieDG.find(Attr_DW_FORM_sdata), 0)); 324 if (Version >= 5) 325 EXPECT_EQ(ICSData, toSigned(DieDG.find(Attr_DW_FORM_implicit_const), 0)); 326 327 //---------------------------------------------------------------------- 328 // Test ULEB128 based forms 329 //---------------------------------------------------------------------- 330 EXPECT_EQ(UData[0], toUnsigned(DieDG.find(Attr_DW_FORM_udata), 0)); 331 332 //---------------------------------------------------------------------- 333 // Test DWARF32/DWARF64 forms 334 //---------------------------------------------------------------------- 335 EXPECT_EQ(Dwarf32Values[0], 336 toReference(DieDG.find(Attr_DW_FORM_GNU_ref_alt), 0)); 337 EXPECT_EQ(Dwarf32Values[1], 338 toSectionOffset(DieDG.find(Attr_DW_FORM_sec_offset), 0)); 339 340 //---------------------------------------------------------------------- 341 // Add an address at the end to make sure we can decode this value 342 //---------------------------------------------------------------------- 343 EXPECT_EQ(AddrValue, toAddress(DieDG.find(Attr_Last), 0)); 344 } 345 346 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4AllForms) { 347 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 348 // addresses. 349 typedef uint32_t AddrType; 350 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2. 351 typedef AddrType RefAddrType; 352 TestAllForms<2, AddrType, RefAddrType>(); 353 } 354 355 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8AllForms) { 356 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 357 // addresses. 358 typedef uint64_t AddrType; 359 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2. 360 typedef AddrType RefAddrType; 361 TestAllForms<2, AddrType, RefAddrType>(); 362 } 363 364 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4AllForms) { 365 // Test that we can decode all forms for DWARF32, version 3, with 4 byte 366 // addresses. 367 typedef uint32_t AddrType; 368 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later. 369 typedef uint32_t RefAddrType; 370 TestAllForms<3, AddrType, RefAddrType>(); 371 } 372 373 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8AllForms) { 374 // Test that we can decode all forms for DWARF32, version 3, with 8 byte 375 // addresses. 376 typedef uint64_t AddrType; 377 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later 378 typedef uint32_t RefAddrType; 379 TestAllForms<3, AddrType, RefAddrType>(); 380 } 381 382 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4AllForms) { 383 // Test that we can decode all forms for DWARF32, version 4, with 4 byte 384 // addresses. 385 typedef uint32_t AddrType; 386 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later 387 typedef uint32_t RefAddrType; 388 TestAllForms<4, AddrType, RefAddrType>(); 389 } 390 391 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8AllForms) { 392 // Test that we can decode all forms for DWARF32, version 4, with 8 byte 393 // addresses. 394 typedef uint64_t AddrType; 395 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later 396 typedef uint32_t RefAddrType; 397 TestAllForms<4, AddrType, RefAddrType>(); 398 } 399 400 TEST(DWARFDebugInfo, TestDWARF32Version5Addr4AllForms) { 401 // Test that we can decode all forms for DWARF32, version 5, with 4 byte 402 // addresses. 403 typedef uint32_t AddrType; 404 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later 405 typedef uint32_t RefAddrType; 406 TestAllForms<5, AddrType, RefAddrType>(); 407 } 408 409 TEST(DWARFDebugInfo, TestDWARF32Version5Addr8AllForms) { 410 // Test that we can decode all forms for DWARF32, version 5, with 8 byte 411 // addresses. 412 typedef uint64_t AddrType; 413 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later 414 typedef uint32_t RefAddrType; 415 TestAllForms<5, AddrType, RefAddrType>(); 416 } 417 418 template <uint16_t Version, class AddrType> void TestChildren() { 419 // Test that we can decode DW_FORM_ref_addr values correctly in DWARF 2 with 420 // 4 byte addresses. DW_FORM_ref_addr values should be 4 bytes when using 421 // 8 byte addresses. 422 423 const uint8_t AddrSize = sizeof(AddrType); 424 initLLVMIfNeeded(); 425 Triple Triple = getHostTripleForAddrSize(AddrSize); 426 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 427 if (HandleExpectedError(ExpectedDG)) 428 return; 429 dwarfgen::Generator *DG = ExpectedDG.get().get(); 430 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 431 dwarfgen::DIE CUDie = CU.getUnitDIE(); 432 433 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c"); 434 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 435 436 dwarfgen::DIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram); 437 SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main"); 438 SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U); 439 SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U); 440 441 dwarfgen::DIE IntDie = CUDie.addChild(DW_TAG_base_type); 442 IntDie.addAttribute(DW_AT_name, DW_FORM_strp, "int"); 443 IntDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed); 444 IntDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4); 445 446 dwarfgen::DIE ArgcDie = SubprogramDie.addChild(DW_TAG_formal_parameter); 447 ArgcDie.addAttribute(DW_AT_name, DW_FORM_strp, "argc"); 448 // ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref4, IntDie); 449 ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, IntDie); 450 451 StringRef FileBytes = DG->generate(); 452 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 453 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 454 EXPECT_TRUE((bool)Obj); 455 DWARFContextInMemory DwarfContext(*Obj.get()); 456 457 // Verify the number of compile units is correct. 458 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 459 EXPECT_EQ(NumCUs, 1u); 460 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 461 462 // Get the compile unit DIE is valid. 463 auto DieDG = U->getUnitDIE(false); 464 EXPECT_TRUE(DieDG.isValid()); 465 466 // Verify the first child of the compile unit DIE is our subprogram. 467 auto SubprogramDieDG = DieDG.getFirstChild(); 468 EXPECT_TRUE(SubprogramDieDG.isValid()); 469 EXPECT_EQ(SubprogramDieDG.getTag(), DW_TAG_subprogram); 470 471 // Verify the first child of the subprogram is our formal parameter. 472 auto ArgcDieDG = SubprogramDieDG.getFirstChild(); 473 EXPECT_TRUE(ArgcDieDG.isValid()); 474 EXPECT_EQ(ArgcDieDG.getTag(), DW_TAG_formal_parameter); 475 476 // Verify our formal parameter has a NULL tag sibling. 477 auto NullDieDG = ArgcDieDG.getSibling(); 478 EXPECT_TRUE(NullDieDG.isValid()); 479 if (NullDieDG) { 480 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null); 481 EXPECT_TRUE(!NullDieDG.getSibling().isValid()); 482 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid()); 483 } 484 485 // Verify the sibling of our subprogram is our integer base type. 486 auto IntDieDG = SubprogramDieDG.getSibling(); 487 EXPECT_TRUE(IntDieDG.isValid()); 488 EXPECT_EQ(IntDieDG.getTag(), DW_TAG_base_type); 489 490 // Verify the sibling of our subprogram is our integer base is a NULL tag. 491 NullDieDG = IntDieDG.getSibling(); 492 EXPECT_TRUE(NullDieDG.isValid()); 493 if (NullDieDG) { 494 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null); 495 EXPECT_TRUE(!NullDieDG.getSibling().isValid()); 496 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid()); 497 } 498 } 499 500 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Children) { 501 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 502 // addresses. 503 typedef uint32_t AddrType; 504 TestChildren<2, AddrType>(); 505 } 506 507 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Children) { 508 // Test that we can decode all forms for DWARF32, version 2, with 8 byte 509 // addresses. 510 typedef uint64_t AddrType; 511 TestChildren<2, AddrType>(); 512 } 513 514 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Children) { 515 // Test that we can decode all forms for DWARF32, version 3, with 4 byte 516 // addresses. 517 typedef uint32_t AddrType; 518 TestChildren<3, AddrType>(); 519 } 520 521 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Children) { 522 // Test that we can decode all forms for DWARF32, version 3, with 8 byte 523 // addresses. 524 typedef uint64_t AddrType; 525 TestChildren<3, AddrType>(); 526 } 527 528 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Children) { 529 // Test that we can decode all forms for DWARF32, version 4, with 4 byte 530 // addresses. 531 typedef uint32_t AddrType; 532 TestChildren<4, AddrType>(); 533 } 534 535 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Children) { 536 // Test that we can decode all forms for DWARF32, version 4, with 8 byte 537 // addresses. 538 typedef uint64_t AddrType; 539 TestChildren<4, AddrType>(); 540 } 541 542 template <uint16_t Version, class AddrType> void TestReferences() { 543 // Test that we can decode DW_FORM_refXXX values correctly in DWARF. 544 545 const uint8_t AddrSize = sizeof(AddrType); 546 initLLVMIfNeeded(); 547 Triple Triple = getHostTripleForAddrSize(AddrSize); 548 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 549 if (HandleExpectedError(ExpectedDG)) 550 return; 551 dwarfgen::Generator *DG = ExpectedDG.get().get(); 552 dwarfgen::CompileUnit &CU1 = DG->addCompileUnit(); 553 dwarfgen::CompileUnit &CU2 = DG->addCompileUnit(); 554 555 dwarfgen::DIE CU1Die = CU1.getUnitDIE(); 556 CU1Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c"); 557 CU1Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 558 559 dwarfgen::DIE CU1TypeDie = CU1Die.addChild(DW_TAG_base_type); 560 CU1TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "int"); 561 CU1TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed); 562 CU1TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4); 563 564 dwarfgen::DIE CU1Ref1Die = CU1Die.addChild(DW_TAG_variable); 565 CU1Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref1"); 566 CU1Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU1TypeDie); 567 568 dwarfgen::DIE CU1Ref2Die = CU1Die.addChild(DW_TAG_variable); 569 CU1Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref2"); 570 CU1Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU1TypeDie); 571 572 dwarfgen::DIE CU1Ref4Die = CU1Die.addChild(DW_TAG_variable); 573 CU1Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref4"); 574 CU1Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU1TypeDie); 575 576 dwarfgen::DIE CU1Ref8Die = CU1Die.addChild(DW_TAG_variable); 577 CU1Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref8"); 578 CU1Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU1TypeDie); 579 580 dwarfgen::DIE CU1RefAddrDie = CU1Die.addChild(DW_TAG_variable); 581 CU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1RefAddr"); 582 CU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie); 583 584 dwarfgen::DIE CU2Die = CU2.getUnitDIE(); 585 CU2Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/foo.c"); 586 CU2Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 587 588 dwarfgen::DIE CU2TypeDie = CU2Die.addChild(DW_TAG_base_type); 589 CU2TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "float"); 590 CU2TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_float); 591 CU2TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4); 592 593 dwarfgen::DIE CU2Ref1Die = CU2Die.addChild(DW_TAG_variable); 594 CU2Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref1"); 595 CU2Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU2TypeDie); 596 597 dwarfgen::DIE CU2Ref2Die = CU2Die.addChild(DW_TAG_variable); 598 CU2Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref2"); 599 CU2Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU2TypeDie); 600 601 dwarfgen::DIE CU2Ref4Die = CU2Die.addChild(DW_TAG_variable); 602 CU2Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref4"); 603 CU2Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU2TypeDie); 604 605 dwarfgen::DIE CU2Ref8Die = CU2Die.addChild(DW_TAG_variable); 606 CU2Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref8"); 607 CU2Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU2TypeDie); 608 609 dwarfgen::DIE CU2RefAddrDie = CU2Die.addChild(DW_TAG_variable); 610 CU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2RefAddr"); 611 CU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie); 612 613 // Refer to a type in CU1 from CU2 614 dwarfgen::DIE CU2ToCU1RefAddrDie = CU2Die.addChild(DW_TAG_variable); 615 CU2ToCU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2ToCU1RefAddr"); 616 CU2ToCU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie); 617 618 // Refer to a type in CU2 from CU1 619 dwarfgen::DIE CU1ToCU2RefAddrDie = CU1Die.addChild(DW_TAG_variable); 620 CU1ToCU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1ToCU2RefAddr"); 621 CU1ToCU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie); 622 623 StringRef FileBytes = DG->generate(); 624 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 625 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 626 EXPECT_TRUE((bool)Obj); 627 DWARFContextInMemory DwarfContext(*Obj.get()); 628 629 // Verify the number of compile units is correct. 630 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 631 EXPECT_EQ(NumCUs, 2u); 632 DWARFCompileUnit *U1 = DwarfContext.getCompileUnitAtIndex(0); 633 DWARFCompileUnit *U2 = DwarfContext.getCompileUnitAtIndex(1); 634 635 // Get the compile unit DIE is valid. 636 auto Unit1DieDG = U1->getUnitDIE(false); 637 EXPECT_TRUE(Unit1DieDG.isValid()); 638 639 auto Unit2DieDG = U2->getUnitDIE(false); 640 EXPECT_TRUE(Unit2DieDG.isValid()); 641 642 // Verify the first child of the compile unit 1 DIE is our int base type. 643 auto CU1TypeDieDG = Unit1DieDG.getFirstChild(); 644 EXPECT_TRUE(CU1TypeDieDG.isValid()); 645 EXPECT_EQ(CU1TypeDieDG.getTag(), DW_TAG_base_type); 646 EXPECT_EQ(DW_ATE_signed, toUnsigned(CU1TypeDieDG.find(DW_AT_encoding), 0)); 647 648 // Verify the first child of the compile unit 2 DIE is our float base type. 649 auto CU2TypeDieDG = Unit2DieDG.getFirstChild(); 650 EXPECT_TRUE(CU2TypeDieDG.isValid()); 651 EXPECT_EQ(CU2TypeDieDG.getTag(), DW_TAG_base_type); 652 EXPECT_EQ(DW_ATE_float, toUnsigned(CU2TypeDieDG.find(DW_AT_encoding), 0)); 653 654 // Verify the sibling of the base type DIE is our Ref1 DIE and that its 655 // DW_AT_type points to our base type DIE. 656 auto CU1Ref1DieDG = CU1TypeDieDG.getSibling(); 657 EXPECT_TRUE(CU1Ref1DieDG.isValid()); 658 EXPECT_EQ(CU1Ref1DieDG.getTag(), DW_TAG_variable); 659 EXPECT_EQ(CU1TypeDieDG.getOffset(), 660 toReference(CU1Ref1DieDG.find(DW_AT_type), -1ULL)); 661 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our 662 // base type DIE in CU1. 663 auto CU1Ref2DieDG = CU1Ref1DieDG.getSibling(); 664 EXPECT_TRUE(CU1Ref2DieDG.isValid()); 665 EXPECT_EQ(CU1Ref2DieDG.getTag(), DW_TAG_variable); 666 EXPECT_EQ(CU1TypeDieDG.getOffset(), 667 toReference(CU1Ref2DieDG.find(DW_AT_type), -1ULL)); 668 669 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our 670 // base type DIE in CU1. 671 auto CU1Ref4DieDG = CU1Ref2DieDG.getSibling(); 672 EXPECT_TRUE(CU1Ref4DieDG.isValid()); 673 EXPECT_EQ(CU1Ref4DieDG.getTag(), DW_TAG_variable); 674 EXPECT_EQ(CU1TypeDieDG.getOffset(), 675 toReference(CU1Ref4DieDG.find(DW_AT_type), -1ULL)); 676 677 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our 678 // base type DIE in CU1. 679 auto CU1Ref8DieDG = CU1Ref4DieDG.getSibling(); 680 EXPECT_TRUE(CU1Ref8DieDG.isValid()); 681 EXPECT_EQ(CU1Ref8DieDG.getTag(), DW_TAG_variable); 682 EXPECT_EQ(CU1TypeDieDG.getOffset(), 683 toReference(CU1Ref8DieDG.find(DW_AT_type), -1ULL)); 684 685 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our 686 // base type DIE in CU1. 687 auto CU1RefAddrDieDG = CU1Ref8DieDG.getSibling(); 688 EXPECT_TRUE(CU1RefAddrDieDG.isValid()); 689 EXPECT_EQ(CU1RefAddrDieDG.getTag(), DW_TAG_variable); 690 EXPECT_EQ(CU1TypeDieDG.getOffset(), 691 toReference(CU1RefAddrDieDG.find(DW_AT_type), -1ULL)); 692 693 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its 694 // DW_AT_type points to our base type DIE. 695 auto CU1ToCU2RefAddrDieDG = CU1RefAddrDieDG.getSibling(); 696 EXPECT_TRUE(CU1ToCU2RefAddrDieDG.isValid()); 697 EXPECT_EQ(CU1ToCU2RefAddrDieDG.getTag(), DW_TAG_variable); 698 EXPECT_EQ(CU2TypeDieDG.getOffset(), 699 toReference(CU1ToCU2RefAddrDieDG.find(DW_AT_type), -1ULL)); 700 701 // Verify the sibling of the base type DIE is our Ref1 DIE and that its 702 // DW_AT_type points to our base type DIE. 703 auto CU2Ref1DieDG = CU2TypeDieDG.getSibling(); 704 EXPECT_TRUE(CU2Ref1DieDG.isValid()); 705 EXPECT_EQ(CU2Ref1DieDG.getTag(), DW_TAG_variable); 706 EXPECT_EQ(CU2TypeDieDG.getOffset(), 707 toReference(CU2Ref1DieDG.find(DW_AT_type), -1ULL)); 708 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our 709 // base type DIE in CU2. 710 auto CU2Ref2DieDG = CU2Ref1DieDG.getSibling(); 711 EXPECT_TRUE(CU2Ref2DieDG.isValid()); 712 EXPECT_EQ(CU2Ref2DieDG.getTag(), DW_TAG_variable); 713 EXPECT_EQ(CU2TypeDieDG.getOffset(), 714 toReference(CU2Ref2DieDG.find(DW_AT_type), -1ULL)); 715 716 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our 717 // base type DIE in CU2. 718 auto CU2Ref4DieDG = CU2Ref2DieDG.getSibling(); 719 EXPECT_TRUE(CU2Ref4DieDG.isValid()); 720 EXPECT_EQ(CU2Ref4DieDG.getTag(), DW_TAG_variable); 721 EXPECT_EQ(CU2TypeDieDG.getOffset(), 722 toReference(CU2Ref4DieDG.find(DW_AT_type), -1ULL)); 723 724 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our 725 // base type DIE in CU2. 726 auto CU2Ref8DieDG = CU2Ref4DieDG.getSibling(); 727 EXPECT_TRUE(CU2Ref8DieDG.isValid()); 728 EXPECT_EQ(CU2Ref8DieDG.getTag(), DW_TAG_variable); 729 EXPECT_EQ(CU2TypeDieDG.getOffset(), 730 toReference(CU2Ref8DieDG.find(DW_AT_type), -1ULL)); 731 732 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our 733 // base type DIE in CU2. 734 auto CU2RefAddrDieDG = CU2Ref8DieDG.getSibling(); 735 EXPECT_TRUE(CU2RefAddrDieDG.isValid()); 736 EXPECT_EQ(CU2RefAddrDieDG.getTag(), DW_TAG_variable); 737 EXPECT_EQ(CU2TypeDieDG.getOffset(), 738 toReference(CU2RefAddrDieDG.find(DW_AT_type), -1ULL)); 739 740 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its 741 // DW_AT_type points to our base type DIE. 742 auto CU2ToCU1RefAddrDieDG = CU2RefAddrDieDG.getSibling(); 743 EXPECT_TRUE(CU2ToCU1RefAddrDieDG.isValid()); 744 EXPECT_EQ(CU2ToCU1RefAddrDieDG.getTag(), DW_TAG_variable); 745 EXPECT_EQ(CU1TypeDieDG.getOffset(), 746 toReference(CU2ToCU1RefAddrDieDG.find(DW_AT_type), -1ULL)); 747 } 748 749 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4References) { 750 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 751 // addresses. 752 typedef uint32_t AddrType; 753 TestReferences<2, AddrType>(); 754 } 755 756 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8References) { 757 // Test that we can decode all forms for DWARF32, version 2, with 8 byte 758 // addresses. 759 typedef uint64_t AddrType; 760 TestReferences<2, AddrType>(); 761 } 762 763 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4References) { 764 // Test that we can decode all forms for DWARF32, version 3, with 4 byte 765 // addresses. 766 typedef uint32_t AddrType; 767 TestReferences<3, AddrType>(); 768 } 769 770 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8References) { 771 // Test that we can decode all forms for DWARF32, version 3, with 8 byte 772 // addresses. 773 typedef uint64_t AddrType; 774 TestReferences<3, AddrType>(); 775 } 776 777 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4References) { 778 // Test that we can decode all forms for DWARF32, version 4, with 4 byte 779 // addresses. 780 typedef uint32_t AddrType; 781 TestReferences<4, AddrType>(); 782 } 783 784 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8References) { 785 // Test that we can decode all forms for DWARF32, version 4, with 8 byte 786 // addresses. 787 typedef uint64_t AddrType; 788 TestReferences<4, AddrType>(); 789 } 790 791 template <uint16_t Version, class AddrType> void TestAddresses() { 792 // Test the DWARF APIs related to accessing the DW_AT_low_pc and 793 // DW_AT_high_pc. 794 const uint8_t AddrSize = sizeof(AddrType); 795 const bool SupportsHighPCAsOffset = Version >= 4; 796 initLLVMIfNeeded(); 797 Triple Triple = getHostTripleForAddrSize(AddrSize); 798 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 799 if (HandleExpectedError(ExpectedDG)) 800 return; 801 dwarfgen::Generator *DG = ExpectedDG.get().get(); 802 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 803 dwarfgen::DIE CUDie = CU.getUnitDIE(); 804 805 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c"); 806 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 807 808 // Create a subprogram DIE with no low or high PC. 809 dwarfgen::DIE SubprogramNoPC = CUDie.addChild(DW_TAG_subprogram); 810 SubprogramNoPC.addAttribute(DW_AT_name, DW_FORM_strp, "no_pc"); 811 812 // Create a subprogram DIE with a low PC only. 813 dwarfgen::DIE SubprogramLowPC = CUDie.addChild(DW_TAG_subprogram); 814 SubprogramLowPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_pc"); 815 const uint64_t ActualLowPC = 0x1000; 816 const uint64_t ActualHighPC = 0x2000; 817 const uint64_t ActualHighPCOffset = ActualHighPC - ActualLowPC; 818 SubprogramLowPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC); 819 820 // Create a subprogram DIE with a low and high PC. 821 dwarfgen::DIE SubprogramLowHighPC = CUDie.addChild(DW_TAG_subprogram); 822 SubprogramLowHighPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_high_pc"); 823 SubprogramLowHighPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC); 824 // Encode the high PC as an offset from the low PC if supported. 825 if (SupportsHighPCAsOffset) 826 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_data4, 827 ActualHighPCOffset); 828 else 829 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_addr, ActualHighPC); 830 831 StringRef FileBytes = DG->generate(); 832 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 833 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 834 EXPECT_TRUE((bool)Obj); 835 DWARFContextInMemory DwarfContext(*Obj.get()); 836 837 // Verify the number of compile units is correct. 838 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 839 EXPECT_EQ(NumCUs, 1u); 840 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 841 842 // Get the compile unit DIE is valid. 843 auto DieDG = U->getUnitDIE(false); 844 EXPECT_TRUE(DieDG.isValid()); 845 846 uint64_t LowPC, HighPC; 847 Optional<uint64_t> OptU64; 848 // Verify the that our subprogram with no PC value fails appropriately when 849 // asked for any PC values. 850 auto SubprogramDieNoPC = DieDG.getFirstChild(); 851 EXPECT_TRUE(SubprogramDieNoPC.isValid()); 852 EXPECT_EQ(SubprogramDieNoPC.getTag(), DW_TAG_subprogram); 853 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_low_pc)); 854 EXPECT_FALSE((bool)OptU64); 855 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_high_pc)); 856 EXPECT_FALSE((bool)OptU64); 857 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC)); 858 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_high_pc)); 859 EXPECT_FALSE((bool)OptU64); 860 OptU64 = toUnsigned(SubprogramDieNoPC.find(DW_AT_high_pc)); 861 EXPECT_FALSE((bool)OptU64); 862 OptU64 = SubprogramDieNoPC.getHighPC(ActualLowPC); 863 EXPECT_FALSE((bool)OptU64); 864 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC)); 865 866 // Verify the that our subprogram with only a low PC value succeeds when 867 // we ask for the Low PC, but fails appropriately when asked for the high PC 868 // or both low and high PC values. 869 auto SubprogramDieLowPC = SubprogramDieNoPC.getSibling(); 870 EXPECT_TRUE(SubprogramDieLowPC.isValid()); 871 EXPECT_EQ(SubprogramDieLowPC.getTag(), DW_TAG_subprogram); 872 OptU64 = toAddress(SubprogramDieLowPC.find(DW_AT_low_pc)); 873 EXPECT_TRUE((bool)OptU64); 874 EXPECT_EQ(OptU64.getValue(), ActualLowPC); 875 OptU64 = toAddress(SubprogramDieLowPC.find(DW_AT_high_pc)); 876 EXPECT_FALSE((bool)OptU64); 877 OptU64 = toUnsigned(SubprogramDieLowPC.find(DW_AT_high_pc)); 878 EXPECT_FALSE((bool)OptU64); 879 OptU64 = SubprogramDieLowPC.getHighPC(ActualLowPC); 880 EXPECT_FALSE((bool)OptU64); 881 EXPECT_FALSE(SubprogramDieLowPC.getLowAndHighPC(LowPC, HighPC)); 882 883 // Verify the that our subprogram with only a low PC value succeeds when 884 // we ask for the Low PC, but fails appropriately when asked for the high PC 885 // or both low and high PC values. 886 auto SubprogramDieLowHighPC = SubprogramDieLowPC.getSibling(); 887 EXPECT_TRUE(SubprogramDieLowHighPC.isValid()); 888 EXPECT_EQ(SubprogramDieLowHighPC.getTag(), DW_TAG_subprogram); 889 OptU64 = toAddress(SubprogramDieLowHighPC.find(DW_AT_low_pc)); 890 EXPECT_TRUE((bool)OptU64); 891 EXPECT_EQ(OptU64.getValue(), ActualLowPC); 892 // Get the high PC as an address. This should succeed if the high PC was 893 // encoded as an address and fail if the high PC was encoded as an offset. 894 OptU64 = toAddress(SubprogramDieLowHighPC.find(DW_AT_high_pc)); 895 if (SupportsHighPCAsOffset) { 896 EXPECT_FALSE((bool)OptU64); 897 } else { 898 EXPECT_TRUE((bool)OptU64); 899 EXPECT_EQ(OptU64.getValue(), ActualHighPC); 900 } 901 // Get the high PC as an unsigned constant. This should succeed if the high PC 902 // was encoded as an offset and fail if the high PC was encoded as an address. 903 OptU64 = toUnsigned(SubprogramDieLowHighPC.find(DW_AT_high_pc)); 904 if (SupportsHighPCAsOffset) { 905 EXPECT_TRUE((bool)OptU64); 906 EXPECT_EQ(OptU64.getValue(), ActualHighPCOffset); 907 } else { 908 EXPECT_FALSE((bool)OptU64); 909 } 910 911 OptU64 = SubprogramDieLowHighPC.getHighPC(ActualLowPC); 912 EXPECT_TRUE((bool)OptU64); 913 EXPECT_EQ(OptU64.getValue(), ActualHighPC); 914 915 EXPECT_TRUE(SubprogramDieLowHighPC.getLowAndHighPC(LowPC, HighPC)); 916 EXPECT_EQ(LowPC, ActualLowPC); 917 EXPECT_EQ(HighPC, ActualHighPC); 918 } 919 920 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Addresses) { 921 // Test that we can decode address values in DWARF32, version 2, with 4 byte 922 // addresses. 923 typedef uint32_t AddrType; 924 TestAddresses<2, AddrType>(); 925 } 926 927 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Addresses) { 928 // Test that we can decode address values in DWARF32, version 2, with 8 byte 929 // addresses. 930 typedef uint64_t AddrType; 931 TestAddresses<2, AddrType>(); 932 } 933 934 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Addresses) { 935 // Test that we can decode address values in DWARF32, version 3, with 4 byte 936 // addresses. 937 typedef uint32_t AddrType; 938 TestAddresses<3, AddrType>(); 939 } 940 941 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Addresses) { 942 // Test that we can decode address values in DWARF32, version 3, with 8 byte 943 // addresses. 944 typedef uint64_t AddrType; 945 TestAddresses<3, AddrType>(); 946 } 947 948 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Addresses) { 949 // Test that we can decode address values in DWARF32, version 4, with 4 byte 950 // addresses. 951 typedef uint32_t AddrType; 952 TestAddresses<4, AddrType>(); 953 } 954 955 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Addresses) { 956 // Test that we can decode address values in DWARF32, version 4, with 8 byte 957 // addresses. 958 typedef uint64_t AddrType; 959 TestAddresses<4, AddrType>(); 960 } 961 962 TEST(DWARFDebugInfo, TestRelations) { 963 // Test the DWARF APIs related to accessing the DW_AT_low_pc and 964 // DW_AT_high_pc. 965 uint16_t Version = 4; 966 967 const uint8_t AddrSize = sizeof(void *); 968 initLLVMIfNeeded(); 969 Triple Triple = getHostTripleForAddrSize(AddrSize); 970 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 971 if (HandleExpectedError(ExpectedDG)) 972 return; 973 dwarfgen::Generator *DG = ExpectedDG.get().get(); 974 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 975 976 enum class Tag: uint16_t { 977 A = dwarf::DW_TAG_lo_user, 978 B, 979 C, 980 C1, 981 C2, 982 D, 983 D1 984 }; 985 986 // Scope to allow us to re-use the same DIE names 987 { 988 // Create DWARF tree that looks like: 989 // 990 // CU 991 // A 992 // B 993 // C 994 // C1 995 // C2 996 // D 997 // D1 998 dwarfgen::DIE CUDie = CU.getUnitDIE(); 999 dwarfgen::DIE A = CUDie.addChild((dwarf::Tag)Tag::A); 1000 A.addChild((dwarf::Tag)Tag::B); 1001 dwarfgen::DIE C = A.addChild((dwarf::Tag)Tag::C); 1002 dwarfgen::DIE D = A.addChild((dwarf::Tag)Tag::D); 1003 C.addChild((dwarf::Tag)Tag::C1); 1004 C.addChild((dwarf::Tag)Tag::C2); 1005 D.addChild((dwarf::Tag)Tag::D1); 1006 } 1007 1008 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1009 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1010 EXPECT_TRUE((bool)Obj); 1011 DWARFContextInMemory DwarfContext(*Obj.get()); 1012 1013 // Verify the number of compile units is correct. 1014 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 1015 EXPECT_EQ(NumCUs, 1u); 1016 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1017 1018 // Get the compile unit DIE is valid. 1019 auto CUDie = U->getUnitDIE(false); 1020 EXPECT_TRUE(CUDie.isValid()); 1021 1022 // The compile unit doesn't have a parent or a sibling. 1023 auto ParentDie = CUDie.getParent(); 1024 EXPECT_FALSE(ParentDie.isValid()); 1025 auto SiblingDie = CUDie.getSibling(); 1026 EXPECT_FALSE(SiblingDie.isValid()); 1027 1028 // Get the children of the compile unit 1029 auto A = CUDie.getFirstChild(); 1030 auto B = A.getFirstChild(); 1031 auto C = B.getSibling(); 1032 auto D = C.getSibling(); 1033 auto Null = D.getSibling(); 1034 1035 // Verify NULL Die is NULL and has no children or siblings 1036 EXPECT_TRUE(Null.isNULL()); 1037 EXPECT_FALSE(Null.getSibling().isValid()); 1038 EXPECT_FALSE(Null.getFirstChild().isValid()); 1039 1040 // Verify all children of the compile unit DIE are correct. 1041 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A); 1042 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B); 1043 EXPECT_EQ(C.getTag(), (dwarf::Tag)Tag::C); 1044 EXPECT_EQ(D.getTag(), (dwarf::Tag)Tag::D); 1045 1046 // Verify who has children 1047 EXPECT_TRUE(A.hasChildren()); 1048 EXPECT_FALSE(B.hasChildren()); 1049 EXPECT_TRUE(C.hasChildren()); 1050 EXPECT_TRUE(D.hasChildren()); 1051 1052 // Make sure the parent of all the children of the compile unit are the 1053 // compile unit. 1054 EXPECT_EQ(A.getParent(), CUDie); 1055 1056 // Make sure the parent of all the children of A are the A. 1057 // B is the first child in A, so we need to verify we can get the previous 1058 // DIE as the parent. 1059 EXPECT_EQ(B.getParent(), A); 1060 // C is the second child in A, so we need to make sure we can backup across 1061 // other DIE (B) at the same level to get the correct parent. 1062 EXPECT_EQ(C.getParent(), A); 1063 // D is the third child of A. We need to verify we can backup across other DIE 1064 // (B and C) including DIE that have children (D) to get the correct parent. 1065 EXPECT_EQ(D.getParent(), A); 1066 1067 // Verify that a DIE with no children returns an invalid DWARFDie. 1068 EXPECT_FALSE(B.getFirstChild().isValid()); 1069 1070 // Verify the children of the B DIE 1071 auto C1 = C.getFirstChild(); 1072 auto C2 = C1.getSibling(); 1073 EXPECT_TRUE(C2.getSibling().isNULL()); 1074 1075 // Verify all children of the B DIE correctly valid or invalid. 1076 EXPECT_EQ(C1.getTag(), (dwarf::Tag)Tag::C1); 1077 EXPECT_EQ(C2.getTag(), (dwarf::Tag)Tag::C2); 1078 1079 // Make sure the parent of all the children of the B are the B. 1080 EXPECT_EQ(C1.getParent(), C); 1081 EXPECT_EQ(C2.getParent(), C); 1082 } 1083 1084 TEST(DWARFDebugInfo, TestDWARFDie) { 1085 // Make sure a default constructed DWARFDie doesn't have any parent, sibling 1086 // or child; 1087 DWARFDie DefaultDie; 1088 EXPECT_FALSE(DefaultDie.getParent().isValid()); 1089 EXPECT_FALSE(DefaultDie.getFirstChild().isValid()); 1090 EXPECT_FALSE(DefaultDie.getSibling().isValid()); 1091 } 1092 1093 TEST(DWARFDebugInfo, TestChildIterators) { 1094 // Test the DWARF APIs related to iterating across the children of a DIE using 1095 // the DWARFDie::iterator class. 1096 uint16_t Version = 4; 1097 1098 const uint8_t AddrSize = sizeof(void *); 1099 initLLVMIfNeeded(); 1100 Triple Triple = getHostTripleForAddrSize(AddrSize); 1101 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1102 if (HandleExpectedError(ExpectedDG)) 1103 return; 1104 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1105 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1106 1107 enum class Tag: uint16_t { 1108 A = dwarf::DW_TAG_lo_user, 1109 B, 1110 }; 1111 1112 // Scope to allow us to re-use the same DIE names 1113 { 1114 // Create DWARF tree that looks like: 1115 // 1116 // CU 1117 // A 1118 // B 1119 auto CUDie = CU.getUnitDIE(); 1120 CUDie.addChild((dwarf::Tag)Tag::A); 1121 CUDie.addChild((dwarf::Tag)Tag::B); 1122 } 1123 1124 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1125 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1126 EXPECT_TRUE((bool)Obj); 1127 DWARFContextInMemory DwarfContext(*Obj.get()); 1128 1129 // Verify the number of compile units is correct. 1130 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 1131 EXPECT_EQ(NumCUs, 1u); 1132 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1133 1134 // Get the compile unit DIE is valid. 1135 auto CUDie = U->getUnitDIE(false); 1136 EXPECT_TRUE(CUDie.isValid()); 1137 uint32_t Index; 1138 DWARFDie A; 1139 DWARFDie B; 1140 1141 // Verify the compile unit DIE's children. 1142 Index = 0; 1143 for (auto Die : CUDie.children()) { 1144 switch (Index++) { 1145 case 0: A = Die; break; 1146 case 1: B = Die; break; 1147 } 1148 } 1149 1150 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A); 1151 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B); 1152 1153 // Verify that A has no children by verifying that the begin and end contain 1154 // invalid DIEs and also that the iterators are equal. 1155 EXPECT_EQ(A.begin(), A.end()); 1156 } 1157 1158 TEST(DWARFDebugInfo, TestChildIteratorsOnInvalidDie) { 1159 // Verify that an invalid DIE has no children. 1160 DWARFDie Invalid; 1161 auto begin = Invalid.begin(); 1162 auto end = Invalid.end(); 1163 EXPECT_FALSE(begin->isValid()); 1164 EXPECT_FALSE(end->isValid()); 1165 EXPECT_EQ(begin, end); 1166 } 1167 1168 TEST(DWARFDebugInfo, TestEmptyChildren) { 1169 const char *yamldata = "debug_abbrev:\n" 1170 " - Code: 0x00000001\n" 1171 " Tag: DW_TAG_compile_unit\n" 1172 " Children: DW_CHILDREN_yes\n" 1173 " Attributes:\n" 1174 "debug_info:\n" 1175 " - Length: 9\n" 1176 " Version: 4\n" 1177 " AbbrOffset: 0\n" 1178 " AddrSize: 8\n" 1179 " Entries:\n" 1180 " - AbbrCode: 0x00000001\n" 1181 " Values:\n" 1182 " - AbbrCode: 0x00000000\n" 1183 " Values:\n"; 1184 1185 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata)); 1186 ASSERT_TRUE((bool)ErrOrSections); 1187 1188 auto &DebugSections = *ErrOrSections; 1189 1190 DWARFContextInMemory DwarfContext(DebugSections, 8); 1191 1192 // Verify the number of compile units is correct. 1193 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 1194 EXPECT_EQ(NumCUs, 1u); 1195 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1196 1197 // Get the compile unit DIE is valid. 1198 auto CUDie = U->getUnitDIE(false); 1199 EXPECT_TRUE(CUDie.isValid()); 1200 1201 // Verify that the CU Die that says it has children, but doesn't, actually 1202 // has begin and end iterators that are equal. We want to make sure we don't 1203 // see the Null DIEs during iteration. 1204 EXPECT_EQ(CUDie.begin(), CUDie.end()); 1205 } 1206 1207 TEST(DWARFDebugInfo, TestAttributeIterators) { 1208 // Test the DWARF APIs related to iterating across all attribute values in a 1209 // a DWARFDie. 1210 uint16_t Version = 4; 1211 1212 const uint8_t AddrSize = sizeof(void *); 1213 initLLVMIfNeeded(); 1214 Triple Triple = getHostTripleForAddrSize(AddrSize); 1215 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1216 if (HandleExpectedError(ExpectedDG)) 1217 return; 1218 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1219 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1220 const uint64_t CULowPC = 0x1000; 1221 StringRef CUPath("/tmp/main.c"); 1222 1223 // Scope to allow us to re-use the same DIE names 1224 { 1225 auto CUDie = CU.getUnitDIE(); 1226 // Encode an attribute value before an attribute with no data. 1227 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, CUPath.data()); 1228 // Encode an attribute value with no data in .debug_info/types to ensure 1229 // the iteration works correctly. 1230 CUDie.addAttribute(DW_AT_declaration, DW_FORM_flag_present); 1231 // Encode an attribute value after an attribute with no data. 1232 CUDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, CULowPC); 1233 } 1234 1235 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1236 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1237 EXPECT_TRUE((bool)Obj); 1238 DWARFContextInMemory DwarfContext(*Obj.get()); 1239 1240 // Verify the number of compile units is correct. 1241 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 1242 EXPECT_EQ(NumCUs, 1u); 1243 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1244 1245 // Get the compile unit DIE is valid. 1246 auto CUDie = U->getUnitDIE(false); 1247 EXPECT_TRUE(CUDie.isValid()); 1248 1249 auto R = CUDie.attributes(); 1250 auto I = R.begin(); 1251 auto E = R.end(); 1252 1253 ASSERT_NE(E, I); 1254 EXPECT_EQ(I->Attr, DW_AT_name); 1255 auto ActualCUPath = I->Value.getAsCString(); 1256 EXPECT_EQ(CUPath, *ActualCUPath); 1257 1258 ASSERT_NE(E, ++I); 1259 EXPECT_EQ(I->Attr, DW_AT_declaration); 1260 EXPECT_EQ(1ull, *I->Value.getAsUnsignedConstant()); 1261 1262 ASSERT_NE(E, ++I); 1263 EXPECT_EQ(I->Attr, DW_AT_low_pc); 1264 EXPECT_EQ(CULowPC, *I->Value.getAsAddress()); 1265 1266 EXPECT_EQ(E, ++I); 1267 } 1268 1269 TEST(DWARFDebugInfo, TestFindRecurse) { 1270 uint16_t Version = 4; 1271 1272 const uint8_t AddrSize = sizeof(void *); 1273 initLLVMIfNeeded(); 1274 Triple Triple = getHostTripleForAddrSize(AddrSize); 1275 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1276 if (HandleExpectedError(ExpectedDG)) 1277 return; 1278 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1279 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1280 1281 StringRef SpecDieName("spec"); 1282 StringRef AbsDieName("abs"); 1283 // Scope to allow us to re-use the same DIE names 1284 { 1285 // Create a compile unit DIE that has an abbreviation that says it has 1286 // children, but doesn't have any actual attributes. This helps us test 1287 // a DIE that has only one child: a NULL DIE. 1288 auto CUDie = CU.getUnitDIE(); 1289 auto FuncSpecDie = CUDie.addChild(DW_TAG_subprogram); 1290 auto FuncDie = CUDie.addChild(DW_TAG_subprogram); 1291 auto VarAbsDie = CUDie.addChild(DW_TAG_variable); 1292 auto VarDie = CUDie.addChild(DW_TAG_variable); 1293 FuncSpecDie.addAttribute(DW_AT_name, DW_FORM_strp, SpecDieName); 1294 FuncDie.addAttribute(DW_AT_specification, DW_FORM_ref4, FuncSpecDie); 1295 VarAbsDie.addAttribute(DW_AT_name, DW_FORM_strp, AbsDieName); 1296 VarDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, VarAbsDie); 1297 } 1298 1299 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1300 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1301 EXPECT_TRUE((bool)Obj); 1302 DWARFContextInMemory DwarfContext(*Obj.get()); 1303 1304 // Verify the number of compile units is correct. 1305 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 1306 EXPECT_EQ(NumCUs, 1u); 1307 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1308 1309 // Get the compile unit DIE is valid. 1310 auto CUDie = U->getUnitDIE(false); 1311 EXPECT_TRUE(CUDie.isValid()); 1312 1313 auto FuncSpecDie = CUDie.getFirstChild(); 1314 auto FuncDie = FuncSpecDie.getSibling(); 1315 auto VarAbsDie = FuncDie.getSibling(); 1316 auto VarDie = VarAbsDie.getSibling(); 1317 1318 // Make sure we can't extract the name from the specification die when using 1319 // DWARFDie::find() since it won't check the DW_AT_specification DIE. 1320 EXPECT_FALSE(FuncDie.find(DW_AT_name).hasValue()); 1321 1322 // Make sure we can extract the name from the specification die when using 1323 // DWARFDie::findRecursively() since it should recurse through the 1324 // DW_AT_specification DIE. 1325 auto NameOpt = FuncDie.findRecursively(DW_AT_name); 1326 EXPECT_TRUE(NameOpt.hasValue()); 1327 // Test the dwarf::toString() helper function. 1328 auto StringOpt = toString(NameOpt); 1329 EXPECT_TRUE(StringOpt.hasValue()); 1330 EXPECT_EQ(SpecDieName, StringOpt.getValueOr(nullptr)); 1331 // Test the dwarf::toString() helper function with a default value specified. 1332 EXPECT_EQ(SpecDieName, toString(NameOpt, nullptr)); 1333 1334 // Make sure we can't extract the name from the abstract origin die when using 1335 // DWARFDie::find() since it won't check the DW_AT_abstract_origin DIE. 1336 EXPECT_FALSE(VarDie.find(DW_AT_name).hasValue()); 1337 1338 // Make sure we can extract the name from the abstract origin die when using 1339 // DWARFDie::findRecursively() since it should recurse through the 1340 // DW_AT_abstract_origin DIE. 1341 NameOpt = VarDie.findRecursively(DW_AT_name); 1342 EXPECT_TRUE(NameOpt.hasValue()); 1343 // Test the dwarf::toString() helper function. 1344 StringOpt = toString(NameOpt); 1345 EXPECT_TRUE(StringOpt.hasValue()); 1346 EXPECT_EQ(AbsDieName, StringOpt.getValueOr(nullptr)); 1347 // Test the dwarf::toString() helper function with a default value specified. 1348 EXPECT_EQ(AbsDieName, toString(NameOpt, nullptr)); 1349 } 1350 1351 TEST(DWARFDebugInfo, TestDwarfToFunctions) { 1352 // Test all of the dwarf::toXXX functions that take a 1353 // Optional<DWARFFormValue> and extract the values from it. 1354 DWARFFormValue FormVal; 1355 uint64_t InvalidU64 = 0xBADBADBADBADBADB; 1356 int64_t InvalidS64 = 0xBADBADBADBADBADB; 1357 // First test that we don't get valid values back when using an optional with 1358 // no value. 1359 Optional<DWARFFormValue> FormValOpt; 1360 EXPECT_FALSE(toString(FormValOpt).hasValue()); 1361 EXPECT_FALSE(toUnsigned(FormValOpt).hasValue()); 1362 EXPECT_FALSE(toReference(FormValOpt).hasValue()); 1363 EXPECT_FALSE(toSigned(FormValOpt).hasValue()); 1364 EXPECT_FALSE(toAddress(FormValOpt).hasValue()); 1365 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue()); 1366 EXPECT_FALSE(toBlock(FormValOpt).hasValue()); 1367 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr)); 1368 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt, InvalidU64)); 1369 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64)); 1370 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64)); 1371 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64)); 1372 EXPECT_EQ(InvalidS64, toSigned(FormValOpt, InvalidS64)); 1373 1374 // Test successful and unsuccessful address decoding. 1375 uint64_t Address = 0x100000000ULL; 1376 FormVal.setForm(DW_FORM_addr); 1377 FormVal.setUValue(Address); 1378 FormValOpt = FormVal; 1379 1380 EXPECT_FALSE(toString(FormValOpt).hasValue()); 1381 EXPECT_FALSE(toUnsigned(FormValOpt).hasValue()); 1382 EXPECT_FALSE(toReference(FormValOpt).hasValue()); 1383 EXPECT_FALSE(toSigned(FormValOpt).hasValue()); 1384 EXPECT_TRUE(toAddress(FormValOpt).hasValue()); 1385 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue()); 1386 EXPECT_FALSE(toBlock(FormValOpt).hasValue()); 1387 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr)); 1388 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt, InvalidU64)); 1389 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64)); 1390 EXPECT_EQ(Address, toAddress(FormValOpt, InvalidU64)); 1391 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64)); 1392 EXPECT_EQ(InvalidS64, toSigned(FormValOpt, InvalidU64)); 1393 1394 // Test successful and unsuccessful unsigned constant decoding. 1395 uint64_t UData8 = 0x1020304050607080ULL; 1396 FormVal.setForm(DW_FORM_udata); 1397 FormVal.setUValue(UData8); 1398 FormValOpt = FormVal; 1399 1400 EXPECT_FALSE(toString(FormValOpt).hasValue()); 1401 EXPECT_TRUE(toUnsigned(FormValOpt).hasValue()); 1402 EXPECT_FALSE(toReference(FormValOpt).hasValue()); 1403 EXPECT_TRUE(toSigned(FormValOpt).hasValue()); 1404 EXPECT_FALSE(toAddress(FormValOpt).hasValue()); 1405 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue()); 1406 EXPECT_FALSE(toBlock(FormValOpt).hasValue()); 1407 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr)); 1408 EXPECT_EQ(UData8, toUnsigned(FormValOpt, InvalidU64)); 1409 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64)); 1410 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64)); 1411 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64)); 1412 EXPECT_EQ((int64_t)UData8, toSigned(FormValOpt, InvalidU64)); 1413 1414 // Test successful and unsuccessful reference decoding. 1415 uint32_t RefData = 0x11223344U; 1416 FormVal.setForm(DW_FORM_ref_addr); 1417 FormVal.setUValue(RefData); 1418 FormValOpt = FormVal; 1419 1420 EXPECT_FALSE(toString(FormValOpt).hasValue()); 1421 EXPECT_FALSE(toUnsigned(FormValOpt).hasValue()); 1422 EXPECT_TRUE(toReference(FormValOpt).hasValue()); 1423 EXPECT_FALSE(toSigned(FormValOpt).hasValue()); 1424 EXPECT_FALSE(toAddress(FormValOpt).hasValue()); 1425 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue()); 1426 EXPECT_FALSE(toBlock(FormValOpt).hasValue()); 1427 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr)); 1428 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt, InvalidU64)); 1429 EXPECT_EQ(RefData, toReference(FormValOpt, InvalidU64)); 1430 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64)); 1431 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64)); 1432 EXPECT_EQ(InvalidS64, toSigned(FormValOpt, InvalidU64)); 1433 1434 // Test successful and unsuccessful signed constant decoding. 1435 int64_t SData8 = 0x1020304050607080ULL; 1436 FormVal.setForm(DW_FORM_udata); 1437 FormVal.setSValue(SData8); 1438 FormValOpt = FormVal; 1439 1440 EXPECT_FALSE(toString(FormValOpt).hasValue()); 1441 EXPECT_TRUE(toUnsigned(FormValOpt).hasValue()); 1442 EXPECT_FALSE(toReference(FormValOpt).hasValue()); 1443 EXPECT_TRUE(toSigned(FormValOpt).hasValue()); 1444 EXPECT_FALSE(toAddress(FormValOpt).hasValue()); 1445 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue()); 1446 EXPECT_FALSE(toBlock(FormValOpt).hasValue()); 1447 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr)); 1448 EXPECT_EQ((uint64_t)SData8, toUnsigned(FormValOpt, InvalidU64)); 1449 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64)); 1450 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64)); 1451 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64)); 1452 EXPECT_EQ(SData8, toSigned(FormValOpt, InvalidU64)); 1453 1454 // Test successful and unsuccessful block decoding. 1455 uint8_t Data[] = { 2, 3, 4 }; 1456 ArrayRef<uint8_t> Array(Data); 1457 FormVal.setForm(DW_FORM_block1); 1458 FormVal.setBlockValue(Array); 1459 FormValOpt = FormVal; 1460 1461 EXPECT_FALSE(toString(FormValOpt).hasValue()); 1462 EXPECT_FALSE(toUnsigned(FormValOpt).hasValue()); 1463 EXPECT_FALSE(toReference(FormValOpt).hasValue()); 1464 EXPECT_FALSE(toSigned(FormValOpt).hasValue()); 1465 EXPECT_FALSE(toAddress(FormValOpt).hasValue()); 1466 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue()); 1467 auto BlockOpt = toBlock(FormValOpt); 1468 EXPECT_TRUE(BlockOpt.hasValue()); 1469 EXPECT_EQ(*BlockOpt, Array); 1470 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr)); 1471 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt, InvalidU64)); 1472 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64)); 1473 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64)); 1474 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64)); 1475 EXPECT_EQ(InvalidS64, toSigned(FormValOpt, InvalidU64)); 1476 1477 // Test 1478 } 1479 1480 TEST(DWARFDebugInfo, TestFindAttrs) { 1481 // Test the DWARFDie::find() and DWARFDie::findRecursively() that take an 1482 // ArrayRef<dwarf::Attribute> value to make sure they work correctly. 1483 uint16_t Version = 4; 1484 1485 const uint8_t AddrSize = sizeof(void *); 1486 initLLVMIfNeeded(); 1487 Triple Triple = getHostTripleForAddrSize(AddrSize); 1488 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1489 if (HandleExpectedError(ExpectedDG)) 1490 return; 1491 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1492 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1493 1494 StringRef DieMangled("_Z3fooi"); 1495 // Scope to allow us to re-use the same DIE names 1496 { 1497 // Create a compile unit DIE that has an abbreviation that says it has 1498 // children, but doesn't have any actual attributes. This helps us test 1499 // a DIE that has only one child: a NULL DIE. 1500 auto CUDie = CU.getUnitDIE(); 1501 auto FuncSpecDie = CUDie.addChild(DW_TAG_subprogram); 1502 auto FuncDie = CUDie.addChild(DW_TAG_subprogram); 1503 FuncSpecDie.addAttribute(DW_AT_MIPS_linkage_name, DW_FORM_strp, DieMangled); 1504 FuncDie.addAttribute(DW_AT_specification, DW_FORM_ref4, FuncSpecDie); 1505 } 1506 1507 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1508 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1509 EXPECT_TRUE((bool)Obj); 1510 DWARFContextInMemory DwarfContext(*Obj.get()); 1511 1512 // Verify the number of compile units is correct. 1513 uint32_t NumCUs = DwarfContext.getNumCompileUnits(); 1514 EXPECT_EQ(NumCUs, 1u); 1515 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1516 1517 // Get the compile unit DIE is valid. 1518 auto CUDie = U->getUnitDIE(false); 1519 EXPECT_TRUE(CUDie.isValid()); 1520 1521 auto FuncSpecDie = CUDie.getFirstChild(); 1522 auto FuncDie = FuncSpecDie.getSibling(); 1523 1524 // Make sure that passing in an empty attribute list behave correctly. 1525 EXPECT_FALSE(FuncDie.find(ArrayRef<dwarf::Attribute>()).hasValue()); 1526 1527 // Make sure that passing in a list of attribute that are not contained 1528 // in the DIE returns nothing. 1529 EXPECT_FALSE(FuncDie.find({DW_AT_low_pc, DW_AT_entry_pc}).hasValue()); 1530 1531 const dwarf::Attribute Attrs[] = {DW_AT_linkage_name, 1532 DW_AT_MIPS_linkage_name}; 1533 1534 // Make sure we can't extract the linkage name attributes when using 1535 // DWARFDie::find() since it won't check the DW_AT_specification DIE. 1536 EXPECT_FALSE(FuncDie.find(Attrs).hasValue()); 1537 1538 // Make sure we can extract the name from the specification die when using 1539 // DWARFDie::findRecursively() since it should recurse through the 1540 // DW_AT_specification DIE. 1541 auto NameOpt = FuncDie.findRecursively(Attrs); 1542 EXPECT_TRUE(NameOpt.hasValue()); 1543 EXPECT_EQ(DieMangled, toString(NameOpt, "")); 1544 } 1545 1546 TEST(DWARFDebugInfo, TestImplicitConstAbbrevs) { 1547 uint16_t Version = 5; 1548 1549 const uint8_t AddrSize = sizeof(void *); 1550 initLLVMIfNeeded(); 1551 Triple Triple = getHostTripleForAddrSize(AddrSize); 1552 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1553 if (HandleExpectedError(ExpectedDG)) 1554 return; 1555 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1556 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1557 dwarfgen::DIE CUDie = CU.getUnitDIE(); 1558 const dwarf::Attribute Attr = DW_AT_lo_user; 1559 const int64_t Val1 = 42; 1560 const int64_t Val2 = 43; 1561 1562 auto FirstVal1DIE = CUDie.addChild(DW_TAG_class_type); 1563 FirstVal1DIE.addAttribute(Attr, DW_FORM_implicit_const, Val1); 1564 1565 auto SecondVal1DIE = CUDie.addChild(DW_TAG_class_type); 1566 SecondVal1DIE.addAttribute(Attr, DW_FORM_implicit_const, Val1); 1567 1568 auto Val2DIE = CUDie.addChild(DW_TAG_class_type); 1569 Val2DIE.addAttribute(Attr, DW_FORM_implicit_const, Val2); 1570 1571 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1572 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1573 EXPECT_TRUE((bool)Obj); 1574 DWARFContextInMemory DwarfContext(*Obj.get()); 1575 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0); 1576 EXPECT_TRUE((bool)U); 1577 1578 const auto *Abbrevs = U->getAbbreviations(); 1579 EXPECT_TRUE((bool)Abbrevs); 1580 1581 // Let's find implicit_const abbrevs and verify, 1582 // that there are exactly two of them and both of them 1583 // can be dumped correctly. 1584 typedef decltype(Abbrevs->begin()) AbbrevIt; 1585 AbbrevIt Val1Abbrev = Abbrevs->end(); 1586 AbbrevIt Val2Abbrev = Abbrevs->end(); 1587 for(auto it = Abbrevs->begin(); it != Abbrevs->end(); ++it) { 1588 if (it->getNumAttributes() == 0) 1589 continue; // root abbrev for DW_TAG_compile_unit 1590 1591 auto A = it->getAttrByIndex(0); 1592 EXPECT_EQ(A, Attr); 1593 1594 auto FormValue = it->getAttributeValue(/* offset */ 0, A, *U); 1595 EXPECT_TRUE((bool)FormValue); 1596 EXPECT_EQ(FormValue->getForm(), dwarf::DW_FORM_implicit_const); 1597 1598 const auto V = FormValue->getAsSignedConstant(); 1599 EXPECT_TRUE((bool)V); 1600 1601 auto VerifyAbbrevDump = [&V](AbbrevIt it) { 1602 std::string S; 1603 llvm::raw_string_ostream OS(S); 1604 it->dump(OS); 1605 auto FormPos = OS.str().find("DW_FORM_implicit_const"); 1606 EXPECT_NE(FormPos, std::string::npos); 1607 auto ValPos = S.find_first_of("-0123456789", FormPos); 1608 EXPECT_NE(ValPos, std::string::npos); 1609 int64_t Val = std::atoll(S.substr(ValPos).c_str()); 1610 EXPECT_EQ(Val, *V); 1611 }; 1612 1613 switch(*V) { 1614 case Val1: 1615 EXPECT_EQ(Val1Abbrev, Abbrevs->end()); 1616 Val1Abbrev = it; 1617 VerifyAbbrevDump(it); 1618 break; 1619 case Val2: 1620 EXPECT_EQ(Val2Abbrev, Abbrevs->end()); 1621 Val2Abbrev = it; 1622 VerifyAbbrevDump(it); 1623 break; 1624 default: 1625 FAIL() << "Unexpected attribute value: " << *V; 1626 } 1627 } 1628 1629 // Now let's make sure that two Val1-DIEs refer to the same abbrev, 1630 // and Val2-DIE refers to another one. 1631 auto DieDG = U->getUnitDIE(false); 1632 auto it = DieDG.begin(); 1633 std::multimap<int64_t, decltype(it->getAbbreviationDeclarationPtr())> DIEs; 1634 const DWARFAbbreviationDeclaration *AbbrevPtrVal1 = nullptr; 1635 const DWARFAbbreviationDeclaration *AbbrevPtrVal2 = nullptr; 1636 for (; it != DieDG.end(); ++it) { 1637 const auto *AbbrevPtr = it->getAbbreviationDeclarationPtr(); 1638 EXPECT_TRUE((bool)AbbrevPtr); 1639 auto FormValue = it->find(Attr); 1640 EXPECT_TRUE((bool)FormValue); 1641 const auto V = FormValue->getAsSignedConstant(); 1642 EXPECT_TRUE((bool)V); 1643 switch(*V) { 1644 case Val1: 1645 AbbrevPtrVal1 = AbbrevPtr; 1646 break; 1647 case Val2: 1648 AbbrevPtrVal2 = AbbrevPtr; 1649 break; 1650 default: 1651 FAIL() << "Unexpected attribute value: " << *V; 1652 } 1653 DIEs.insert(std::make_pair(*V, AbbrevPtr)); 1654 } 1655 EXPECT_EQ(DIEs.count(Val1), 2u); 1656 EXPECT_EQ(DIEs.count(Val2), 1u); 1657 auto Val1Range = DIEs.equal_range(Val1); 1658 for (auto it = Val1Range.first; it != Val1Range.second; ++it) 1659 EXPECT_EQ(it->second, AbbrevPtrVal1); 1660 EXPECT_EQ(DIEs.find(Val2)->second, AbbrevPtrVal2); 1661 } 1662 1663 } // end anonymous namespace 1664