1 //===- llvm/unittest/DebugInfo/DWARFDebugInfoTest.cpp ---------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "DwarfGenerator.h" 10 #include "DwarfUtils.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/CodeGen/AsmPrinter.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/DebugInfo/DWARF/DWARFVerifier.h" 23 #include "llvm/MC/MCContext.h" 24 #include "llvm/MC/MCSectionELF.h" 25 #include "llvm/MC/MCStreamer.h" 26 #include "llvm/Object/ObjectFile.h" 27 #include "llvm/ObjectYAML/DWARFEmitter.h" 28 #include "llvm/Support/Error.h" 29 #include "llvm/Support/MemoryBuffer.h" 30 #include "llvm/Support/TargetRegistry.h" 31 #include "llvm/Support/TargetSelect.h" 32 #include "llvm/Testing/Support/Error.h" 33 #include "gtest/gtest.h" 34 #include <string> 35 36 using namespace llvm; 37 using namespace dwarf; 38 using namespace utils; 39 40 namespace { 41 42 template <uint16_t Version, class AddrType, class RefAddrType> 43 void TestAllForms() { 44 Triple Triple = getDefaultTargetTripleForAddrSize(sizeof(AddrType)); 45 if (!isObjectEmissionSupported(Triple)) 46 return; 47 48 // Test that we can decode all DW_FORM values correctly. 49 const AddrType AddrValue = (AddrType)0x0123456789abcdefULL; 50 const uint8_t BlockData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; 51 const uint32_t BlockSize = sizeof(BlockData); 52 const RefAddrType RefAddr = 0x12345678; 53 const uint8_t Data1 = 0x01U; 54 const uint16_t Data2 = 0x2345U; 55 const uint32_t Data4 = 0x6789abcdU; 56 const uint64_t Data8 = 0x0011223344556677ULL; 57 const uint64_t Data8_2 = 0xAABBCCDDEEFF0011ULL; 58 const uint8_t Data16[16] = {1, 2, 3, 4, 5, 6, 7, 8, 59 9, 10, 11, 12, 13, 14, 15, 16}; 60 const int64_t SData = INT64_MIN; 61 const int64_t ICSData = INT64_MAX; // DW_FORM_implicit_const SData 62 const uint64_t UData[] = {UINT64_MAX - 1, UINT64_MAX - 2, UINT64_MAX - 3, 63 UINT64_MAX - 4, UINT64_MAX - 5, UINT64_MAX - 6, 64 UINT64_MAX - 7, UINT64_MAX - 8, UINT64_MAX - 9}; 65 #define UDATA_1 18446744073709551614ULL 66 const uint32_t Dwarf32Values[] = {1, 2, 3, 4, 5, 6, 7, 8}; 67 const char *StringValue = "Hello"; 68 const char *StrpValue = "World"; 69 const char *StrxValue = "Indexed"; 70 const char *Strx1Value = "Indexed1"; 71 const char *Strx2Value = "Indexed2"; 72 const char *Strx3Value = "Indexed3"; 73 const char *Strx4Value = "Indexed4"; 74 75 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 76 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded()); 77 dwarfgen::Generator *DG = ExpectedDG.get().get(); 78 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 79 dwarfgen::DIE CUDie = CU.getUnitDIE(); 80 81 if (Version >= 5) 82 CUDie.addStrOffsetsBaseAttribute(); 83 84 uint16_t Attr = DW_AT_lo_user; 85 86 //---------------------------------------------------------------------- 87 // Test address forms 88 //---------------------------------------------------------------------- 89 const auto Attr_DW_FORM_addr = static_cast<dwarf::Attribute>(Attr++); 90 CUDie.addAttribute(Attr_DW_FORM_addr, DW_FORM_addr, AddrValue); 91 92 //---------------------------------------------------------------------- 93 // Test block forms 94 //---------------------------------------------------------------------- 95 const auto Attr_DW_FORM_block = static_cast<dwarf::Attribute>(Attr++); 96 CUDie.addAttribute(Attr_DW_FORM_block, DW_FORM_block, BlockData, BlockSize); 97 98 const auto Attr_DW_FORM_block1 = static_cast<dwarf::Attribute>(Attr++); 99 CUDie.addAttribute(Attr_DW_FORM_block1, DW_FORM_block1, BlockData, BlockSize); 100 101 const auto Attr_DW_FORM_block2 = static_cast<dwarf::Attribute>(Attr++); 102 CUDie.addAttribute(Attr_DW_FORM_block2, DW_FORM_block2, BlockData, BlockSize); 103 104 const auto Attr_DW_FORM_block4 = static_cast<dwarf::Attribute>(Attr++); 105 CUDie.addAttribute(Attr_DW_FORM_block4, DW_FORM_block4, BlockData, BlockSize); 106 107 // We handle data16 as a block form. 108 const auto Attr_DW_FORM_data16 = static_cast<dwarf::Attribute>(Attr++); 109 if (Version >= 5) 110 CUDie.addAttribute(Attr_DW_FORM_data16, DW_FORM_data16, Data16, 16); 111 112 //---------------------------------------------------------------------- 113 // Test data forms 114 //---------------------------------------------------------------------- 115 const auto Attr_DW_FORM_data1 = static_cast<dwarf::Attribute>(Attr++); 116 CUDie.addAttribute(Attr_DW_FORM_data1, DW_FORM_data1, Data1); 117 118 const auto Attr_DW_FORM_data2 = static_cast<dwarf::Attribute>(Attr++); 119 CUDie.addAttribute(Attr_DW_FORM_data2, DW_FORM_data2, Data2); 120 121 const auto Attr_DW_FORM_data4 = static_cast<dwarf::Attribute>(Attr++); 122 CUDie.addAttribute(Attr_DW_FORM_data4, DW_FORM_data4, Data4); 123 124 const auto Attr_DW_FORM_data8 = static_cast<dwarf::Attribute>(Attr++); 125 CUDie.addAttribute(Attr_DW_FORM_data8, DW_FORM_data8, Data8); 126 127 //---------------------------------------------------------------------- 128 // Test string forms 129 //---------------------------------------------------------------------- 130 const auto Attr_DW_FORM_string = static_cast<dwarf::Attribute>(Attr++); 131 CUDie.addAttribute(Attr_DW_FORM_string, DW_FORM_string, StringValue); 132 133 const auto Attr_DW_FORM_strx = static_cast<dwarf::Attribute>(Attr++); 134 const auto Attr_DW_FORM_strx1 = static_cast<dwarf::Attribute>(Attr++); 135 const auto Attr_DW_FORM_strx2 = static_cast<dwarf::Attribute>(Attr++); 136 const auto Attr_DW_FORM_strx3 = static_cast<dwarf::Attribute>(Attr++); 137 const auto Attr_DW_FORM_strx4 = static_cast<dwarf::Attribute>(Attr++); 138 if (Version >= 5) { 139 CUDie.addAttribute(Attr_DW_FORM_strx, DW_FORM_strx, StrxValue); 140 CUDie.addAttribute(Attr_DW_FORM_strx1, DW_FORM_strx1, Strx1Value); 141 CUDie.addAttribute(Attr_DW_FORM_strx2, DW_FORM_strx2, Strx2Value); 142 CUDie.addAttribute(Attr_DW_FORM_strx3, DW_FORM_strx3, Strx3Value); 143 CUDie.addAttribute(Attr_DW_FORM_strx4, DW_FORM_strx4, Strx4Value); 144 } 145 146 const auto Attr_DW_FORM_strp = static_cast<dwarf::Attribute>(Attr++); 147 CUDie.addAttribute(Attr_DW_FORM_strp, DW_FORM_strp, StrpValue); 148 149 //---------------------------------------------------------------------- 150 // Test reference forms 151 //---------------------------------------------------------------------- 152 const auto Attr_DW_FORM_ref_addr = static_cast<dwarf::Attribute>(Attr++); 153 CUDie.addAttribute(Attr_DW_FORM_ref_addr, DW_FORM_ref_addr, RefAddr); 154 155 const auto Attr_DW_FORM_ref1 = static_cast<dwarf::Attribute>(Attr++); 156 CUDie.addAttribute(Attr_DW_FORM_ref1, DW_FORM_ref1, Data1); 157 158 const auto Attr_DW_FORM_ref2 = static_cast<dwarf::Attribute>(Attr++); 159 CUDie.addAttribute(Attr_DW_FORM_ref2, DW_FORM_ref2, Data2); 160 161 const auto Attr_DW_FORM_ref4 = static_cast<dwarf::Attribute>(Attr++); 162 CUDie.addAttribute(Attr_DW_FORM_ref4, DW_FORM_ref4, Data4); 163 164 const auto Attr_DW_FORM_ref8 = static_cast<dwarf::Attribute>(Attr++); 165 CUDie.addAttribute(Attr_DW_FORM_ref8, DW_FORM_ref8, Data8); 166 167 const auto Attr_DW_FORM_ref_sig8 = static_cast<dwarf::Attribute>(Attr++); 168 if (Version >= 4) 169 CUDie.addAttribute(Attr_DW_FORM_ref_sig8, DW_FORM_ref_sig8, Data8_2); 170 171 const auto Attr_DW_FORM_ref_udata = static_cast<dwarf::Attribute>(Attr++); 172 CUDie.addAttribute(Attr_DW_FORM_ref_udata, DW_FORM_ref_udata, UData[0]); 173 174 //---------------------------------------------------------------------- 175 // Test flag forms 176 //---------------------------------------------------------------------- 177 const auto Attr_DW_FORM_flag_true = static_cast<dwarf::Attribute>(Attr++); 178 CUDie.addAttribute(Attr_DW_FORM_flag_true, DW_FORM_flag, true); 179 180 const auto Attr_DW_FORM_flag_false = static_cast<dwarf::Attribute>(Attr++); 181 CUDie.addAttribute(Attr_DW_FORM_flag_false, DW_FORM_flag, false); 182 183 const auto Attr_DW_FORM_flag_present = static_cast<dwarf::Attribute>(Attr++); 184 if (Version >= 4) 185 CUDie.addAttribute(Attr_DW_FORM_flag_present, DW_FORM_flag_present); 186 187 //---------------------------------------------------------------------- 188 // Test SLEB128 based forms 189 //---------------------------------------------------------------------- 190 const auto Attr_DW_FORM_sdata = static_cast<dwarf::Attribute>(Attr++); 191 CUDie.addAttribute(Attr_DW_FORM_sdata, DW_FORM_sdata, SData); 192 193 const auto Attr_DW_FORM_implicit_const = 194 static_cast<dwarf::Attribute>(Attr++); 195 if (Version >= 5) 196 CUDie.addAttribute(Attr_DW_FORM_implicit_const, DW_FORM_implicit_const, 197 ICSData); 198 199 //---------------------------------------------------------------------- 200 // Test ULEB128 based forms 201 //---------------------------------------------------------------------- 202 const auto Attr_DW_FORM_udata = static_cast<dwarf::Attribute>(Attr++); 203 CUDie.addAttribute(Attr_DW_FORM_udata, DW_FORM_udata, UData[0]); 204 205 //---------------------------------------------------------------------- 206 // Test DWARF32/DWARF64 forms 207 //---------------------------------------------------------------------- 208 const auto Attr_DW_FORM_GNU_ref_alt = static_cast<dwarf::Attribute>(Attr++); 209 CUDie.addAttribute(Attr_DW_FORM_GNU_ref_alt, DW_FORM_GNU_ref_alt, 210 Dwarf32Values[0]); 211 212 const auto Attr_DW_FORM_sec_offset = static_cast<dwarf::Attribute>(Attr++); 213 if (Version >= 4) 214 CUDie.addAttribute(Attr_DW_FORM_sec_offset, DW_FORM_sec_offset, 215 Dwarf32Values[1]); 216 217 //---------------------------------------------------------------------- 218 // Add an address at the end to make sure we can decode this value 219 //---------------------------------------------------------------------- 220 const auto Attr_Last = static_cast<dwarf::Attribute>(Attr++); 221 CUDie.addAttribute(Attr_Last, DW_FORM_addr, AddrValue); 222 223 //---------------------------------------------------------------------- 224 // Generate the DWARF 225 //---------------------------------------------------------------------- 226 StringRef FileBytes = DG->generate(); 227 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 228 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 229 EXPECT_TRUE((bool)Obj); 230 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj); 231 uint32_t NumCUs = DwarfContext->getNumCompileUnits(); 232 EXPECT_EQ(NumCUs, 1u); 233 DWARFCompileUnit *U = 234 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0)); 235 auto DieDG = U->getUnitDIE(false); 236 EXPECT_TRUE(DieDG.isValid()); 237 238 //---------------------------------------------------------------------- 239 // Test address forms 240 //---------------------------------------------------------------------- 241 EXPECT_EQ(AddrValue, toAddress(DieDG.find(Attr_DW_FORM_addr), 0)); 242 243 //---------------------------------------------------------------------- 244 // Test block forms 245 //---------------------------------------------------------------------- 246 Optional<DWARFFormValue> FormValue; 247 ArrayRef<uint8_t> ExtractedBlockData; 248 Optional<ArrayRef<uint8_t>> BlockDataOpt; 249 250 FormValue = DieDG.find(Attr_DW_FORM_block); 251 EXPECT_TRUE((bool)FormValue); 252 BlockDataOpt = FormValue->getAsBlock(); 253 EXPECT_TRUE(BlockDataOpt.hasValue()); 254 ExtractedBlockData = BlockDataOpt.getValue(); 255 EXPECT_EQ(ExtractedBlockData.size(), BlockSize); 256 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0); 257 258 FormValue = DieDG.find(Attr_DW_FORM_block1); 259 EXPECT_TRUE((bool)FormValue); 260 BlockDataOpt = FormValue->getAsBlock(); 261 EXPECT_TRUE(BlockDataOpt.hasValue()); 262 ExtractedBlockData = BlockDataOpt.getValue(); 263 EXPECT_EQ(ExtractedBlockData.size(), BlockSize); 264 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0); 265 266 FormValue = DieDG.find(Attr_DW_FORM_block2); 267 EXPECT_TRUE((bool)FormValue); 268 BlockDataOpt = FormValue->getAsBlock(); 269 EXPECT_TRUE(BlockDataOpt.hasValue()); 270 ExtractedBlockData = BlockDataOpt.getValue(); 271 EXPECT_EQ(ExtractedBlockData.size(), BlockSize); 272 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0); 273 274 FormValue = DieDG.find(Attr_DW_FORM_block4); 275 EXPECT_TRUE((bool)FormValue); 276 BlockDataOpt = FormValue->getAsBlock(); 277 EXPECT_TRUE(BlockDataOpt.hasValue()); 278 ExtractedBlockData = BlockDataOpt.getValue(); 279 EXPECT_EQ(ExtractedBlockData.size(), BlockSize); 280 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0); 281 282 // Data16 is handled like a block. 283 if (Version >= 5) { 284 FormValue = DieDG.find(Attr_DW_FORM_data16); 285 EXPECT_TRUE((bool)FormValue); 286 BlockDataOpt = FormValue->getAsBlock(); 287 EXPECT_TRUE(BlockDataOpt.hasValue()); 288 ExtractedBlockData = BlockDataOpt.getValue(); 289 EXPECT_EQ(ExtractedBlockData.size(), 16u); 290 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), Data16, 16) == 0); 291 } 292 293 //---------------------------------------------------------------------- 294 // Test data forms 295 //---------------------------------------------------------------------- 296 EXPECT_EQ(Data1, toUnsigned(DieDG.find(Attr_DW_FORM_data1), 0)); 297 EXPECT_EQ(Data2, toUnsigned(DieDG.find(Attr_DW_FORM_data2), 0)); 298 EXPECT_EQ(Data4, toUnsigned(DieDG.find(Attr_DW_FORM_data4), 0)); 299 EXPECT_EQ(Data8, toUnsigned(DieDG.find(Attr_DW_FORM_data8), 0)); 300 301 //---------------------------------------------------------------------- 302 // Test string forms 303 //---------------------------------------------------------------------- 304 auto ExtractedStringValue = toString(DieDG.find(Attr_DW_FORM_string)); 305 EXPECT_TRUE((bool)ExtractedStringValue); 306 EXPECT_STREQ(StringValue, *ExtractedStringValue); 307 308 if (Version >= 5) { 309 auto ExtractedStrxValue = toString(DieDG.find(Attr_DW_FORM_strx)); 310 EXPECT_TRUE((bool)ExtractedStrxValue); 311 EXPECT_STREQ(StrxValue, *ExtractedStrxValue); 312 313 auto ExtractedStrx1Value = toString(DieDG.find(Attr_DW_FORM_strx1)); 314 EXPECT_TRUE((bool)ExtractedStrx1Value); 315 EXPECT_STREQ(Strx1Value, *ExtractedStrx1Value); 316 317 auto ExtractedStrx2Value = toString(DieDG.find(Attr_DW_FORM_strx2)); 318 EXPECT_TRUE((bool)ExtractedStrx2Value); 319 EXPECT_STREQ(Strx2Value, *ExtractedStrx2Value); 320 321 auto ExtractedStrx3Value = toString(DieDG.find(Attr_DW_FORM_strx3)); 322 EXPECT_TRUE((bool)ExtractedStrx3Value); 323 EXPECT_STREQ(Strx3Value, *ExtractedStrx3Value); 324 325 auto ExtractedStrx4Value = toString(DieDG.find(Attr_DW_FORM_strx4)); 326 EXPECT_TRUE((bool)ExtractedStrx4Value); 327 EXPECT_STREQ(Strx4Value, *ExtractedStrx4Value); 328 } 329 330 auto ExtractedStrpValue = toString(DieDG.find(Attr_DW_FORM_strp)); 331 EXPECT_TRUE((bool)ExtractedStrpValue); 332 EXPECT_STREQ(StrpValue, *ExtractedStrpValue); 333 334 //---------------------------------------------------------------------- 335 // Test reference forms 336 //---------------------------------------------------------------------- 337 EXPECT_EQ(RefAddr, toReference(DieDG.find(Attr_DW_FORM_ref_addr), 0)); 338 EXPECT_EQ(Data1, toReference(DieDG.find(Attr_DW_FORM_ref1), 0)); 339 EXPECT_EQ(Data2, toReference(DieDG.find(Attr_DW_FORM_ref2), 0)); 340 EXPECT_EQ(Data4, toReference(DieDG.find(Attr_DW_FORM_ref4), 0)); 341 EXPECT_EQ(Data8, toReference(DieDG.find(Attr_DW_FORM_ref8), 0)); 342 if (Version >= 4) { 343 EXPECT_EQ(Data8_2, toReference(DieDG.find(Attr_DW_FORM_ref_sig8), 0)); 344 } 345 EXPECT_EQ(UData[0], toReference(DieDG.find(Attr_DW_FORM_ref_udata), 0)); 346 347 //---------------------------------------------------------------------- 348 // Test flag forms 349 //---------------------------------------------------------------------- 350 EXPECT_EQ(1ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_true), 0)); 351 EXPECT_EQ(0ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_false), 1)); 352 if (Version >= 4) { 353 EXPECT_EQ(1ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_present), 0)); 354 } 355 356 //---------------------------------------------------------------------- 357 // Test SLEB128 based forms 358 //---------------------------------------------------------------------- 359 EXPECT_EQ(SData, toSigned(DieDG.find(Attr_DW_FORM_sdata), 0)); 360 if (Version >= 5) { 361 EXPECT_EQ(ICSData, toSigned(DieDG.find(Attr_DW_FORM_implicit_const), 0)); 362 } 363 364 //---------------------------------------------------------------------- 365 // Test ULEB128 based forms 366 //---------------------------------------------------------------------- 367 EXPECT_EQ(UData[0], toUnsigned(DieDG.find(Attr_DW_FORM_udata), 0)); 368 369 //---------------------------------------------------------------------- 370 // Test DWARF32/DWARF64 forms 371 //---------------------------------------------------------------------- 372 EXPECT_EQ(Dwarf32Values[0], 373 toReference(DieDG.find(Attr_DW_FORM_GNU_ref_alt), 0)); 374 if (Version >= 4) { 375 EXPECT_EQ(Dwarf32Values[1], 376 toSectionOffset(DieDG.find(Attr_DW_FORM_sec_offset), 0)); 377 } 378 379 //---------------------------------------------------------------------- 380 // Add an address at the end to make sure we can decode this value 381 //---------------------------------------------------------------------- 382 EXPECT_EQ(AddrValue, toAddress(DieDG.find(Attr_Last), 0)); 383 } 384 385 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4AllForms) { 386 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 387 // addresses. 388 typedef uint32_t AddrType; 389 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2. 390 typedef AddrType RefAddrType; 391 TestAllForms<2, AddrType, RefAddrType>(); 392 } 393 394 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8AllForms) { 395 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 396 // addresses. 397 typedef uint64_t AddrType; 398 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2. 399 typedef AddrType RefAddrType; 400 TestAllForms<2, AddrType, RefAddrType>(); 401 } 402 403 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4AllForms) { 404 // Test that we can decode all forms for DWARF32, version 3, with 4 byte 405 // addresses. 406 typedef uint32_t AddrType; 407 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later. 408 typedef uint32_t RefAddrType; 409 TestAllForms<3, AddrType, RefAddrType>(); 410 } 411 412 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8AllForms) { 413 // Test that we can decode all forms for DWARF32, version 3, with 8 byte 414 // addresses. 415 typedef uint64_t AddrType; 416 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later 417 typedef uint32_t RefAddrType; 418 TestAllForms<3, AddrType, RefAddrType>(); 419 } 420 421 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4AllForms) { 422 // Test that we can decode all forms for DWARF32, version 4, with 4 byte 423 // addresses. 424 typedef uint32_t AddrType; 425 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later 426 typedef uint32_t RefAddrType; 427 TestAllForms<4, AddrType, RefAddrType>(); 428 } 429 430 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8AllForms) { 431 // Test that we can decode all forms for DWARF32, version 4, with 8 byte 432 // addresses. 433 typedef uint64_t AddrType; 434 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later 435 typedef uint32_t RefAddrType; 436 TestAllForms<4, AddrType, RefAddrType>(); 437 } 438 439 TEST(DWARFDebugInfo, TestDWARF32Version5Addr4AllForms) { 440 // Test that we can decode all forms for DWARF32, version 5, with 4 byte 441 // addresses. 442 typedef uint32_t AddrType; 443 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later 444 typedef uint32_t RefAddrType; 445 TestAllForms<5, AddrType, RefAddrType>(); 446 } 447 448 TEST(DWARFDebugInfo, TestDWARF32Version5Addr8AllForms) { 449 // Test that we can decode all forms for DWARF32, version 5, with 8 byte 450 // addresses. 451 typedef uint64_t AddrType; 452 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later 453 typedef uint32_t RefAddrType; 454 TestAllForms<5, AddrType, RefAddrType>(); 455 } 456 457 template <uint16_t Version, class AddrType> void TestChildren() { 458 Triple Triple = getDefaultTargetTripleForAddrSize(sizeof(AddrType)); 459 if (!isObjectEmissionSupported(Triple)) 460 return; 461 462 // Test that we can decode DW_FORM_ref_addr values correctly in DWARF 2 with 463 // 4 byte addresses. DW_FORM_ref_addr values should be 4 bytes when using 464 // 8 byte addresses. 465 466 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 467 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded()); 468 dwarfgen::Generator *DG = ExpectedDG.get().get(); 469 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 470 dwarfgen::DIE CUDie = CU.getUnitDIE(); 471 472 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c"); 473 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 474 475 dwarfgen::DIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram); 476 SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main"); 477 SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U); 478 SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U); 479 480 dwarfgen::DIE IntDie = CUDie.addChild(DW_TAG_base_type); 481 IntDie.addAttribute(DW_AT_name, DW_FORM_strp, "int"); 482 IntDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed); 483 IntDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4); 484 485 dwarfgen::DIE ArgcDie = SubprogramDie.addChild(DW_TAG_formal_parameter); 486 ArgcDie.addAttribute(DW_AT_name, DW_FORM_strp, "argc"); 487 // ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref4, IntDie); 488 ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, IntDie); 489 490 StringRef FileBytes = DG->generate(); 491 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 492 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 493 EXPECT_TRUE((bool)Obj); 494 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj); 495 496 // Verify the number of compile units is correct. 497 uint32_t NumCUs = DwarfContext->getNumCompileUnits(); 498 EXPECT_EQ(NumCUs, 1u); 499 DWARFCompileUnit *U = 500 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0)); 501 502 // Get the compile unit DIE is valid. 503 auto DieDG = U->getUnitDIE(false); 504 EXPECT_TRUE(DieDG.isValid()); 505 506 // Verify the first child of the compile unit DIE is our subprogram. 507 auto SubprogramDieDG = DieDG.getFirstChild(); 508 EXPECT_TRUE(SubprogramDieDG.isValid()); 509 EXPECT_EQ(SubprogramDieDG.getTag(), DW_TAG_subprogram); 510 511 // Verify the first child of the subprogram is our formal parameter. 512 auto ArgcDieDG = SubprogramDieDG.getFirstChild(); 513 EXPECT_TRUE(ArgcDieDG.isValid()); 514 EXPECT_EQ(ArgcDieDG.getTag(), DW_TAG_formal_parameter); 515 516 // Verify our formal parameter has a NULL tag sibling. 517 auto NullDieDG = ArgcDieDG.getSibling(); 518 EXPECT_TRUE(NullDieDG.isValid()); 519 if (NullDieDG) { 520 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null); 521 EXPECT_TRUE(!NullDieDG.getSibling().isValid()); 522 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid()); 523 } 524 525 // Verify the sibling of our subprogram is our integer base type. 526 auto IntDieDG = SubprogramDieDG.getSibling(); 527 EXPECT_TRUE(IntDieDG.isValid()); 528 EXPECT_EQ(IntDieDG.getTag(), DW_TAG_base_type); 529 530 // Verify the sibling of our subprogram is our integer base is a NULL tag. 531 NullDieDG = IntDieDG.getSibling(); 532 EXPECT_TRUE(NullDieDG.isValid()); 533 if (NullDieDG) { 534 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null); 535 EXPECT_TRUE(!NullDieDG.getSibling().isValid()); 536 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid()); 537 } 538 539 // Verify the previous sibling of our subprogram is our integer base type. 540 IntDieDG = NullDieDG.getPreviousSibling(); 541 EXPECT_TRUE(IntDieDG.isValid()); 542 EXPECT_EQ(IntDieDG.getTag(), DW_TAG_base_type); 543 } 544 545 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Children) { 546 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 547 // addresses. 548 typedef uint32_t AddrType; 549 TestChildren<2, AddrType>(); 550 } 551 552 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Children) { 553 // Test that we can decode all forms for DWARF32, version 2, with 8 byte 554 // addresses. 555 typedef uint64_t AddrType; 556 TestChildren<2, AddrType>(); 557 } 558 559 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Children) { 560 // Test that we can decode all forms for DWARF32, version 3, with 4 byte 561 // addresses. 562 typedef uint32_t AddrType; 563 TestChildren<3, AddrType>(); 564 } 565 566 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Children) { 567 // Test that we can decode all forms for DWARF32, version 3, with 8 byte 568 // addresses. 569 typedef uint64_t AddrType; 570 TestChildren<3, AddrType>(); 571 } 572 573 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Children) { 574 // Test that we can decode all forms for DWARF32, version 4, with 4 byte 575 // addresses. 576 typedef uint32_t AddrType; 577 TestChildren<4, AddrType>(); 578 } 579 580 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Children) { 581 // Test that we can decode all forms for DWARF32, version 4, with 8 byte 582 // addresses. 583 typedef uint64_t AddrType; 584 TestChildren<4, AddrType>(); 585 } 586 587 template <uint16_t Version, class AddrType> void TestReferences() { 588 Triple Triple = getDefaultTargetTripleForAddrSize(sizeof(AddrType)); 589 if (!isObjectEmissionSupported(Triple)) 590 return; 591 592 // Test that we can decode DW_FORM_refXXX values correctly in DWARF. 593 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 594 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded()); 595 dwarfgen::Generator *DG = ExpectedDG.get().get(); 596 dwarfgen::CompileUnit &CU1 = DG->addCompileUnit(); 597 dwarfgen::CompileUnit &CU2 = DG->addCompileUnit(); 598 599 dwarfgen::DIE CU1Die = CU1.getUnitDIE(); 600 CU1Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c"); 601 CU1Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 602 603 dwarfgen::DIE CU1TypeDie = CU1Die.addChild(DW_TAG_base_type); 604 CU1TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "int"); 605 CU1TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed); 606 CU1TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4); 607 608 dwarfgen::DIE CU1Ref1Die = CU1Die.addChild(DW_TAG_variable); 609 CU1Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref1"); 610 CU1Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU1TypeDie); 611 612 dwarfgen::DIE CU1Ref2Die = CU1Die.addChild(DW_TAG_variable); 613 CU1Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref2"); 614 CU1Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU1TypeDie); 615 616 dwarfgen::DIE CU1Ref4Die = CU1Die.addChild(DW_TAG_variable); 617 CU1Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref4"); 618 CU1Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU1TypeDie); 619 620 dwarfgen::DIE CU1Ref8Die = CU1Die.addChild(DW_TAG_variable); 621 CU1Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref8"); 622 CU1Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU1TypeDie); 623 624 dwarfgen::DIE CU1RefAddrDie = CU1Die.addChild(DW_TAG_variable); 625 CU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1RefAddr"); 626 CU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie); 627 628 dwarfgen::DIE CU2Die = CU2.getUnitDIE(); 629 CU2Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/foo.c"); 630 CU2Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 631 632 dwarfgen::DIE CU2TypeDie = CU2Die.addChild(DW_TAG_base_type); 633 CU2TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "float"); 634 CU2TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_float); 635 CU2TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4); 636 637 dwarfgen::DIE CU2Ref1Die = CU2Die.addChild(DW_TAG_variable); 638 CU2Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref1"); 639 CU2Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU2TypeDie); 640 641 dwarfgen::DIE CU2Ref2Die = CU2Die.addChild(DW_TAG_variable); 642 CU2Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref2"); 643 CU2Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU2TypeDie); 644 645 dwarfgen::DIE CU2Ref4Die = CU2Die.addChild(DW_TAG_variable); 646 CU2Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref4"); 647 CU2Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU2TypeDie); 648 649 dwarfgen::DIE CU2Ref8Die = CU2Die.addChild(DW_TAG_variable); 650 CU2Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref8"); 651 CU2Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU2TypeDie); 652 653 dwarfgen::DIE CU2RefAddrDie = CU2Die.addChild(DW_TAG_variable); 654 CU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2RefAddr"); 655 CU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie); 656 657 // Refer to a type in CU1 from CU2 658 dwarfgen::DIE CU2ToCU1RefAddrDie = CU2Die.addChild(DW_TAG_variable); 659 CU2ToCU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2ToCU1RefAddr"); 660 CU2ToCU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie); 661 662 // Refer to a type in CU2 from CU1 663 dwarfgen::DIE CU1ToCU2RefAddrDie = CU1Die.addChild(DW_TAG_variable); 664 CU1ToCU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1ToCU2RefAddr"); 665 CU1ToCU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie); 666 667 StringRef FileBytes = DG->generate(); 668 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 669 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 670 EXPECT_TRUE((bool)Obj); 671 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj); 672 673 // Verify the number of compile units is correct. 674 uint32_t NumCUs = DwarfContext->getNumCompileUnits(); 675 EXPECT_EQ(NumCUs, 2u); 676 DWARFCompileUnit *U1 = 677 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0)); 678 DWARFCompileUnit *U2 = 679 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(1)); 680 681 // Get the compile unit DIE is valid. 682 auto Unit1DieDG = U1->getUnitDIE(false); 683 EXPECT_TRUE(Unit1DieDG.isValid()); 684 685 auto Unit2DieDG = U2->getUnitDIE(false); 686 EXPECT_TRUE(Unit2DieDG.isValid()); 687 688 // Verify the first child of the compile unit 1 DIE is our int base type. 689 auto CU1TypeDieDG = Unit1DieDG.getFirstChild(); 690 EXPECT_TRUE(CU1TypeDieDG.isValid()); 691 EXPECT_EQ(CU1TypeDieDG.getTag(), DW_TAG_base_type); 692 EXPECT_EQ(DW_ATE_signed, toUnsigned(CU1TypeDieDG.find(DW_AT_encoding), 0)); 693 694 // Verify the first child of the compile unit 2 DIE is our float base type. 695 auto CU2TypeDieDG = Unit2DieDG.getFirstChild(); 696 EXPECT_TRUE(CU2TypeDieDG.isValid()); 697 EXPECT_EQ(CU2TypeDieDG.getTag(), DW_TAG_base_type); 698 EXPECT_EQ(DW_ATE_float, toUnsigned(CU2TypeDieDG.find(DW_AT_encoding), 0)); 699 700 // Verify the sibling of the base type DIE is our Ref1 DIE and that its 701 // DW_AT_type points to our base type DIE. 702 auto CU1Ref1DieDG = CU1TypeDieDG.getSibling(); 703 EXPECT_TRUE(CU1Ref1DieDG.isValid()); 704 EXPECT_EQ(CU1Ref1DieDG.getTag(), DW_TAG_variable); 705 EXPECT_EQ(CU1TypeDieDG.getOffset(), 706 toReference(CU1Ref1DieDG.find(DW_AT_type), -1ULL)); 707 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our 708 // base type DIE in CU1. 709 auto CU1Ref2DieDG = CU1Ref1DieDG.getSibling(); 710 EXPECT_TRUE(CU1Ref2DieDG.isValid()); 711 EXPECT_EQ(CU1Ref2DieDG.getTag(), DW_TAG_variable); 712 EXPECT_EQ(CU1TypeDieDG.getOffset(), 713 toReference(CU1Ref2DieDG.find(DW_AT_type), -1ULL)); 714 715 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our 716 // base type DIE in CU1. 717 auto CU1Ref4DieDG = CU1Ref2DieDG.getSibling(); 718 EXPECT_TRUE(CU1Ref4DieDG.isValid()); 719 EXPECT_EQ(CU1Ref4DieDG.getTag(), DW_TAG_variable); 720 EXPECT_EQ(CU1TypeDieDG.getOffset(), 721 toReference(CU1Ref4DieDG.find(DW_AT_type), -1ULL)); 722 723 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our 724 // base type DIE in CU1. 725 auto CU1Ref8DieDG = CU1Ref4DieDG.getSibling(); 726 EXPECT_TRUE(CU1Ref8DieDG.isValid()); 727 EXPECT_EQ(CU1Ref8DieDG.getTag(), DW_TAG_variable); 728 EXPECT_EQ(CU1TypeDieDG.getOffset(), 729 toReference(CU1Ref8DieDG.find(DW_AT_type), -1ULL)); 730 731 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our 732 // base type DIE in CU1. 733 auto CU1RefAddrDieDG = CU1Ref8DieDG.getSibling(); 734 EXPECT_TRUE(CU1RefAddrDieDG.isValid()); 735 EXPECT_EQ(CU1RefAddrDieDG.getTag(), DW_TAG_variable); 736 EXPECT_EQ(CU1TypeDieDG.getOffset(), 737 toReference(CU1RefAddrDieDG.find(DW_AT_type), -1ULL)); 738 739 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its 740 // DW_AT_type points to our base type DIE. 741 auto CU1ToCU2RefAddrDieDG = CU1RefAddrDieDG.getSibling(); 742 EXPECT_TRUE(CU1ToCU2RefAddrDieDG.isValid()); 743 EXPECT_EQ(CU1ToCU2RefAddrDieDG.getTag(), DW_TAG_variable); 744 EXPECT_EQ(CU2TypeDieDG.getOffset(), 745 toReference(CU1ToCU2RefAddrDieDG.find(DW_AT_type), -1ULL)); 746 747 // Verify the sibling of the base type DIE is our Ref1 DIE and that its 748 // DW_AT_type points to our base type DIE. 749 auto CU2Ref1DieDG = CU2TypeDieDG.getSibling(); 750 EXPECT_TRUE(CU2Ref1DieDG.isValid()); 751 EXPECT_EQ(CU2Ref1DieDG.getTag(), DW_TAG_variable); 752 EXPECT_EQ(CU2TypeDieDG.getOffset(), 753 toReference(CU2Ref1DieDG.find(DW_AT_type), -1ULL)); 754 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our 755 // base type DIE in CU2. 756 auto CU2Ref2DieDG = CU2Ref1DieDG.getSibling(); 757 EXPECT_TRUE(CU2Ref2DieDG.isValid()); 758 EXPECT_EQ(CU2Ref2DieDG.getTag(), DW_TAG_variable); 759 EXPECT_EQ(CU2TypeDieDG.getOffset(), 760 toReference(CU2Ref2DieDG.find(DW_AT_type), -1ULL)); 761 762 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our 763 // base type DIE in CU2. 764 auto CU2Ref4DieDG = CU2Ref2DieDG.getSibling(); 765 EXPECT_TRUE(CU2Ref4DieDG.isValid()); 766 EXPECT_EQ(CU2Ref4DieDG.getTag(), DW_TAG_variable); 767 EXPECT_EQ(CU2TypeDieDG.getOffset(), 768 toReference(CU2Ref4DieDG.find(DW_AT_type), -1ULL)); 769 770 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our 771 // base type DIE in CU2. 772 auto CU2Ref8DieDG = CU2Ref4DieDG.getSibling(); 773 EXPECT_TRUE(CU2Ref8DieDG.isValid()); 774 EXPECT_EQ(CU2Ref8DieDG.getTag(), DW_TAG_variable); 775 EXPECT_EQ(CU2TypeDieDG.getOffset(), 776 toReference(CU2Ref8DieDG.find(DW_AT_type), -1ULL)); 777 778 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our 779 // base type DIE in CU2. 780 auto CU2RefAddrDieDG = CU2Ref8DieDG.getSibling(); 781 EXPECT_TRUE(CU2RefAddrDieDG.isValid()); 782 EXPECT_EQ(CU2RefAddrDieDG.getTag(), DW_TAG_variable); 783 EXPECT_EQ(CU2TypeDieDG.getOffset(), 784 toReference(CU2RefAddrDieDG.find(DW_AT_type), -1ULL)); 785 786 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its 787 // DW_AT_type points to our base type DIE. 788 auto CU2ToCU1RefAddrDieDG = CU2RefAddrDieDG.getSibling(); 789 EXPECT_TRUE(CU2ToCU1RefAddrDieDG.isValid()); 790 EXPECT_EQ(CU2ToCU1RefAddrDieDG.getTag(), DW_TAG_variable); 791 EXPECT_EQ(CU1TypeDieDG.getOffset(), 792 toReference(CU2ToCU1RefAddrDieDG.find(DW_AT_type), -1ULL)); 793 } 794 795 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4References) { 796 // Test that we can decode all forms for DWARF32, version 2, with 4 byte 797 // addresses. 798 typedef uint32_t AddrType; 799 TestReferences<2, AddrType>(); 800 } 801 802 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8References) { 803 // Test that we can decode all forms for DWARF32, version 2, with 8 byte 804 // addresses. 805 typedef uint64_t AddrType; 806 TestReferences<2, AddrType>(); 807 } 808 809 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4References) { 810 // Test that we can decode all forms for DWARF32, version 3, with 4 byte 811 // addresses. 812 typedef uint32_t AddrType; 813 TestReferences<3, AddrType>(); 814 } 815 816 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8References) { 817 // Test that we can decode all forms for DWARF32, version 3, with 8 byte 818 // addresses. 819 typedef uint64_t AddrType; 820 TestReferences<3, AddrType>(); 821 } 822 823 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4References) { 824 // Test that we can decode all forms for DWARF32, version 4, with 4 byte 825 // addresses. 826 typedef uint32_t AddrType; 827 TestReferences<4, AddrType>(); 828 } 829 830 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8References) { 831 // Test that we can decode all forms for DWARF32, version 4, with 8 byte 832 // addresses. 833 typedef uint64_t AddrType; 834 TestReferences<4, AddrType>(); 835 } 836 837 template <uint16_t Version, class AddrType> void TestAddresses() { 838 Triple Triple = getDefaultTargetTripleForAddrSize(sizeof(AddrType)); 839 if (!isObjectEmissionSupported(Triple)) 840 return; 841 842 // Test the DWARF APIs related to accessing the DW_AT_low_pc and 843 // DW_AT_high_pc. 844 const bool SupportsHighPCAsOffset = Version >= 4; 845 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 846 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded()); 847 dwarfgen::Generator *DG = ExpectedDG.get().get(); 848 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 849 dwarfgen::DIE CUDie = CU.getUnitDIE(); 850 851 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c"); 852 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); 853 854 // Create a subprogram DIE with no low or high PC. 855 dwarfgen::DIE SubprogramNoPC = CUDie.addChild(DW_TAG_subprogram); 856 SubprogramNoPC.addAttribute(DW_AT_name, DW_FORM_strp, "no_pc"); 857 858 // Create a subprogram DIE with a low PC only. 859 dwarfgen::DIE SubprogramLowPC = CUDie.addChild(DW_TAG_subprogram); 860 SubprogramLowPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_pc"); 861 const uint64_t ActualLowPC = 0x1000; 862 const uint64_t ActualHighPC = 0x2000; 863 const uint64_t ActualHighPCOffset = ActualHighPC - ActualLowPC; 864 SubprogramLowPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC); 865 866 // Create a subprogram DIE with a low and high PC. 867 dwarfgen::DIE SubprogramLowHighPC = CUDie.addChild(DW_TAG_subprogram); 868 SubprogramLowHighPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_high_pc"); 869 SubprogramLowHighPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC); 870 // Encode the high PC as an offset from the low PC if supported. 871 if (SupportsHighPCAsOffset) 872 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_data4, 873 ActualHighPCOffset); 874 else 875 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_addr, ActualHighPC); 876 877 StringRef FileBytes = DG->generate(); 878 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 879 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 880 EXPECT_TRUE((bool)Obj); 881 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj); 882 883 // Verify the number of compile units is correct. 884 uint32_t NumCUs = DwarfContext->getNumCompileUnits(); 885 EXPECT_EQ(NumCUs, 1u); 886 DWARFCompileUnit *U = 887 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0)); 888 889 // Get the compile unit DIE is valid. 890 auto DieDG = U->getUnitDIE(false); 891 EXPECT_TRUE(DieDG.isValid()); 892 893 uint64_t LowPC, HighPC, SectionIndex; 894 Optional<uint64_t> OptU64; 895 // Verify the that our subprogram with no PC value fails appropriately when 896 // asked for any PC values. 897 auto SubprogramDieNoPC = DieDG.getFirstChild(); 898 EXPECT_TRUE(SubprogramDieNoPC.isValid()); 899 EXPECT_EQ(SubprogramDieNoPC.getTag(), DW_TAG_subprogram); 900 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_low_pc)); 901 EXPECT_FALSE((bool)OptU64); 902 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_high_pc)); 903 EXPECT_FALSE((bool)OptU64); 904 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC, SectionIndex)); 905 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_high_pc)); 906 EXPECT_FALSE((bool)OptU64); 907 OptU64 = toUnsigned(SubprogramDieNoPC.find(DW_AT_high_pc)); 908 EXPECT_FALSE((bool)OptU64); 909 OptU64 = SubprogramDieNoPC.getHighPC(ActualLowPC); 910 EXPECT_FALSE((bool)OptU64); 911 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC, SectionIndex)); 912 913 // Verify the that our subprogram with only a low PC value succeeds when 914 // we ask for the Low PC, but fails appropriately when asked for the high PC 915 // or both low and high PC values. 916 auto SubprogramDieLowPC = SubprogramDieNoPC.getSibling(); 917 EXPECT_TRUE(SubprogramDieLowPC.isValid()); 918 EXPECT_EQ(SubprogramDieLowPC.getTag(), DW_TAG_subprogram); 919 OptU64 = toAddress(SubprogramDieLowPC.find(DW_AT_low_pc)); 920 EXPECT_TRUE((bool)OptU64); 921 EXPECT_EQ(OptU64.getValue(), ActualLowPC); 922 OptU64 = toAddress(SubprogramDieLowPC.find(DW_AT_high_pc)); 923 EXPECT_FALSE((bool)OptU64); 924 OptU64 = toUnsigned(SubprogramDieLowPC.find(DW_AT_high_pc)); 925 EXPECT_FALSE((bool)OptU64); 926 OptU64 = SubprogramDieLowPC.getHighPC(ActualLowPC); 927 EXPECT_FALSE((bool)OptU64); 928 EXPECT_FALSE(SubprogramDieLowPC.getLowAndHighPC(LowPC, HighPC, SectionIndex)); 929 930 // Verify the that our subprogram with only a low PC value succeeds when 931 // we ask for the Low PC, but fails appropriately when asked for the high PC 932 // or both low and high PC values. 933 auto SubprogramDieLowHighPC = SubprogramDieLowPC.getSibling(); 934 EXPECT_TRUE(SubprogramDieLowHighPC.isValid()); 935 EXPECT_EQ(SubprogramDieLowHighPC.getTag(), DW_TAG_subprogram); 936 OptU64 = toAddress(SubprogramDieLowHighPC.find(DW_AT_low_pc)); 937 EXPECT_TRUE((bool)OptU64); 938 EXPECT_EQ(OptU64.getValue(), ActualLowPC); 939 // Get the high PC as an address. This should succeed if the high PC was 940 // encoded as an address and fail if the high PC was encoded as an offset. 941 OptU64 = toAddress(SubprogramDieLowHighPC.find(DW_AT_high_pc)); 942 if (SupportsHighPCAsOffset) { 943 EXPECT_FALSE((bool)OptU64); 944 } else { 945 EXPECT_TRUE((bool)OptU64); 946 EXPECT_EQ(OptU64.getValue(), ActualHighPC); 947 } 948 // Get the high PC as an unsigned constant. This should succeed if the high PC 949 // was encoded as an offset and fail if the high PC was encoded as an address. 950 OptU64 = toUnsigned(SubprogramDieLowHighPC.find(DW_AT_high_pc)); 951 if (SupportsHighPCAsOffset) { 952 EXPECT_TRUE((bool)OptU64); 953 EXPECT_EQ(OptU64.getValue(), ActualHighPCOffset); 954 } else { 955 EXPECT_FALSE((bool)OptU64); 956 } 957 958 OptU64 = SubprogramDieLowHighPC.getHighPC(ActualLowPC); 959 EXPECT_TRUE((bool)OptU64); 960 EXPECT_EQ(OptU64.getValue(), ActualHighPC); 961 962 EXPECT_TRUE(SubprogramDieLowHighPC.getLowAndHighPC(LowPC, HighPC, SectionIndex)); 963 EXPECT_EQ(LowPC, ActualLowPC); 964 EXPECT_EQ(HighPC, ActualHighPC); 965 } 966 967 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Addresses) { 968 // Test that we can decode address values in DWARF32, version 2, with 4 byte 969 // addresses. 970 typedef uint32_t AddrType; 971 TestAddresses<2, AddrType>(); 972 } 973 974 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Addresses) { 975 // Test that we can decode address values in DWARF32, version 2, with 8 byte 976 // addresses. 977 typedef uint64_t AddrType; 978 TestAddresses<2, AddrType>(); 979 } 980 981 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Addresses) { 982 // Test that we can decode address values in DWARF32, version 3, with 4 byte 983 // addresses. 984 typedef uint32_t AddrType; 985 TestAddresses<3, AddrType>(); 986 } 987 988 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Addresses) { 989 // Test that we can decode address values in DWARF32, version 3, with 8 byte 990 // addresses. 991 typedef uint64_t AddrType; 992 TestAddresses<3, AddrType>(); 993 } 994 995 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Addresses) { 996 // Test that we can decode address values in DWARF32, version 4, with 4 byte 997 // addresses. 998 typedef uint32_t AddrType; 999 TestAddresses<4, AddrType>(); 1000 } 1001 1002 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Addresses) { 1003 // Test that we can decode address values in DWARF32, version 4, with 8 byte 1004 // addresses. 1005 typedef uint64_t AddrType; 1006 TestAddresses<4, AddrType>(); 1007 } 1008 1009 TEST(DWARFDebugInfo, TestStringOffsets) { 1010 Triple Triple = getNormalizedDefaultTargetTriple(); 1011 if (!isObjectEmissionSupported(Triple)) 1012 return; 1013 1014 const char *String1 = "Hello"; 1015 const char *String2 = "World"; 1016 1017 auto ExpectedDG = dwarfgen::Generator::create(Triple, 5); 1018 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded()); 1019 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1020 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1021 dwarfgen::DIE CUDie = CU.getUnitDIE(); 1022 1023 CUDie.addStrOffsetsBaseAttribute(); 1024 1025 uint16_t Attr = DW_AT_lo_user; 1026 1027 // Create our strings. First we create a non-indexed reference to String1, 1028 // followed by an indexed String2. Finally, we add an indexed reference to 1029 // String1. 1030 const auto Attr1 = static_cast<dwarf::Attribute>(Attr++); 1031 CUDie.addAttribute(Attr1, DW_FORM_strp, String1); 1032 1033 const auto Attr2 = static_cast<dwarf::Attribute>(Attr++); 1034 CUDie.addAttribute(Attr2, DW_FORM_strx, String2); 1035 1036 const auto Attr3 = static_cast<dwarf::Attribute>(Attr++); 1037 CUDie.addAttribute(Attr3, DW_FORM_strx, String1); 1038 1039 // Generate the DWARF 1040 StringRef FileBytes = DG->generate(); 1041 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 1042 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1043 ASSERT_TRUE((bool)Obj); 1044 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj); 1045 uint32_t NumCUs = DwarfContext->getNumCompileUnits(); 1046 ASSERT_EQ(NumCUs, 1u); 1047 DWARFUnit *U = DwarfContext->getUnitAtIndex(0); 1048 auto DieDG = U->getUnitDIE(false); 1049 ASSERT_TRUE(DieDG.isValid()); 1050 1051 // Now make sure the string offsets came out properly. Attr2 should have index 1052 // 0 (because it was the first indexed string) even though the string itself 1053 // was added eariler. 1054 auto Extracted1 = toString(DieDG.find(Attr1)); 1055 ASSERT_TRUE((bool)Extracted1); 1056 EXPECT_STREQ(String1, *Extracted1); 1057 1058 Optional<DWARFFormValue> Form2 = DieDG.find(Attr2); 1059 ASSERT_TRUE((bool)Form2); 1060 EXPECT_EQ(0u, Form2->getRawUValue()); 1061 auto Extracted2 = toString(Form2); 1062 ASSERT_TRUE((bool)Extracted2); 1063 EXPECT_STREQ(String2, *Extracted2); 1064 1065 Optional<DWARFFormValue> Form3 = DieDG.find(Attr3); 1066 ASSERT_TRUE((bool)Form3); 1067 EXPECT_EQ(1u, Form3->getRawUValue()); 1068 auto Extracted3 = toString(Form3); 1069 ASSERT_TRUE((bool)Extracted3); 1070 EXPECT_STREQ(String1, *Extracted3); 1071 } 1072 1073 TEST(DWARFDebugInfo, TestEmptyStringOffsets) { 1074 Triple Triple = getNormalizedDefaultTargetTriple(); 1075 if (!isObjectEmissionSupported(Triple)) 1076 return; 1077 1078 const char *String1 = "Hello"; 1079 1080 auto ExpectedDG = dwarfgen::Generator::create(Triple, 5); 1081 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded()); 1082 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1083 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1084 dwarfgen::DIE CUDie = CU.getUnitDIE(); 1085 1086 uint16_t Attr = DW_AT_lo_user; 1087 1088 // We shall insert only one string. It will be referenced directly. 1089 const auto Attr1 = static_cast<dwarf::Attribute>(Attr++); 1090 CUDie.addAttribute(Attr1, DW_FORM_strp, String1); 1091 1092 // Generate the DWARF 1093 StringRef FileBytes = DG->generate(); 1094 MemoryBufferRef FileBuffer(FileBytes, "dwarf"); 1095 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1096 ASSERT_TRUE((bool)Obj); 1097 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj); 1098 EXPECT_TRUE( 1099 DwarfContext->getDWARFObj().getStrOffsetsSection().Data.empty()); 1100 } 1101 1102 TEST(DWARFDebugInfo, TestRelations) { 1103 Triple Triple = getNormalizedDefaultTargetTriple(); 1104 if (!isObjectEmissionSupported(Triple)) 1105 return; 1106 1107 // Test the DWARF APIs related to accessing the DW_AT_low_pc and 1108 // DW_AT_high_pc. 1109 uint16_t Version = 4; 1110 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1111 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded()); 1112 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1113 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1114 1115 enum class Tag: uint16_t { 1116 A = dwarf::DW_TAG_lo_user, 1117 B, 1118 C, 1119 C1, 1120 C2, 1121 D, 1122 D1 1123 }; 1124 1125 // Scope to allow us to re-use the same DIE names 1126 { 1127 // Create DWARF tree that looks like: 1128 // 1129 // CU 1130 // A 1131 // B 1132 // C 1133 // C1 1134 // C2 1135 // D 1136 // D1 1137 dwarfgen::DIE CUDie = CU.getUnitDIE(); 1138 dwarfgen::DIE A = CUDie.addChild((dwarf::Tag)Tag::A); 1139 A.addChild((dwarf::Tag)Tag::B); 1140 dwarfgen::DIE C = A.addChild((dwarf::Tag)Tag::C); 1141 dwarfgen::DIE D = A.addChild((dwarf::Tag)Tag::D); 1142 C.addChild((dwarf::Tag)Tag::C1); 1143 C.addChild((dwarf::Tag)Tag::C2); 1144 D.addChild((dwarf::Tag)Tag::D1); 1145 } 1146 1147 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1148 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1149 EXPECT_TRUE((bool)Obj); 1150 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj); 1151 1152 // Verify the number of compile units is correct. 1153 uint32_t NumCUs = DwarfContext->getNumCompileUnits(); 1154 EXPECT_EQ(NumCUs, 1u); 1155 DWARFCompileUnit *U = 1156 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0)); 1157 1158 // Get the compile unit DIE is valid. 1159 auto CUDie = U->getUnitDIE(false); 1160 EXPECT_TRUE(CUDie.isValid()); 1161 1162 // The compile unit doesn't have a parent or a sibling. 1163 auto ParentDie = CUDie.getParent(); 1164 EXPECT_FALSE(ParentDie.isValid()); 1165 auto SiblingDie = CUDie.getSibling(); 1166 EXPECT_FALSE(SiblingDie.isValid()); 1167 1168 // Get the children of the compile unit 1169 auto A = CUDie.getFirstChild(); 1170 auto B = A.getFirstChild(); 1171 auto C = B.getSibling(); 1172 auto D = C.getSibling(); 1173 auto Null = D.getSibling(); 1174 1175 // Verify NULL Die is NULL and has no children or siblings 1176 EXPECT_TRUE(Null.isNULL()); 1177 EXPECT_FALSE(Null.getSibling().isValid()); 1178 EXPECT_FALSE(Null.getFirstChild().isValid()); 1179 1180 // Verify all children of the compile unit DIE are correct. 1181 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A); 1182 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B); 1183 EXPECT_EQ(C.getTag(), (dwarf::Tag)Tag::C); 1184 EXPECT_EQ(D.getTag(), (dwarf::Tag)Tag::D); 1185 1186 // Verify who has children 1187 EXPECT_TRUE(A.hasChildren()); 1188 EXPECT_FALSE(B.hasChildren()); 1189 EXPECT_TRUE(C.hasChildren()); 1190 EXPECT_TRUE(D.hasChildren()); 1191 1192 // Make sure the parent of all the children of the compile unit are the 1193 // compile unit. 1194 EXPECT_EQ(A.getParent(), CUDie); 1195 1196 // Make sure the parent of all the children of A are the A. 1197 // B is the first child in A, so we need to verify we can get the previous 1198 // DIE as the parent. 1199 EXPECT_EQ(B.getParent(), A); 1200 // C is the second child in A, so we need to make sure we can backup across 1201 // other DIE (B) at the same level to get the correct parent. 1202 EXPECT_EQ(C.getParent(), A); 1203 // D is the third child of A. We need to verify we can backup across other DIE 1204 // (B and C) including DIE that have children (D) to get the correct parent. 1205 EXPECT_EQ(D.getParent(), A); 1206 1207 // Verify that a DIE with no children returns an invalid DWARFDie. 1208 EXPECT_FALSE(B.getFirstChild().isValid()); 1209 1210 // Verify the children of the B DIE 1211 auto C1 = C.getFirstChild(); 1212 auto C2 = C1.getSibling(); 1213 EXPECT_TRUE(C2.getSibling().isNULL()); 1214 1215 // Verify all children of the B DIE correctly valid or invalid. 1216 EXPECT_EQ(C1.getTag(), (dwarf::Tag)Tag::C1); 1217 EXPECT_EQ(C2.getTag(), (dwarf::Tag)Tag::C2); 1218 1219 // Make sure the parent of all the children of the B are the B. 1220 EXPECT_EQ(C1.getParent(), C); 1221 EXPECT_EQ(C2.getParent(), C); 1222 1223 // Make sure iterators work as expected. 1224 EXPECT_THAT(std::vector<DWARFDie>(A.begin(), A.end()), 1225 testing::ElementsAre(B, C, D)); 1226 EXPECT_THAT(std::vector<DWARFDie>(A.rbegin(), A.rend()), 1227 testing::ElementsAre(D, C, B)); 1228 1229 // Make sure conversion from reverse iterator works as expected. 1230 EXPECT_EQ(A.rbegin().base(), A.end()); 1231 EXPECT_EQ(A.rend().base(), A.begin()); 1232 1233 // Make sure iterator is bidirectional. 1234 { 1235 auto Begin = A.begin(); 1236 auto End = A.end(); 1237 auto It = A.begin(); 1238 1239 EXPECT_EQ(It, Begin); 1240 EXPECT_EQ(*It, B); 1241 ++It; 1242 EXPECT_EQ(*It, C); 1243 ++It; 1244 EXPECT_EQ(*It, D); 1245 ++It; 1246 EXPECT_EQ(It, End); 1247 --It; 1248 EXPECT_EQ(*It, D); 1249 --It; 1250 EXPECT_EQ(*It, C); 1251 --It; 1252 EXPECT_EQ(*It, B); 1253 EXPECT_EQ(It, Begin); 1254 } 1255 1256 // Make sure reverse iterator is bidirectional. 1257 { 1258 auto Begin = A.rbegin(); 1259 auto End = A.rend(); 1260 auto It = A.rbegin(); 1261 1262 EXPECT_EQ(It, Begin); 1263 EXPECT_EQ(*It, D); 1264 ++It; 1265 EXPECT_EQ(*It, C); 1266 ++It; 1267 EXPECT_EQ(*It, B); 1268 ++It; 1269 EXPECT_EQ(It, End); 1270 --It; 1271 EXPECT_EQ(*It, B); 1272 --It; 1273 EXPECT_EQ(*It, C); 1274 --It; 1275 EXPECT_EQ(*It, D); 1276 EXPECT_EQ(It, Begin); 1277 } 1278 } 1279 1280 TEST(DWARFDebugInfo, TestDWARFDie) { 1281 // Make sure a default constructed DWARFDie doesn't have any parent, sibling 1282 // or child; 1283 DWARFDie DefaultDie; 1284 EXPECT_FALSE(DefaultDie.getParent().isValid()); 1285 EXPECT_FALSE(DefaultDie.getFirstChild().isValid()); 1286 EXPECT_FALSE(DefaultDie.getSibling().isValid()); 1287 } 1288 1289 TEST(DWARFDebugInfo, TestChildIterators) { 1290 Triple Triple = getNormalizedDefaultTargetTriple(); 1291 if (!isObjectEmissionSupported(Triple)) 1292 return; 1293 1294 // Test the DWARF APIs related to iterating across the children of a DIE using 1295 // the DWARFDie::iterator class. 1296 uint16_t Version = 4; 1297 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1298 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded()); 1299 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1300 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1301 1302 enum class Tag: uint16_t { 1303 A = dwarf::DW_TAG_lo_user, 1304 B, 1305 }; 1306 1307 // Scope to allow us to re-use the same DIE names 1308 { 1309 // Create DWARF tree that looks like: 1310 // 1311 // CU 1312 // A 1313 // B 1314 auto CUDie = CU.getUnitDIE(); 1315 CUDie.addChild((dwarf::Tag)Tag::A); 1316 CUDie.addChild((dwarf::Tag)Tag::B); 1317 } 1318 1319 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1320 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1321 EXPECT_TRUE((bool)Obj); 1322 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj); 1323 1324 // Verify the number of compile units is correct. 1325 uint32_t NumCUs = DwarfContext->getNumCompileUnits(); 1326 EXPECT_EQ(NumCUs, 1u); 1327 DWARFCompileUnit *U = 1328 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0)); 1329 1330 // Get the compile unit DIE is valid. 1331 auto CUDie = U->getUnitDIE(false); 1332 EXPECT_TRUE(CUDie.isValid()); 1333 uint32_t Index; 1334 DWARFDie A; 1335 DWARFDie B; 1336 1337 // Verify the compile unit DIE's children. 1338 Index = 0; 1339 for (auto Die : CUDie.children()) { 1340 switch (Index++) { 1341 case 0: A = Die; break; 1342 case 1: B = Die; break; 1343 } 1344 } 1345 1346 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A); 1347 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B); 1348 1349 // Verify that A has no children by verifying that the begin and end contain 1350 // invalid DIEs and also that the iterators are equal. 1351 EXPECT_EQ(A.begin(), A.end()); 1352 } 1353 1354 TEST(DWARFDebugInfo, TestChildIteratorsOnInvalidDie) { 1355 // Verify that an invalid DIE has no children. 1356 DWARFDie Invalid; 1357 auto begin = Invalid.begin(); 1358 auto end = Invalid.end(); 1359 EXPECT_FALSE(begin->isValid()); 1360 EXPECT_FALSE(end->isValid()); 1361 EXPECT_EQ(begin, end); 1362 } 1363 1364 TEST(DWARFDebugInfo, TestEmptyChildren) { 1365 const char *yamldata = "debug_abbrev:\n" 1366 " - Table:\n" 1367 " - Code: 0x00000001\n" 1368 " Tag: DW_TAG_compile_unit\n" 1369 " Children: DW_CHILDREN_yes\n" 1370 "debug_info:\n" 1371 " - Version: 4\n" 1372 " AddrSize: 8\n" 1373 " Entries:\n" 1374 " - AbbrCode: 0x00000001\n" 1375 " - AbbrCode: 0x00000000\n"; 1376 1377 auto ErrOrSections = DWARFYAML::emitDebugSections(StringRef(yamldata)); 1378 ASSERT_TRUE((bool)ErrOrSections); 1379 std::unique_ptr<DWARFContext> DwarfContext = 1380 DWARFContext::create(*ErrOrSections, 8); 1381 1382 // Verify the number of compile units is correct. 1383 uint32_t NumCUs = DwarfContext->getNumCompileUnits(); 1384 EXPECT_EQ(NumCUs, 1u); 1385 DWARFCompileUnit *U = 1386 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0)); 1387 1388 // Get the compile unit DIE is valid. 1389 auto CUDie = U->getUnitDIE(false); 1390 EXPECT_TRUE(CUDie.isValid()); 1391 1392 // Verify that the CU Die that says it has children, but doesn't, actually 1393 // has begin and end iterators that are equal. We want to make sure we don't 1394 // see the Null DIEs during iteration. 1395 EXPECT_EQ(CUDie.begin(), CUDie.end()); 1396 } 1397 1398 TEST(DWARFDebugInfo, TestAttributeIterators) { 1399 Triple Triple = getNormalizedDefaultTargetTriple(); 1400 if (!isObjectEmissionSupported(Triple)) 1401 return; 1402 1403 // Test the DWARF APIs related to iterating across all attribute values in a 1404 // a DWARFDie. 1405 uint16_t Version = 4; 1406 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1407 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded()); 1408 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1409 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1410 const uint64_t CULowPC = 0x1000; 1411 StringRef CUPath("/tmp/main.c"); 1412 1413 // Scope to allow us to re-use the same DIE names 1414 { 1415 auto CUDie = CU.getUnitDIE(); 1416 // Encode an attribute value before an attribute with no data. 1417 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, CUPath.data()); 1418 // Encode an attribute value with no data in .debug_info/types to ensure 1419 // the iteration works correctly. 1420 CUDie.addAttribute(DW_AT_declaration, DW_FORM_flag_present); 1421 // Encode an attribute value after an attribute with no data. 1422 CUDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, CULowPC); 1423 } 1424 1425 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1426 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1427 EXPECT_TRUE((bool)Obj); 1428 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj); 1429 1430 // Verify the number of compile units is correct. 1431 uint32_t NumCUs = DwarfContext->getNumCompileUnits(); 1432 EXPECT_EQ(NumCUs, 1u); 1433 DWARFCompileUnit *U = 1434 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0)); 1435 1436 // Get the compile unit DIE is valid. 1437 auto CUDie = U->getUnitDIE(false); 1438 EXPECT_TRUE(CUDie.isValid()); 1439 1440 auto R = CUDie.attributes(); 1441 auto I = R.begin(); 1442 auto E = R.end(); 1443 1444 ASSERT_NE(E, I); 1445 EXPECT_EQ(I->Attr, DW_AT_name); 1446 auto ActualCUPath = I->Value.getAsCString(); 1447 EXPECT_EQ(CUPath, *ActualCUPath); 1448 1449 ASSERT_NE(E, ++I); 1450 EXPECT_EQ(I->Attr, DW_AT_declaration); 1451 EXPECT_EQ(1ull, *I->Value.getAsUnsignedConstant()); 1452 1453 ASSERT_NE(E, ++I); 1454 EXPECT_EQ(I->Attr, DW_AT_low_pc); 1455 EXPECT_EQ(CULowPC, *I->Value.getAsAddress()); 1456 1457 EXPECT_EQ(E, ++I); 1458 } 1459 1460 TEST(DWARFDebugInfo, TestFindRecurse) { 1461 Triple Triple = getNormalizedDefaultTargetTriple(); 1462 if (!isObjectEmissionSupported(Triple)) 1463 return; 1464 1465 uint16_t Version = 4; 1466 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1467 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded()); 1468 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1469 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1470 1471 StringRef SpecDieName = "spec"; 1472 StringRef SpecLinkageName = "spec_linkage"; 1473 StringRef AbsDieName = "abs"; 1474 // Scope to allow us to re-use the same DIE names 1475 { 1476 auto CUDie = CU.getUnitDIE(); 1477 auto FuncSpecDie = CUDie.addChild(DW_TAG_subprogram); 1478 auto FuncAbsDie = CUDie.addChild(DW_TAG_subprogram); 1479 // Put the linkage name in a second abstract origin DIE to ensure we 1480 // recurse through more than just one DIE when looking for attributes. 1481 auto FuncAbsDie2 = CUDie.addChild(DW_TAG_subprogram); 1482 auto FuncDie = CUDie.addChild(DW_TAG_subprogram); 1483 auto VarAbsDie = CUDie.addChild(DW_TAG_variable); 1484 auto VarDie = CUDie.addChild(DW_TAG_variable); 1485 FuncSpecDie.addAttribute(DW_AT_name, DW_FORM_strp, SpecDieName); 1486 FuncAbsDie2.addAttribute(DW_AT_linkage_name, DW_FORM_strp, SpecLinkageName); 1487 FuncAbsDie.addAttribute(DW_AT_specification, DW_FORM_ref4, FuncSpecDie); 1488 FuncAbsDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, FuncAbsDie2); 1489 FuncDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, FuncAbsDie); 1490 VarAbsDie.addAttribute(DW_AT_name, DW_FORM_strp, AbsDieName); 1491 VarDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, VarAbsDie); 1492 } 1493 1494 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1495 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1496 EXPECT_TRUE((bool)Obj); 1497 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj); 1498 1499 // Verify the number of compile units is correct. 1500 uint32_t NumCUs = DwarfContext->getNumCompileUnits(); 1501 EXPECT_EQ(NumCUs, 1u); 1502 DWARFCompileUnit *U = 1503 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0)); 1504 1505 // Get the compile unit DIE is valid. 1506 auto CUDie = U->getUnitDIE(false); 1507 EXPECT_TRUE(CUDie.isValid()); 1508 1509 auto FuncSpecDie = CUDie.getFirstChild(); 1510 auto FuncAbsDie = FuncSpecDie.getSibling(); 1511 auto FuncAbsDie2 = FuncAbsDie.getSibling(); 1512 auto FuncDie = FuncAbsDie2.getSibling(); 1513 auto VarAbsDie = FuncDie.getSibling(); 1514 auto VarDie = VarAbsDie.getSibling(); 1515 1516 // Make sure we can't extract the name from the specification die when using 1517 // DWARFDie::find() since it won't check the DW_AT_specification DIE. 1518 EXPECT_FALSE(FuncDie.find(DW_AT_name)); 1519 1520 // Make sure we can extract the name from the specification die when using 1521 // DWARFDie::findRecursively() since it should recurse through the 1522 // DW_AT_specification DIE. 1523 auto NameOpt = FuncDie.findRecursively(DW_AT_name); 1524 EXPECT_TRUE(NameOpt); 1525 // Test the dwarf::toString() helper function. 1526 auto StringOpt = toString(NameOpt); 1527 EXPECT_TRUE(StringOpt); 1528 EXPECT_EQ(SpecDieName, StringOpt.getValueOr(nullptr)); 1529 // Test the dwarf::toString() helper function with a default value specified. 1530 EXPECT_EQ(SpecDieName, toString(NameOpt, nullptr)); 1531 1532 auto LinkageNameOpt = FuncDie.findRecursively(DW_AT_linkage_name); 1533 EXPECT_EQ(SpecLinkageName, toString(LinkageNameOpt).getValueOr(nullptr)); 1534 1535 // Make sure we can't extract the name from the abstract origin die when using 1536 // DWARFDie::find() since it won't check the DW_AT_abstract_origin DIE. 1537 EXPECT_FALSE(VarDie.find(DW_AT_name)); 1538 1539 // Make sure we can extract the name from the abstract origin die when using 1540 // DWARFDie::findRecursively() since it should recurse through the 1541 // DW_AT_abstract_origin DIE. 1542 NameOpt = VarDie.findRecursively(DW_AT_name); 1543 EXPECT_TRUE(NameOpt); 1544 // Test the dwarf::toString() helper function. 1545 StringOpt = toString(NameOpt); 1546 EXPECT_TRUE(StringOpt); 1547 EXPECT_EQ(AbsDieName, StringOpt.getValueOr(nullptr)); 1548 } 1549 1550 TEST(DWARFDebugInfo, TestDwarfToFunctions) { 1551 // Test all of the dwarf::toXXX functions that take a 1552 // Optional<DWARFFormValue> and extract the values from it. 1553 uint64_t InvalidU64 = 0xBADBADBADBADBADB; 1554 int64_t InvalidS64 = 0xBADBADBADBADBADB; 1555 1556 // First test that we don't get valid values back when using an optional with 1557 // no value. 1558 Optional<DWARFFormValue> FormValOpt1 = DWARFFormValue(); 1559 EXPECT_FALSE(toString(FormValOpt1).hasValue()); 1560 EXPECT_FALSE(toUnsigned(FormValOpt1).hasValue()); 1561 EXPECT_FALSE(toReference(FormValOpt1).hasValue()); 1562 EXPECT_FALSE(toSigned(FormValOpt1).hasValue()); 1563 EXPECT_FALSE(toAddress(FormValOpt1).hasValue()); 1564 EXPECT_FALSE(toSectionOffset(FormValOpt1).hasValue()); 1565 EXPECT_FALSE(toBlock(FormValOpt1).hasValue()); 1566 EXPECT_EQ(nullptr, toString(FormValOpt1, nullptr)); 1567 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt1, InvalidU64)); 1568 EXPECT_EQ(InvalidU64, toReference(FormValOpt1, InvalidU64)); 1569 EXPECT_EQ(InvalidU64, toAddress(FormValOpt1, InvalidU64)); 1570 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt1, InvalidU64)); 1571 EXPECT_EQ(InvalidS64, toSigned(FormValOpt1, InvalidS64)); 1572 1573 // Test successful and unsuccessful address decoding. 1574 uint64_t Address = 0x100000000ULL; 1575 Optional<DWARFFormValue> FormValOpt2 = 1576 DWARFFormValue::createFromUValue(DW_FORM_addr, Address); 1577 1578 EXPECT_FALSE(toString(FormValOpt2).hasValue()); 1579 EXPECT_FALSE(toUnsigned(FormValOpt2).hasValue()); 1580 EXPECT_FALSE(toReference(FormValOpt2).hasValue()); 1581 EXPECT_FALSE(toSigned(FormValOpt2).hasValue()); 1582 EXPECT_TRUE(toAddress(FormValOpt2).hasValue()); 1583 EXPECT_FALSE(toSectionOffset(FormValOpt2).hasValue()); 1584 EXPECT_FALSE(toBlock(FormValOpt2).hasValue()); 1585 EXPECT_EQ(nullptr, toString(FormValOpt2, nullptr)); 1586 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt2, InvalidU64)); 1587 EXPECT_EQ(InvalidU64, toReference(FormValOpt2, InvalidU64)); 1588 EXPECT_EQ(Address, toAddress(FormValOpt2, InvalidU64)); 1589 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt2, InvalidU64)); 1590 EXPECT_EQ(InvalidS64, toSigned(FormValOpt2, InvalidU64)); 1591 1592 // Test successful and unsuccessful unsigned constant decoding. 1593 uint64_t UData8 = 0x1020304050607080ULL; 1594 Optional<DWARFFormValue> FormValOpt3 = 1595 DWARFFormValue::createFromUValue(DW_FORM_udata, UData8); 1596 1597 EXPECT_FALSE(toString(FormValOpt3).hasValue()); 1598 EXPECT_TRUE(toUnsigned(FormValOpt3).hasValue()); 1599 EXPECT_FALSE(toReference(FormValOpt3).hasValue()); 1600 EXPECT_TRUE(toSigned(FormValOpt3).hasValue()); 1601 EXPECT_FALSE(toAddress(FormValOpt3).hasValue()); 1602 EXPECT_FALSE(toSectionOffset(FormValOpt3).hasValue()); 1603 EXPECT_FALSE(toBlock(FormValOpt3).hasValue()); 1604 EXPECT_EQ(nullptr, toString(FormValOpt3, nullptr)); 1605 EXPECT_EQ(UData8, toUnsigned(FormValOpt3, InvalidU64)); 1606 EXPECT_EQ(InvalidU64, toReference(FormValOpt3, InvalidU64)); 1607 EXPECT_EQ(InvalidU64, toAddress(FormValOpt3, InvalidU64)); 1608 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt3, InvalidU64)); 1609 EXPECT_EQ((int64_t)UData8, toSigned(FormValOpt3, InvalidU64)); 1610 1611 // Test successful and unsuccessful reference decoding. 1612 uint32_t RefData = 0x11223344U; 1613 Optional<DWARFFormValue> FormValOpt4 = 1614 DWARFFormValue::createFromUValue(DW_FORM_ref_addr, RefData); 1615 1616 EXPECT_FALSE(toString(FormValOpt4).hasValue()); 1617 EXPECT_FALSE(toUnsigned(FormValOpt4).hasValue()); 1618 EXPECT_TRUE(toReference(FormValOpt4).hasValue()); 1619 EXPECT_FALSE(toSigned(FormValOpt4).hasValue()); 1620 EXPECT_FALSE(toAddress(FormValOpt4).hasValue()); 1621 EXPECT_FALSE(toSectionOffset(FormValOpt4).hasValue()); 1622 EXPECT_FALSE(toBlock(FormValOpt4).hasValue()); 1623 EXPECT_EQ(nullptr, toString(FormValOpt4, nullptr)); 1624 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt4, InvalidU64)); 1625 EXPECT_EQ(RefData, toReference(FormValOpt4, InvalidU64)); 1626 EXPECT_EQ(InvalidU64, toAddress(FormValOpt4, InvalidU64)); 1627 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt4, InvalidU64)); 1628 EXPECT_EQ(InvalidS64, toSigned(FormValOpt4, InvalidU64)); 1629 1630 // Test successful and unsuccessful signed constant decoding. 1631 int64_t SData8 = 0x1020304050607080ULL; 1632 Optional<DWARFFormValue> FormValOpt5 = 1633 DWARFFormValue::createFromSValue(DW_FORM_udata, SData8); 1634 1635 EXPECT_FALSE(toString(FormValOpt5).hasValue()); 1636 EXPECT_TRUE(toUnsigned(FormValOpt5).hasValue()); 1637 EXPECT_FALSE(toReference(FormValOpt5).hasValue()); 1638 EXPECT_TRUE(toSigned(FormValOpt5).hasValue()); 1639 EXPECT_FALSE(toAddress(FormValOpt5).hasValue()); 1640 EXPECT_FALSE(toSectionOffset(FormValOpt5).hasValue()); 1641 EXPECT_FALSE(toBlock(FormValOpt5).hasValue()); 1642 EXPECT_EQ(nullptr, toString(FormValOpt5, nullptr)); 1643 EXPECT_EQ((uint64_t)SData8, toUnsigned(FormValOpt5, InvalidU64)); 1644 EXPECT_EQ(InvalidU64, toReference(FormValOpt5, InvalidU64)); 1645 EXPECT_EQ(InvalidU64, toAddress(FormValOpt5, InvalidU64)); 1646 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt5, InvalidU64)); 1647 EXPECT_EQ(SData8, toSigned(FormValOpt5, InvalidU64)); 1648 1649 // Test successful and unsuccessful block decoding. 1650 uint8_t Data[] = { 2, 3, 4 }; 1651 ArrayRef<uint8_t> Array(Data); 1652 Optional<DWARFFormValue> FormValOpt6 = 1653 DWARFFormValue::createFromBlockValue(DW_FORM_block1, Array); 1654 1655 EXPECT_FALSE(toString(FormValOpt6).hasValue()); 1656 EXPECT_FALSE(toUnsigned(FormValOpt6).hasValue()); 1657 EXPECT_FALSE(toReference(FormValOpt6).hasValue()); 1658 EXPECT_FALSE(toSigned(FormValOpt6).hasValue()); 1659 EXPECT_FALSE(toAddress(FormValOpt6).hasValue()); 1660 EXPECT_FALSE(toSectionOffset(FormValOpt6).hasValue()); 1661 auto BlockOpt = toBlock(FormValOpt6); 1662 EXPECT_TRUE(BlockOpt.hasValue()); 1663 EXPECT_EQ(*BlockOpt, Array); 1664 EXPECT_EQ(nullptr, toString(FormValOpt6, nullptr)); 1665 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt6, InvalidU64)); 1666 EXPECT_EQ(InvalidU64, toReference(FormValOpt6, InvalidU64)); 1667 EXPECT_EQ(InvalidU64, toAddress(FormValOpt6, InvalidU64)); 1668 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt6, InvalidU64)); 1669 EXPECT_EQ(InvalidS64, toSigned(FormValOpt6, InvalidU64)); 1670 1671 // Test 1672 } 1673 1674 TEST(DWARFDebugInfo, TestFindAttrs) { 1675 Triple Triple = getNormalizedDefaultTargetTriple(); 1676 if (!isObjectEmissionSupported(Triple)) 1677 return; 1678 1679 // Test the DWARFDie::find() and DWARFDie::findRecursively() that take an 1680 // ArrayRef<dwarf::Attribute> value to make sure they work correctly. 1681 uint16_t Version = 4; 1682 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1683 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded()); 1684 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1685 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1686 1687 StringRef DieMangled("_Z3fooi"); 1688 // Scope to allow us to re-use the same DIE names 1689 { 1690 auto CUDie = CU.getUnitDIE(); 1691 auto FuncSpecDie = CUDie.addChild(DW_TAG_subprogram); 1692 auto FuncDie = CUDie.addChild(DW_TAG_subprogram); 1693 FuncSpecDie.addAttribute(DW_AT_MIPS_linkage_name, DW_FORM_strp, DieMangled); 1694 FuncDie.addAttribute(DW_AT_specification, DW_FORM_ref4, FuncSpecDie); 1695 } 1696 1697 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1698 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1699 EXPECT_TRUE((bool)Obj); 1700 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj); 1701 1702 // Verify the number of compile units is correct. 1703 uint32_t NumCUs = DwarfContext->getNumCompileUnits(); 1704 EXPECT_EQ(NumCUs, 1u); 1705 DWARFCompileUnit *U = 1706 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0)); 1707 1708 // Get the compile unit DIE is valid. 1709 auto CUDie = U->getUnitDIE(false); 1710 EXPECT_TRUE(CUDie.isValid()); 1711 1712 auto FuncSpecDie = CUDie.getFirstChild(); 1713 auto FuncDie = FuncSpecDie.getSibling(); 1714 1715 // Make sure that passing in an empty attribute list behave correctly. 1716 EXPECT_FALSE(FuncDie.find(ArrayRef<dwarf::Attribute>()).hasValue()); 1717 1718 // Make sure that passing in a list of attribute that are not contained 1719 // in the DIE returns nothing. 1720 EXPECT_FALSE(FuncDie.find({DW_AT_low_pc, DW_AT_entry_pc}).hasValue()); 1721 1722 const dwarf::Attribute Attrs[] = {DW_AT_linkage_name, 1723 DW_AT_MIPS_linkage_name}; 1724 1725 // Make sure we can't extract the linkage name attributes when using 1726 // DWARFDie::find() since it won't check the DW_AT_specification DIE. 1727 EXPECT_FALSE(FuncDie.find(Attrs).hasValue()); 1728 1729 // Make sure we can extract the name from the specification die when using 1730 // DWARFDie::findRecursively() since it should recurse through the 1731 // DW_AT_specification DIE. 1732 auto NameOpt = FuncDie.findRecursively(Attrs); 1733 EXPECT_TRUE(NameOpt.hasValue()); 1734 EXPECT_EQ(DieMangled, toString(NameOpt, "")); 1735 } 1736 1737 TEST(DWARFDebugInfo, TestImplicitConstAbbrevs) { 1738 Triple Triple = getNormalizedDefaultTargetTriple(); 1739 if (!isObjectEmissionSupported(Triple)) 1740 return; 1741 1742 uint16_t Version = 5; 1743 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version); 1744 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded()); 1745 dwarfgen::Generator *DG = ExpectedDG.get().get(); 1746 dwarfgen::CompileUnit &CU = DG->addCompileUnit(); 1747 dwarfgen::DIE CUDie = CU.getUnitDIE(); 1748 const dwarf::Attribute Attr = DW_AT_lo_user; 1749 const int64_t Val1 = 42; 1750 const int64_t Val2 = 43; 1751 1752 auto FirstVal1DIE = CUDie.addChild(DW_TAG_class_type); 1753 FirstVal1DIE.addAttribute(Attr, DW_FORM_implicit_const, Val1); 1754 1755 auto SecondVal1DIE = CUDie.addChild(DW_TAG_class_type); 1756 SecondVal1DIE.addAttribute(Attr, DW_FORM_implicit_const, Val1); 1757 1758 auto Val2DIE = CUDie.addChild(DW_TAG_class_type); 1759 Val2DIE.addAttribute(Attr, DW_FORM_implicit_const, Val2); 1760 1761 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 1762 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 1763 EXPECT_TRUE((bool)Obj); 1764 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj); 1765 DWARFCompileUnit *U = 1766 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0)); 1767 EXPECT_TRUE((bool)U); 1768 1769 const auto *Abbrevs = U->getAbbreviations(); 1770 EXPECT_TRUE((bool)Abbrevs); 1771 1772 // Let's find implicit_const abbrevs and verify, 1773 // that there are exactly two of them and both of them 1774 // can be dumped correctly. 1775 typedef decltype(Abbrevs->begin()) AbbrevIt; 1776 AbbrevIt Val1Abbrev = Abbrevs->end(); 1777 AbbrevIt Val2Abbrev = Abbrevs->end(); 1778 for(auto it = Abbrevs->begin(); it != Abbrevs->end(); ++it) { 1779 if (it->getNumAttributes() == 0) 1780 continue; // root abbrev for DW_TAG_compile_unit 1781 1782 auto A = it->getAttrByIndex(0); 1783 EXPECT_EQ(A, Attr); 1784 1785 auto FormValue = it->getAttributeValue(/* offset */ 0, A, *U); 1786 EXPECT_TRUE((bool)FormValue); 1787 EXPECT_EQ(FormValue->getForm(), dwarf::DW_FORM_implicit_const); 1788 1789 const auto V = FormValue->getAsSignedConstant(); 1790 EXPECT_TRUE((bool)V); 1791 1792 auto VerifyAbbrevDump = [&V](AbbrevIt it) { 1793 std::string S; 1794 llvm::raw_string_ostream OS(S); 1795 it->dump(OS); 1796 auto FormPos = OS.str().find("DW_FORM_implicit_const"); 1797 EXPECT_NE(FormPos, std::string::npos); 1798 auto ValPos = S.find_first_of("-0123456789", FormPos); 1799 EXPECT_NE(ValPos, std::string::npos); 1800 int64_t Val = std::atoll(S.substr(ValPos).c_str()); 1801 EXPECT_EQ(Val, *V); 1802 }; 1803 1804 switch(*V) { 1805 case Val1: 1806 EXPECT_EQ(Val1Abbrev, Abbrevs->end()); 1807 Val1Abbrev = it; 1808 VerifyAbbrevDump(it); 1809 break; 1810 case Val2: 1811 EXPECT_EQ(Val2Abbrev, Abbrevs->end()); 1812 Val2Abbrev = it; 1813 VerifyAbbrevDump(it); 1814 break; 1815 default: 1816 FAIL() << "Unexpected attribute value: " << *V; 1817 } 1818 } 1819 1820 // Now let's make sure that two Val1-DIEs refer to the same abbrev, 1821 // and Val2-DIE refers to another one. 1822 auto DieDG = U->getUnitDIE(false); 1823 auto it = DieDG.begin(); 1824 std::multimap<int64_t, decltype(it->getAbbreviationDeclarationPtr())> DIEs; 1825 const DWARFAbbreviationDeclaration *AbbrevPtrVal1 = nullptr; 1826 const DWARFAbbreviationDeclaration *AbbrevPtrVal2 = nullptr; 1827 for (; it != DieDG.end(); ++it) { 1828 const auto *AbbrevPtr = it->getAbbreviationDeclarationPtr(); 1829 EXPECT_TRUE((bool)AbbrevPtr); 1830 auto FormValue = it->find(Attr); 1831 EXPECT_TRUE((bool)FormValue); 1832 const auto V = FormValue->getAsSignedConstant(); 1833 EXPECT_TRUE((bool)V); 1834 switch(*V) { 1835 case Val1: 1836 AbbrevPtrVal1 = AbbrevPtr; 1837 break; 1838 case Val2: 1839 AbbrevPtrVal2 = AbbrevPtr; 1840 break; 1841 default: 1842 FAIL() << "Unexpected attribute value: " << *V; 1843 } 1844 DIEs.insert(std::make_pair(*V, AbbrevPtr)); 1845 } 1846 EXPECT_EQ(DIEs.count(Val1), 2u); 1847 EXPECT_EQ(DIEs.count(Val2), 1u); 1848 auto Val1Range = DIEs.equal_range(Val1); 1849 for (auto it = Val1Range.first; it != Val1Range.second; ++it) 1850 EXPECT_EQ(it->second, AbbrevPtrVal1); 1851 EXPECT_EQ(DIEs.find(Val2)->second, AbbrevPtrVal2); 1852 } 1853 1854 void VerifyWarning(DWARFContext &DwarfContext, StringRef Error) { 1855 SmallString<1024> Str; 1856 raw_svector_ostream Strm(Str); 1857 EXPECT_TRUE(DwarfContext.verify(Strm)); 1858 EXPECT_TRUE(Str.str().contains(Error)); 1859 } 1860 1861 void VerifyError(DWARFContext &DwarfContext, StringRef Error) { 1862 SmallString<1024> Str; 1863 raw_svector_ostream Strm(Str); 1864 EXPECT_FALSE(DwarfContext.verify(Strm)); 1865 EXPECT_TRUE(Str.str().contains(Error)); 1866 } 1867 1868 void VerifySuccess(DWARFContext &DwarfContext) { 1869 SmallString<1024> Str; 1870 raw_svector_ostream Strm(Str); 1871 EXPECT_TRUE(DwarfContext.verify(Strm)); 1872 } 1873 1874 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidCURef) { 1875 // Create a single compile unit with a single function that has a DW_AT_type 1876 // that is CU relative. The CU offset is not valid because it is larger than 1877 // the compile unit itself. 1878 1879 const char *yamldata = R"( 1880 debug_str: 1881 - '' 1882 - /tmp/main.c 1883 - main 1884 debug_abbrev: 1885 - Table: 1886 - Code: 0x00000001 1887 Tag: DW_TAG_compile_unit 1888 Children: DW_CHILDREN_yes 1889 Attributes: 1890 - Attribute: DW_AT_name 1891 Form: DW_FORM_strp 1892 - Code: 0x00000002 1893 Tag: DW_TAG_subprogram 1894 Children: DW_CHILDREN_no 1895 Attributes: 1896 - Attribute: DW_AT_name 1897 Form: DW_FORM_strp 1898 - Attribute: DW_AT_type 1899 Form: DW_FORM_ref4 1900 debug_info: 1901 - Version: 4 1902 AddrSize: 8 1903 Entries: 1904 - AbbrCode: 0x00000001 1905 Values: 1906 - Value: 0x0000000000000001 1907 - AbbrCode: 0x00000002 1908 Values: 1909 - Value: 0x000000000000000D 1910 - Value: 0x0000000000001234 1911 - AbbrCode: 0x00000000 1912 )"; 1913 auto ErrOrSections = DWARFYAML::emitDebugSections(StringRef(yamldata)); 1914 ASSERT_TRUE((bool)ErrOrSections); 1915 std::unique_ptr<DWARFContext> DwarfContext = 1916 DWARFContext::create(*ErrOrSections, 8); 1917 VerifyError(*DwarfContext, "error: DW_FORM_ref4 CU offset 0x00001234 is " 1918 "invalid (must be less than CU size of " 1919 "0x0000001a):"); 1920 } 1921 1922 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidRefAddr) { 1923 // Create a single compile unit with a single function that has an invalid 1924 // DW_AT_type with an invalid .debug_info offset in its DW_FORM_ref_addr. 1925 const char *yamldata = R"( 1926 debug_str: 1927 - '' 1928 - /tmp/main.c 1929 - main 1930 debug_abbrev: 1931 - Table: 1932 - Code: 0x00000001 1933 Tag: DW_TAG_compile_unit 1934 Children: DW_CHILDREN_yes 1935 Attributes: 1936 - Attribute: DW_AT_name 1937 Form: DW_FORM_strp 1938 - Code: 0x00000002 1939 Tag: DW_TAG_subprogram 1940 Children: DW_CHILDREN_no 1941 Attributes: 1942 - Attribute: DW_AT_name 1943 Form: DW_FORM_strp 1944 - Attribute: DW_AT_type 1945 Form: DW_FORM_ref_addr 1946 debug_info: 1947 - Version: 4 1948 AddrSize: 8 1949 Entries: 1950 - AbbrCode: 0x00000001 1951 Values: 1952 - Value: 0x0000000000000001 1953 - AbbrCode: 0x00000002 1954 Values: 1955 - Value: 0x000000000000000D 1956 - Value: 0x0000000000001234 1957 - AbbrCode: 0x00000000 1958 )"; 1959 auto ErrOrSections = DWARFYAML::emitDebugSections(StringRef(yamldata)); 1960 ASSERT_TRUE((bool)ErrOrSections); 1961 std::unique_ptr<DWARFContext> DwarfContext = 1962 DWARFContext::create(*ErrOrSections, 8); 1963 VerifyError(*DwarfContext, 1964 "error: DW_FORM_ref_addr offset beyond .debug_info bounds:"); 1965 } 1966 1967 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidRanges) { 1968 // Create a single compile unit with a DW_AT_ranges whose section offset 1969 // isn't valid. 1970 const char *yamldata = R"( 1971 debug_str: 1972 - '' 1973 - /tmp/main.c 1974 debug_abbrev: 1975 - Table: 1976 - Code: 0x00000001 1977 Tag: DW_TAG_compile_unit 1978 Children: DW_CHILDREN_no 1979 Attributes: 1980 - Attribute: DW_AT_name 1981 Form: DW_FORM_strp 1982 - Attribute: DW_AT_ranges 1983 Form: DW_FORM_sec_offset 1984 debug_info: 1985 - Version: 4 1986 AddrSize: 8 1987 Entries: 1988 - AbbrCode: 0x00000001 1989 Values: 1990 - Value: 0x0000000000000001 1991 - Value: 0x0000000000001000 1992 )"; 1993 auto ErrOrSections = DWARFYAML::emitDebugSections(StringRef(yamldata)); 1994 ASSERT_TRUE((bool)ErrOrSections); 1995 std::unique_ptr<DWARFContext> DwarfContext = 1996 DWARFContext::create(*ErrOrSections, 8); 1997 VerifyError( 1998 *DwarfContext, 1999 "error: DW_AT_ranges offset is beyond .debug_ranges bounds: 0x00001000"); 2000 } 2001 2002 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidRnglists) { 2003 // Create a single compile unit with a DW_AT_ranges whose section offset 2004 // isn't valid. 2005 const char *yamldata = R"( 2006 debug_str: 2007 - '' 2008 - /tmp/main.c 2009 debug_abbrev: 2010 - Table: 2011 - Code: 0x00000001 2012 Tag: DW_TAG_compile_unit 2013 Children: DW_CHILDREN_no 2014 Attributes: 2015 - Attribute: DW_AT_name 2016 Form: DW_FORM_strp 2017 - Attribute: DW_AT_ranges 2018 Form: DW_FORM_sec_offset 2019 debug_info: 2020 - Version: 5 2021 UnitType: DW_UT_compile 2022 AddrSize: 8 2023 Entries: 2024 - AbbrCode: 0x00000001 2025 Values: 2026 - Value: 0x0000000000000001 2027 - Value: 0x0000000000001000 2028 )"; 2029 auto ErrOrSections = DWARFYAML::emitDebugSections(StringRef(yamldata)); 2030 ASSERT_TRUE((bool)ErrOrSections); 2031 std::unique_ptr<DWARFContext> DwarfContext = 2032 DWARFContext::create(*ErrOrSections, 8); 2033 VerifyError(*DwarfContext, "error: DW_AT_ranges offset is beyond " 2034 ".debug_rnglists bounds: 0x00001000"); 2035 } 2036 2037 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidStmtList) { 2038 // Create a single compile unit with a DW_AT_stmt_list whose section offset 2039 // isn't valid. 2040 const char *yamldata = R"( 2041 debug_str: 2042 - '' 2043 - /tmp/main.c 2044 debug_abbrev: 2045 - Table: 2046 - Code: 0x00000001 2047 Tag: DW_TAG_compile_unit 2048 Children: DW_CHILDREN_no 2049 Attributes: 2050 - Attribute: DW_AT_name 2051 Form: DW_FORM_strp 2052 - Attribute: DW_AT_stmt_list 2053 Form: DW_FORM_sec_offset 2054 debug_info: 2055 - Version: 4 2056 AddrSize: 8 2057 Entries: 2058 - AbbrCode: 0x00000001 2059 Values: 2060 - Value: 0x0000000000000001 2061 - Value: 0x0000000000001000 2062 )"; 2063 auto ErrOrSections = DWARFYAML::emitDebugSections(StringRef(yamldata)); 2064 ASSERT_TRUE((bool)ErrOrSections); 2065 std::unique_ptr<DWARFContext> DwarfContext = 2066 DWARFContext::create(*ErrOrSections, 8); 2067 VerifyError( 2068 *DwarfContext, 2069 "error: DW_AT_stmt_list offset is beyond .debug_line bounds: 0x00001000"); 2070 } 2071 2072 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidStrp) { 2073 // Create a single compile unit with a single function that has an invalid 2074 // DW_FORM_strp for the DW_AT_name. 2075 const char *yamldata = R"( 2076 debug_str: 2077 - '' 2078 debug_abbrev: 2079 - Table: 2080 - Code: 0x00000001 2081 Tag: DW_TAG_compile_unit 2082 Children: DW_CHILDREN_no 2083 Attributes: 2084 - Attribute: DW_AT_name 2085 Form: DW_FORM_strp 2086 debug_info: 2087 - Version: 4 2088 AddrSize: 8 2089 Entries: 2090 - AbbrCode: 0x00000001 2091 Values: 2092 - Value: 0x0000000000001234 2093 )"; 2094 auto ErrOrSections = DWARFYAML::emitDebugSections(StringRef(yamldata)); 2095 ASSERT_TRUE((bool)ErrOrSections); 2096 std::unique_ptr<DWARFContext> DwarfContext = 2097 DWARFContext::create(*ErrOrSections, 8); 2098 VerifyError(*DwarfContext, 2099 "error: DW_FORM_strp offset beyond .debug_str bounds:"); 2100 } 2101 2102 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidRefAddrBetween) { 2103 // Create a single compile unit with a single function that has a DW_AT_type 2104 // with a valid .debug_info offset, but the offset is between two DIEs. 2105 const char *yamldata = R"( 2106 debug_str: 2107 - '' 2108 - /tmp/main.c 2109 - main 2110 debug_abbrev: 2111 - Table: 2112 - Code: 0x00000001 2113 Tag: DW_TAG_compile_unit 2114 Children: DW_CHILDREN_yes 2115 Attributes: 2116 - Attribute: DW_AT_name 2117 Form: DW_FORM_strp 2118 - Code: 0x00000002 2119 Tag: DW_TAG_subprogram 2120 Children: DW_CHILDREN_no 2121 Attributes: 2122 - Attribute: DW_AT_name 2123 Form: DW_FORM_strp 2124 - Attribute: DW_AT_type 2125 Form: DW_FORM_ref_addr 2126 debug_info: 2127 - Version: 4 2128 AddrSize: 8 2129 Entries: 2130 - AbbrCode: 0x00000001 2131 Values: 2132 - Value: 0x0000000000000001 2133 - AbbrCode: 0x00000002 2134 Values: 2135 - Value: 0x000000000000000D 2136 - Value: 0x0000000000000011 2137 - AbbrCode: 0x00000000 2138 )"; 2139 auto ErrOrSections = DWARFYAML::emitDebugSections(StringRef(yamldata)); 2140 ASSERT_TRUE((bool)ErrOrSections); 2141 std::unique_ptr<DWARFContext> DwarfContext = 2142 DWARFContext::create(*ErrOrSections, 8); 2143 VerifyError( 2144 *DwarfContext, 2145 "error: invalid DIE reference 0x00000011. Offset is in between DIEs:"); 2146 } 2147 2148 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidLineSequence) { 2149 // Create a single compile unit whose line table has a sequence in it where 2150 // the address decreases. 2151 StringRef yamldata = R"( 2152 debug_str: 2153 - '' 2154 - /tmp/main.c 2155 debug_abbrev: 2156 - Table: 2157 - Code: 0x00000001 2158 Tag: DW_TAG_compile_unit 2159 Children: DW_CHILDREN_no 2160 Attributes: 2161 - Attribute: DW_AT_name 2162 Form: DW_FORM_strp 2163 - Attribute: DW_AT_stmt_list 2164 Form: DW_FORM_sec_offset 2165 debug_info: 2166 - Version: 4 2167 AddrSize: 8 2168 Entries: 2169 - AbbrCode: 0x00000001 2170 Values: 2171 - Value: 0x0000000000000001 2172 - Value: 0x0000000000000000 2173 debug_line: 2174 - Version: 2 2175 MinInstLength: 1 2176 DefaultIsStmt: 1 2177 LineBase: 251 2178 LineRange: 14 2179 OpcodeBase: 13 2180 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ] 2181 IncludeDirs: 2182 - /tmp 2183 Files: 2184 - Name: main.c 2185 DirIdx: 1 2186 ModTime: 0 2187 Length: 0 2188 Opcodes: 2189 - Opcode: DW_LNS_extended_op 2190 ExtLen: 9 2191 SubOpcode: DW_LNE_set_address 2192 Data: 4112 2193 - Opcode: DW_LNS_advance_line 2194 SData: 9 2195 Data: 4112 2196 - Opcode: DW_LNS_copy 2197 Data: 4112 2198 - Opcode: DW_LNS_advance_pc 2199 Data: 18446744073709551600 2200 - Opcode: DW_LNS_extended_op 2201 ExtLen: 1 2202 SubOpcode: DW_LNE_end_sequence 2203 Data: 18446744073709551600 2204 )"; 2205 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 2206 ASSERT_TRUE((bool)ErrOrSections); 2207 std::unique_ptr<DWARFContext> DwarfContext = 2208 DWARFContext::create(*ErrOrSections, 8); 2209 VerifyError(*DwarfContext, "error: .debug_line[0x00000000] row[1] decreases " 2210 "in address from previous row:"); 2211 } 2212 2213 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidLineFileIndex) { 2214 // Create a single compile unit whose line table has a line table row with 2215 // an invalid file index. 2216 StringRef yamldata = R"( 2217 debug_str: 2218 - '' 2219 - /tmp/main.c 2220 debug_abbrev: 2221 - Table: 2222 - Code: 0x00000001 2223 Tag: DW_TAG_compile_unit 2224 Children: DW_CHILDREN_no 2225 Attributes: 2226 - Attribute: DW_AT_name 2227 Form: DW_FORM_strp 2228 - Attribute: DW_AT_stmt_list 2229 Form: DW_FORM_sec_offset 2230 debug_info: 2231 - Version: 4 2232 AddrSize: 8 2233 Entries: 2234 - AbbrCode: 0x00000001 2235 Values: 2236 - Value: 0x0000000000000001 2237 - Value: 0x0000000000000000 2238 debug_line: 2239 - Version: 2 2240 MinInstLength: 1 2241 DefaultIsStmt: 1 2242 LineBase: 251 2243 LineRange: 14 2244 OpcodeBase: 13 2245 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ] 2246 IncludeDirs: 2247 - /tmp 2248 Files: 2249 - Name: main.c 2250 DirIdx: 1 2251 ModTime: 0 2252 Length: 0 2253 Opcodes: 2254 - Opcode: DW_LNS_extended_op 2255 ExtLen: 9 2256 SubOpcode: DW_LNE_set_address 2257 Data: 4096 2258 - Opcode: DW_LNS_advance_line 2259 SData: 9 2260 Data: 4096 2261 - Opcode: DW_LNS_copy 2262 Data: 4096 2263 - Opcode: DW_LNS_advance_pc 2264 Data: 16 2265 - Opcode: DW_LNS_set_file 2266 Data: 5 2267 - Opcode: DW_LNS_extended_op 2268 ExtLen: 1 2269 SubOpcode: DW_LNE_end_sequence 2270 Data: 5 2271 )"; 2272 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 2273 ASSERT_TRUE((bool)ErrOrSections); 2274 std::unique_ptr<DWARFContext> DwarfContext = 2275 DWARFContext::create(*ErrOrSections, 8); 2276 VerifyError(*DwarfContext, "error: .debug_line[0x00000000][1] has invalid " 2277 "file index 5 (valid values are [1,1]):"); 2278 } 2279 2280 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidLineTablePorlogueDirIndex) { 2281 // Create a single compile unit whose line table has a prologue with an 2282 // invalid dir index. 2283 StringRef yamldata = R"( 2284 debug_str: 2285 - '' 2286 - /tmp/main.c 2287 debug_abbrev: 2288 - Table: 2289 - Code: 0x00000001 2290 Tag: DW_TAG_compile_unit 2291 Children: DW_CHILDREN_no 2292 Attributes: 2293 - Attribute: DW_AT_name 2294 Form: DW_FORM_strp 2295 - Attribute: DW_AT_stmt_list 2296 Form: DW_FORM_sec_offset 2297 debug_info: 2298 - Version: 4 2299 AddrSize: 8 2300 Entries: 2301 - AbbrCode: 0x00000001 2302 Values: 2303 - Value: 0x0000000000000001 2304 - Value: 0x0000000000000000 2305 debug_line: 2306 - Version: 2 2307 MinInstLength: 1 2308 DefaultIsStmt: 1 2309 LineBase: 251 2310 LineRange: 14 2311 OpcodeBase: 13 2312 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ] 2313 IncludeDirs: 2314 - /tmp 2315 Files: 2316 - Name: main.c 2317 DirIdx: 2 2318 ModTime: 0 2319 Length: 0 2320 Opcodes: 2321 - Opcode: DW_LNS_extended_op 2322 ExtLen: 9 2323 SubOpcode: DW_LNE_set_address 2324 Data: 4096 2325 - Opcode: DW_LNS_advance_line 2326 SData: 9 2327 Data: 4096 2328 - Opcode: DW_LNS_copy 2329 Data: 4096 2330 - Opcode: DW_LNS_advance_pc 2331 Data: 16 2332 - Opcode: DW_LNS_set_file 2333 Data: 1 2334 - Opcode: DW_LNS_extended_op 2335 ExtLen: 1 2336 SubOpcode: DW_LNE_end_sequence 2337 Data: 1 2338 )"; 2339 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 2340 ASSERT_TRUE((bool)ErrOrSections); 2341 std::unique_ptr<DWARFContext> DwarfContext = 2342 DWARFContext::create(*ErrOrSections, 8); 2343 VerifyError(*DwarfContext, 2344 "error: .debug_line[0x00000000].prologue." 2345 "file_names[1].dir_idx contains an invalid index: 2"); 2346 } 2347 2348 TEST(DWARFDebugInfo, TestDwarfVerifyDuplicateFileWarning) { 2349 // Create a single compile unit whose line table has a prologue with an 2350 // invalid dir index. 2351 StringRef yamldata = R"( 2352 debug_str: 2353 - '' 2354 - /tmp/main.c 2355 debug_abbrev: 2356 - Table: 2357 - Code: 0x00000001 2358 Tag: DW_TAG_compile_unit 2359 Children: DW_CHILDREN_no 2360 Attributes: 2361 - Attribute: DW_AT_name 2362 Form: DW_FORM_strp 2363 - Attribute: DW_AT_stmt_list 2364 Form: DW_FORM_sec_offset 2365 debug_info: 2366 - Version: 4 2367 AddrSize: 8 2368 Entries: 2369 - AbbrCode: 0x00000001 2370 Values: 2371 - Value: 0x0000000000000001 2372 - Value: 0x0000000000000000 2373 debug_line: 2374 - Version: 2 2375 MinInstLength: 1 2376 DefaultIsStmt: 1 2377 LineBase: 251 2378 LineRange: 14 2379 OpcodeBase: 13 2380 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ] 2381 IncludeDirs: 2382 - /tmp 2383 Files: 2384 - Name: main.c 2385 DirIdx: 1 2386 ModTime: 0 2387 Length: 0 2388 - Name: main.c 2389 DirIdx: 1 2390 ModTime: 0 2391 Length: 0 2392 Opcodes: 2393 - Opcode: DW_LNS_extended_op 2394 ExtLen: 9 2395 SubOpcode: DW_LNE_set_address 2396 Data: 4096 2397 - Opcode: DW_LNS_advance_line 2398 SData: 9 2399 Data: 4096 2400 - Opcode: DW_LNS_copy 2401 Data: 4096 2402 - Opcode: DW_LNS_advance_pc 2403 Data: 16 2404 - Opcode: DW_LNS_set_file 2405 Data: 1 2406 - Opcode: DW_LNS_extended_op 2407 ExtLen: 1 2408 SubOpcode: DW_LNE_end_sequence 2409 Data: 2 2410 )"; 2411 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 2412 ASSERT_TRUE((bool)ErrOrSections); 2413 std::unique_ptr<DWARFContext> DwarfContext = 2414 DWARFContext::create(*ErrOrSections, 8); 2415 VerifyWarning(*DwarfContext, 2416 "warning: .debug_line[0x00000000].prologue.file_names[2] is " 2417 "a duplicate of file_names[1]"); 2418 } 2419 2420 TEST(DWARFDebugInfo, TestDwarfVerifyCUDontShareLineTable) { 2421 // Create a two compile units where both compile units share the same 2422 // DW_AT_stmt_list value and verify we report the error correctly. 2423 StringRef yamldata = R"( 2424 debug_str: 2425 - '' 2426 - /tmp/main.c 2427 - /tmp/foo.c 2428 debug_abbrev: 2429 - Table: 2430 - Code: 0x00000001 2431 Tag: DW_TAG_compile_unit 2432 Children: DW_CHILDREN_no 2433 Attributes: 2434 - Attribute: DW_AT_name 2435 Form: DW_FORM_strp 2436 - Attribute: DW_AT_stmt_list 2437 Form: DW_FORM_sec_offset 2438 debug_info: 2439 - Version: 4 2440 AddrSize: 8 2441 Entries: 2442 - AbbrCode: 0x00000001 2443 Values: 2444 - Value: 0x0000000000000001 2445 - Value: 0x0000000000000000 2446 - Length: 16 2447 Version: 4 2448 AbbrevTableID: 0 2449 AddrSize: 8 2450 Entries: 2451 - AbbrCode: 0x00000001 2452 Values: 2453 - Value: 0x000000000000000D 2454 - Value: 0x0000000000000000 2455 debug_line: 2456 - Version: 2 2457 MinInstLength: 1 2458 DefaultIsStmt: 1 2459 LineBase: 251 2460 LineRange: 14 2461 OpcodeBase: 13 2462 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ] 2463 IncludeDirs: 2464 - /tmp 2465 Files: 2466 - Name: main.c 2467 DirIdx: 1 2468 ModTime: 0 2469 Length: 0 2470 Opcodes: 2471 - Opcode: DW_LNS_extended_op 2472 ExtLen: 9 2473 SubOpcode: DW_LNE_set_address 2474 Data: 4096 2475 - Opcode: DW_LNS_advance_line 2476 SData: 9 2477 Data: 4096 2478 - Opcode: DW_LNS_copy 2479 Data: 4096 2480 - Opcode: DW_LNS_advance_pc 2481 Data: 256 2482 - Opcode: DW_LNS_extended_op 2483 ExtLen: 1 2484 SubOpcode: DW_LNE_end_sequence 2485 Data: 256 2486 )"; 2487 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 2488 ASSERT_TRUE((bool)ErrOrSections); 2489 std::unique_ptr<DWARFContext> DwarfContext = 2490 DWARFContext::create(*ErrOrSections, 8); 2491 VerifyError(*DwarfContext, 2492 "error: two compile unit DIEs, 0x0000000b and " 2493 "0x0000001f, have the same DW_AT_stmt_list section " 2494 "offset:"); 2495 } 2496 2497 TEST(DWARFDebugInfo, TestErrorReporting) { 2498 Triple Triple("x86_64-pc-linux"); 2499 if (!isConfigurationSupported(Triple)) 2500 return; 2501 2502 auto ExpectedDG = dwarfgen::Generator::create(Triple, 4 /*DwarfVersion*/); 2503 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded()); 2504 dwarfgen::Generator *DG = ExpectedDG.get().get(); 2505 AsmPrinter *AP = DG->getAsmPrinter(); 2506 MCContext *MC = DG->getMCContext(); 2507 2508 // Emit two compressed sections with broken headers. 2509 AP->OutStreamer->SwitchSection( 2510 MC->getELFSection(".zdebug_foo", 0 /*Type*/, 0 /*Flags*/)); 2511 AP->OutStreamer->emitBytes("0"); 2512 AP->OutStreamer->SwitchSection( 2513 MC->getELFSection(".zdebug_bar", 0 /*Type*/, 0 /*Flags*/)); 2514 AP->OutStreamer->emitBytes("0"); 2515 2516 MemoryBufferRef FileBuffer(DG->generate(), "dwarf"); 2517 auto Obj = object::ObjectFile::createObjectFile(FileBuffer); 2518 EXPECT_TRUE((bool)Obj); 2519 2520 // DWARFContext parses whole file and finds the two errors we expect. 2521 int Errors = 0; 2522 std::unique_ptr<DWARFContext> Ctx1 = 2523 DWARFContext::create(**Obj, nullptr, "", [&](Error E) { 2524 ++Errors; 2525 consumeError(std::move(E)); 2526 }); 2527 EXPECT_TRUE(Errors == 2); 2528 } 2529 2530 TEST(DWARFDebugInfo, TestDwarfVerifyCURangesIncomplete) { 2531 // Create a single compile unit with a single function. The compile 2532 // unit has a DW_AT_ranges attribute that doesn't fully contain the 2533 // address range of the function. The verification should fail due to 2534 // the CU ranges not containing all of the address ranges of all of the 2535 // functions. 2536 StringRef yamldata = R"( 2537 debug_str: 2538 - '' 2539 - /tmp/main.c 2540 debug_abbrev: 2541 - Table: 2542 - Code: 0x00000001 2543 Tag: DW_TAG_compile_unit 2544 Children: DW_CHILDREN_yes 2545 Attributes: 2546 - Attribute: DW_AT_low_pc 2547 Form: DW_FORM_addr 2548 - Attribute: DW_AT_high_pc 2549 Form: DW_FORM_addr 2550 - Attribute: DW_AT_name 2551 Form: DW_FORM_strp 2552 - Code: 0x00000002 2553 Tag: DW_TAG_subprogram 2554 Children: DW_CHILDREN_no 2555 Attributes: 2556 - Attribute: DW_AT_low_pc 2557 Form: DW_FORM_addr 2558 - Attribute: DW_AT_high_pc 2559 Form: DW_FORM_addr 2560 debug_info: 2561 - Version: 4 2562 AddrSize: 8 2563 Entries: 2564 - AbbrCode: 0x00000001 2565 Values: 2566 - Value: 0x0000000000001000 2567 - Value: 0x0000000000001500 2568 - Value: 0x0000000000000001 2569 - AbbrCode: 0x00000002 2570 Values: 2571 - Value: 0x0000000000001000 2572 - Value: 0x0000000000002000 2573 - AbbrCode: 0x00000000 2574 )"; 2575 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 2576 ASSERT_TRUE((bool)ErrOrSections); 2577 std::unique_ptr<DWARFContext> DwarfContext = 2578 DWARFContext::create(*ErrOrSections, 8); 2579 VerifyError(*DwarfContext, "error: DIE address ranges are not " 2580 "contained in its parent's ranges:"); 2581 } 2582 2583 TEST(DWARFDebugInfo, TestDwarfVerifyLexicalBlockRanges) { 2584 // Create a single compile unit with a single function that has a lexical 2585 // block whose address range is not contained in the function address range. 2586 StringRef yamldata = R"( 2587 debug_str: 2588 - '' 2589 - /tmp/main.c 2590 - main 2591 debug_abbrev: 2592 - Table: 2593 - Code: 0x00000001 2594 Tag: DW_TAG_compile_unit 2595 Children: DW_CHILDREN_yes 2596 Attributes: 2597 - Attribute: DW_AT_name 2598 Form: DW_FORM_strp 2599 - Code: 0x00000002 2600 Tag: DW_TAG_subprogram 2601 Children: DW_CHILDREN_yes 2602 Attributes: 2603 - Attribute: DW_AT_name 2604 Form: DW_FORM_strp 2605 - Attribute: DW_AT_low_pc 2606 Form: DW_FORM_addr 2607 - Attribute: DW_AT_high_pc 2608 Form: DW_FORM_addr 2609 - Code: 0x00000003 2610 Tag: DW_TAG_lexical_block 2611 Children: DW_CHILDREN_no 2612 Attributes: 2613 - Attribute: DW_AT_low_pc 2614 Form: DW_FORM_addr 2615 - Attribute: DW_AT_high_pc 2616 Form: DW_FORM_addr 2617 debug_info: 2618 - Version: 4 2619 AddrSize: 8 2620 Entries: 2621 - AbbrCode: 0x00000001 2622 Values: 2623 - Value: 0x0000000000000001 2624 - AbbrCode: 0x00000002 2625 Values: 2626 - Value: 0x000000000000000D 2627 - Value: 0x0000000000001000 2628 - Value: 0x0000000000002000 2629 - AbbrCode: 0x00000003 2630 Values: 2631 - Value: 0x0000000000001000 2632 - Value: 0x0000000000002001 2633 - AbbrCode: 0x00000000 2634 - AbbrCode: 0x00000000 2635 )"; 2636 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 2637 ASSERT_TRUE((bool)ErrOrSections); 2638 std::unique_ptr<DWARFContext> DwarfContext = 2639 DWARFContext::create(*ErrOrSections, 8); 2640 VerifyError(*DwarfContext, "error: DIE address ranges are not " 2641 "contained in its parent's ranges:"); 2642 } 2643 2644 TEST(DWARFDebugInfo, TestDwarfVerifyOverlappingFunctionRanges) { 2645 // Create a single compile unit with a two functions that have overlapping 2646 // address ranges. 2647 StringRef yamldata = R"( 2648 debug_str: 2649 - '' 2650 - /tmp/main.c 2651 - main 2652 - foo 2653 debug_abbrev: 2654 - Table: 2655 - Code: 0x00000001 2656 Tag: DW_TAG_compile_unit 2657 Children: DW_CHILDREN_yes 2658 Attributes: 2659 - Attribute: DW_AT_name 2660 Form: DW_FORM_strp 2661 - Code: 0x00000002 2662 Tag: DW_TAG_subprogram 2663 Children: DW_CHILDREN_no 2664 Attributes: 2665 - Attribute: DW_AT_name 2666 Form: DW_FORM_strp 2667 - Attribute: DW_AT_low_pc 2668 Form: DW_FORM_addr 2669 - Attribute: DW_AT_high_pc 2670 Form: DW_FORM_addr 2671 debug_info: 2672 - Version: 4 2673 AddrSize: 8 2674 Entries: 2675 - AbbrCode: 0x00000001 2676 Values: 2677 - Value: 0x0000000000000001 2678 - AbbrCode: 0x00000002 2679 Values: 2680 - Value: 0x000000000000000D 2681 - Value: 0x0000000000001000 2682 - Value: 0x0000000000002000 2683 - AbbrCode: 0x00000002 2684 Values: 2685 - Value: 0x0000000000000012 2686 - Value: 0x0000000000001FFF 2687 - Value: 0x0000000000002000 2688 - AbbrCode: 0x00000000 2689 )"; 2690 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 2691 ASSERT_TRUE((bool)ErrOrSections); 2692 std::unique_ptr<DWARFContext> DwarfContext = 2693 DWARFContext::create(*ErrOrSections, 8); 2694 VerifyError(*DwarfContext, "error: DIEs have overlapping address ranges:"); 2695 } 2696 2697 TEST(DWARFDebugInfo, TestDwarfVerifyOverlappingLexicalBlockRanges) { 2698 // Create a single compile unit with a one function that has two lexical 2699 // blocks with overlapping address ranges. 2700 StringRef yamldata = R"( 2701 debug_str: 2702 - '' 2703 - /tmp/main.c 2704 - main 2705 debug_abbrev: 2706 - Table: 2707 - Code: 0x00000001 2708 Tag: DW_TAG_compile_unit 2709 Children: DW_CHILDREN_yes 2710 Attributes: 2711 - Attribute: DW_AT_low_pc 2712 Form: DW_FORM_addr 2713 - Attribute: DW_AT_high_pc 2714 Form: DW_FORM_addr 2715 - Attribute: DW_AT_name 2716 Form: DW_FORM_strp 2717 - Code: 0x00000002 2718 Tag: DW_TAG_subprogram 2719 Children: DW_CHILDREN_yes 2720 Attributes: 2721 - Attribute: DW_AT_name 2722 Form: DW_FORM_strp 2723 - Attribute: DW_AT_low_pc 2724 Form: DW_FORM_addr 2725 - Attribute: DW_AT_high_pc 2726 Form: DW_FORM_addr 2727 - Code: 0x00000003 2728 Tag: DW_TAG_lexical_block 2729 Children: DW_CHILDREN_no 2730 Attributes: 2731 - Attribute: DW_AT_low_pc 2732 Form: DW_FORM_addr 2733 - Attribute: DW_AT_high_pc 2734 Form: DW_FORM_addr 2735 debug_info: 2736 - Version: 4 2737 AddrSize: 8 2738 Entries: 2739 - AbbrCode: 0x00000001 2740 Values: 2741 - Value: 0x0000000000001000 2742 - Value: 0x0000000000002000 2743 - Value: 0x0000000000000001 2744 - AbbrCode: 0x00000002 2745 Values: 2746 - Value: 0x000000000000000D 2747 - Value: 0x0000000000001000 2748 - Value: 0x0000000000002000 2749 - AbbrCode: 0x00000003 2750 Values: 2751 - Value: 0x0000000000001100 2752 - Value: 0x0000000000001300 2753 - AbbrCode: 0x00000003 2754 Values: 2755 - Value: 0x00000000000012FF 2756 - Value: 0x0000000000001300 2757 - AbbrCode: 0x00000000 2758 - AbbrCode: 0x00000000 2759 )"; 2760 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 2761 ASSERT_TRUE((bool)ErrOrSections); 2762 std::unique_ptr<DWARFContext> DwarfContext = 2763 DWARFContext::create(*ErrOrSections, 8); 2764 VerifyError(*DwarfContext, "error: DIEs have overlapping address ranges:"); 2765 } 2766 2767 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidDIERange) { 2768 // Create a single compile unit with a single function that has an invalid 2769 // address range where the high PC is smaller than the low PC. 2770 StringRef yamldata = R"( 2771 debug_str: 2772 - '' 2773 - /tmp/main.c 2774 - main 2775 debug_abbrev: 2776 - Table: 2777 - Code: 0x00000001 2778 Tag: DW_TAG_compile_unit 2779 Children: DW_CHILDREN_yes 2780 Attributes: 2781 - Attribute: DW_AT_name 2782 Form: DW_FORM_strp 2783 - Code: 0x00000002 2784 Tag: DW_TAG_subprogram 2785 Children: DW_CHILDREN_no 2786 Attributes: 2787 - Attribute: DW_AT_name 2788 Form: DW_FORM_strp 2789 - Attribute: DW_AT_low_pc 2790 Form: DW_FORM_addr 2791 - Attribute: DW_AT_high_pc 2792 Form: DW_FORM_addr 2793 debug_info: 2794 - Version: 4 2795 AddrSize: 8 2796 Entries: 2797 - AbbrCode: 0x00000001 2798 Values: 2799 - Value: 0x0000000000000001 2800 - AbbrCode: 0x00000002 2801 Values: 2802 - Value: 0x000000000000000D 2803 - Value: 0x0000000000001000 2804 - Value: 0x0000000000000900 2805 - AbbrCode: 0x00000000 2806 )"; 2807 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 2808 ASSERT_TRUE((bool)ErrOrSections); 2809 std::unique_ptr<DWARFContext> DwarfContext = 2810 DWARFContext::create(*ErrOrSections, 8); 2811 VerifyError(*DwarfContext, "error: Invalid address range"); 2812 } 2813 2814 TEST(DWARFDebugInfo, TestDwarfVerifyElidedDoesntFail) { 2815 // Create a single compile unit with two functions: one that has a valid range 2816 // and one whose low and high PC are the same. When the low and high PC are 2817 // the same, this indicates the function was dead code stripped. We want to 2818 // ensure that verification succeeds. 2819 StringRef yamldata = R"( 2820 debug_str: 2821 - '' 2822 - /tmp/main.c 2823 - main 2824 - elided 2825 debug_abbrev: 2826 - Table: 2827 - Code: 0x00000001 2828 Tag: DW_TAG_compile_unit 2829 Children: DW_CHILDREN_yes 2830 Attributes: 2831 - Attribute: DW_AT_low_pc 2832 Form: DW_FORM_addr 2833 - Attribute: DW_AT_high_pc 2834 Form: DW_FORM_addr 2835 - Attribute: DW_AT_name 2836 Form: DW_FORM_strp 2837 - Code: 0x00000002 2838 Tag: DW_TAG_subprogram 2839 Children: DW_CHILDREN_no 2840 Attributes: 2841 - Attribute: DW_AT_name 2842 Form: DW_FORM_strp 2843 - Attribute: DW_AT_low_pc 2844 Form: DW_FORM_addr 2845 - Attribute: DW_AT_high_pc 2846 Form: DW_FORM_addr 2847 debug_info: 2848 - Version: 4 2849 AddrSize: 8 2850 Entries: 2851 - AbbrCode: 0x00000001 2852 Values: 2853 - Value: 0x0000000000001000 2854 - Value: 0x0000000000002000 2855 - Value: 0x0000000000000001 2856 - AbbrCode: 0x00000002 2857 Values: 2858 - Value: 0x000000000000000D 2859 - Value: 0x0000000000001000 2860 - Value: 0x0000000000002000 2861 - AbbrCode: 0x00000002 2862 Values: 2863 - Value: 0x0000000000000012 2864 - Value: 0x0000000000002000 2865 - Value: 0x0000000000002000 2866 - AbbrCode: 0x00000000 2867 )"; 2868 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 2869 ASSERT_TRUE((bool)ErrOrSections); 2870 std::unique_ptr<DWARFContext> DwarfContext = 2871 DWARFContext::create(*ErrOrSections, 8); 2872 VerifySuccess(*DwarfContext); 2873 } 2874 2875 TEST(DWARFDebugInfo, TestDwarfVerifyNestedFunctions) { 2876 // Create a single compile unit with a nested function which is not contained 2877 // in its parent. Although LLVM doesn't generate this, it is valid accoridng 2878 // to the DWARF standard. 2879 StringRef yamldata = R"( 2880 debug_str: 2881 - '' 2882 - /tmp/main.c 2883 - main 2884 - nested 2885 debug_abbrev: 2886 - Table: 2887 - Code: 0x00000001 2888 Tag: DW_TAG_compile_unit 2889 Children: DW_CHILDREN_yes 2890 Attributes: 2891 - Attribute: DW_AT_low_pc 2892 Form: DW_FORM_addr 2893 - Attribute: DW_AT_high_pc 2894 Form: DW_FORM_addr 2895 - Attribute: DW_AT_name 2896 Form: DW_FORM_strp 2897 - Code: 0x00000002 2898 Tag: DW_TAG_subprogram 2899 Children: DW_CHILDREN_yes 2900 Attributes: 2901 - Attribute: DW_AT_name 2902 Form: DW_FORM_strp 2903 - Attribute: DW_AT_low_pc 2904 Form: DW_FORM_addr 2905 - Attribute: DW_AT_high_pc 2906 Form: DW_FORM_addr 2907 debug_info: 2908 - Version: 4 2909 AddrSize: 8 2910 Entries: 2911 - AbbrCode: 0x00000001 2912 Values: 2913 - Value: 0x0000000000001000 2914 - Value: 0x0000000000002000 2915 - Value: 0x0000000000000001 2916 - AbbrCode: 0x00000002 2917 Values: 2918 - Value: 0x000000000000000D 2919 - Value: 0x0000000000001000 2920 - Value: 0x0000000000001500 2921 - AbbrCode: 0x00000002 2922 Values: 2923 - Value: 0x0000000000000012 2924 - Value: 0x0000000000001500 2925 - Value: 0x0000000000002000 2926 - AbbrCode: 0x00000000 2927 - AbbrCode: 0x00000000 2928 - AbbrCode: 0x00000000 2929 )"; 2930 auto ErrOrSections = DWARFYAML::emitDebugSections(yamldata); 2931 ASSERT_TRUE((bool)ErrOrSections); 2932 std::unique_ptr<DWARFContext> DwarfContext = 2933 DWARFContext::create(*ErrOrSections, 8); 2934 VerifySuccess(*DwarfContext); 2935 } 2936 2937 TEST(DWARFDebugInfo, TestDWARFDieRangeInfoContains) { 2938 DWARFVerifier::DieRangeInfo Empty; 2939 ASSERT_TRUE(Empty.contains(Empty)); 2940 2941 DWARFVerifier::DieRangeInfo Ranges( 2942 {{0x10, 0x20}, {0x30, 0x40}, {0x40, 0x50}}); 2943 2944 ASSERT_TRUE(Ranges.contains(Empty)); 2945 ASSERT_FALSE(Ranges.contains({{{0x0f, 0x10}}})); 2946 ASSERT_FALSE(Ranges.contains({{{0x0f, 0x20}}})); 2947 ASSERT_FALSE(Ranges.contains({{{0x0f, 0x21}}})); 2948 2949 // Test ranges that start at R's start address 2950 ASSERT_TRUE(Ranges.contains({{{0x10, 0x10}}})); 2951 ASSERT_TRUE(Ranges.contains({{{0x10, 0x11}}})); 2952 ASSERT_TRUE(Ranges.contains({{{0x10, 0x20}}})); 2953 ASSERT_FALSE(Ranges.contains({{{0x10, 0x21}}})); 2954 2955 ASSERT_TRUE(Ranges.contains({{{0x11, 0x12}}})); 2956 2957 // Test ranges that start at last bytes of Range 2958 ASSERT_TRUE(Ranges.contains({{{0x1f, 0x20}}})); 2959 ASSERT_FALSE(Ranges.contains({{{0x1f, 0x21}}})); 2960 2961 // Test ranges that start after Range 2962 ASSERT_TRUE(Ranges.contains({{{0x20, 0x20}}})); 2963 ASSERT_FALSE(Ranges.contains({{{0x20, 0x21}}})); 2964 2965 ASSERT_TRUE(Ranges.contains({{{0x31, 0x32}}})); 2966 ASSERT_TRUE(Ranges.contains({{{0x3f, 0x40}}})); 2967 ASSERT_TRUE(Ranges.contains({{{0x10, 0x20}, {0x30, 0x40}}})); 2968 ASSERT_TRUE(Ranges.contains({{{0x11, 0x12}, {0x31, 0x32}}})); 2969 ASSERT_TRUE(Ranges.contains( 2970 {{{0x11, 0x12}, {0x12, 0x13}, {0x31, 0x32}, {0x32, 0x33}}})); 2971 ASSERT_FALSE(Ranges.contains({{{0x11, 0x12}, 2972 {0x12, 0x13}, 2973 {0x20, 0x21}, 2974 {0x31, 0x32}, 2975 {0x32, 0x33}}})); 2976 ASSERT_FALSE(Ranges.contains( 2977 {{{0x11, 0x12}, {0x12, 0x13}, {0x31, 0x32}, {0x32, 0x51}}})); 2978 ASSERT_TRUE(Ranges.contains({{{0x11, 0x12}, {0x30, 0x50}}})); 2979 ASSERT_FALSE(Ranges.contains({{{0x30, 0x51}}})); 2980 ASSERT_FALSE(Ranges.contains({{{0x50, 0x51}}})); 2981 } 2982 2983 namespace { 2984 2985 void AssertRangesIntersect(const DWARFAddressRange &LHS, 2986 const DWARFAddressRange &RHS) { 2987 ASSERT_TRUE(LHS.intersects(RHS)); 2988 ASSERT_TRUE(RHS.intersects(LHS)); 2989 } 2990 void AssertRangesDontIntersect(const DWARFAddressRange &LHS, 2991 const DWARFAddressRange &RHS) { 2992 ASSERT_FALSE(LHS.intersects(RHS)); 2993 ASSERT_FALSE(RHS.intersects(LHS)); 2994 } 2995 2996 void AssertRangesIntersect(const DWARFVerifier::DieRangeInfo &LHS, 2997 const DWARFAddressRangesVector &Ranges) { 2998 DWARFVerifier::DieRangeInfo RHS(Ranges); 2999 ASSERT_TRUE(LHS.intersects(RHS)); 3000 ASSERT_TRUE(RHS.intersects(LHS)); 3001 } 3002 3003 void AssertRangesDontIntersect(const DWARFVerifier::DieRangeInfo &LHS, 3004 const DWARFAddressRangesVector &Ranges) { 3005 DWARFVerifier::DieRangeInfo RHS(Ranges); 3006 ASSERT_FALSE(LHS.intersects(RHS)); 3007 ASSERT_FALSE(RHS.intersects(LHS)); 3008 } 3009 3010 } // namespace 3011 TEST(DWARFDebugInfo, TestDwarfRangesIntersect) { 3012 DWARFAddressRange R(0x10, 0x20); 3013 3014 //---------------------------------------------------------------------- 3015 // Test ranges that start before R... 3016 //---------------------------------------------------------------------- 3017 // Other range ends before start of R 3018 AssertRangesDontIntersect(R, {0x00, 0x10}); 3019 // Other range end address is start of a R 3020 AssertRangesIntersect(R, {0x00, 0x11}); 3021 // Other range end address is in R 3022 AssertRangesIntersect(R, {0x00, 0x15}); 3023 // Other range end address is at and of R 3024 AssertRangesIntersect(R, {0x00, 0x20}); 3025 // Other range end address is past end of R 3026 AssertRangesIntersect(R, {0x00, 0x40}); 3027 3028 //---------------------------------------------------------------------- 3029 // Test ranges that start at R's start address 3030 //---------------------------------------------------------------------- 3031 // Ensure empty ranges doesn't match 3032 AssertRangesDontIntersect(R, {0x10, 0x10}); 3033 // 1 byte of Range 3034 AssertRangesIntersect(R, {0x10, 0x11}); 3035 // same as Range 3036 AssertRangesIntersect(R, {0x10, 0x20}); 3037 // 1 byte past Range 3038 AssertRangesIntersect(R, {0x10, 0x21}); 3039 3040 //---------------------------------------------------------------------- 3041 // Test ranges that start inside Range 3042 //---------------------------------------------------------------------- 3043 // empty in range 3044 AssertRangesDontIntersect(R, {0x11, 0x11}); 3045 // all in Range 3046 AssertRangesIntersect(R, {0x11, 0x1f}); 3047 // ends at end of Range 3048 AssertRangesIntersect(R, {0x11, 0x20}); 3049 // ends past Range 3050 AssertRangesIntersect(R, {0x11, 0x21}); 3051 3052 //---------------------------------------------------------------------- 3053 // Test ranges that start at last bytes of Range 3054 //---------------------------------------------------------------------- 3055 // ends at end of Range 3056 AssertRangesIntersect(R, {0x1f, 0x20}); 3057 // ends past Range 3058 AssertRangesIntersect(R, {0x1f, 0x21}); 3059 3060 //---------------------------------------------------------------------- 3061 // Test ranges that start after Range 3062 //---------------------------------------------------------------------- 3063 // empty just past in Range 3064 AssertRangesDontIntersect(R, {0x20, 0x20}); 3065 // valid past Range 3066 AssertRangesDontIntersect(R, {0x20, 0x21}); 3067 } 3068 3069 TEST(DWARFDebugInfo, TestDWARFDieRangeInfoIntersects) { 3070 3071 DWARFVerifier::DieRangeInfo Ranges({{0x10, 0x20}, {0x30, 0x40}}); 3072 3073 // Test empty range 3074 AssertRangesDontIntersect(Ranges, {}); 3075 // Test range that appears before all ranges in Ranges 3076 AssertRangesDontIntersect(Ranges, {{0x00, 0x10}}); 3077 // Test range that appears between ranges in Ranges 3078 AssertRangesDontIntersect(Ranges, {{0x20, 0x30}}); 3079 // Test range that appears after ranges in Ranges 3080 AssertRangesDontIntersect(Ranges, {{0x40, 0x50}}); 3081 3082 // Test range that start before first range 3083 AssertRangesIntersect(Ranges, {{0x00, 0x11}}); 3084 // Test range that start at first range 3085 AssertRangesIntersect(Ranges, {{0x10, 0x11}}); 3086 // Test range that start in first range 3087 AssertRangesIntersect(Ranges, {{0x11, 0x12}}); 3088 // Test range that start at end of first range 3089 AssertRangesIntersect(Ranges, {{0x1f, 0x20}}); 3090 // Test range that starts at end of first range 3091 AssertRangesDontIntersect(Ranges, {{0x20, 0x21}}); 3092 // Test range that starts at end of first range 3093 AssertRangesIntersect(Ranges, {{0x20, 0x31}}); 3094 3095 // Test range that start before second range and ends before second 3096 AssertRangesDontIntersect(Ranges, {{0x2f, 0x30}}); 3097 // Test range that start before second range and ends in second 3098 AssertRangesIntersect(Ranges, {{0x2f, 0x31}}); 3099 // Test range that start at second range 3100 AssertRangesIntersect(Ranges, {{0x30, 0x31}}); 3101 // Test range that start in second range 3102 AssertRangesIntersect(Ranges, {{0x31, 0x32}}); 3103 // Test range that start at end of second range 3104 AssertRangesIntersect(Ranges, {{0x3f, 0x40}}); 3105 // Test range that starts at end of second range 3106 AssertRangesDontIntersect(Ranges, {{0x40, 0x41}}); 3107 3108 AssertRangesDontIntersect(Ranges, {{0x20, 0x21}, {0x2f, 0x30}}); 3109 AssertRangesIntersect(Ranges, {{0x20, 0x21}, {0x2f, 0x31}}); 3110 } 3111 3112 TEST(DWARFDebugInfo, TestDWARF64UnitLength) { 3113 static const char DebugInfoSecRaw[] = 3114 "\xff\xff\xff\xff" // DWARF64 mark 3115 "\x88\x77\x66\x55\x44\x33\x22\x11" // Length 3116 "\x05\x00" // Version 3117 "\x01" // DW_UT_compile 3118 "\x04" // Address size 3119 "\0\0\0\0\0\0\0\0"; // Offset Into Abbrev. Sec. 3120 StringMap<std::unique_ptr<MemoryBuffer>> Sections; 3121 Sections.insert(std::make_pair( 3122 "debug_info", MemoryBuffer::getMemBuffer(StringRef( 3123 DebugInfoSecRaw, sizeof(DebugInfoSecRaw) - 1)))); 3124 auto Context = DWARFContext::create(Sections, /* AddrSize = */ 4, 3125 /* isLittleEndian = */ true); 3126 const auto &Obj = Context->getDWARFObj(); 3127 Obj.forEachInfoSections([&](const DWARFSection &Sec) { 3128 DWARFUnitHeader Header; 3129 DWARFDataExtractor Data(Obj, Sec, /* IsLittleEndian = */ true, 3130 /* AddressSize = */ 4); 3131 uint64_t Offset = 0; 3132 EXPECT_FALSE(Header.extract(*Context, Data, &Offset, DW_SECT_INFO)); 3133 // Header.extract() returns false because there is not enough space 3134 // in the section for the declared length. Anyway, we can check that 3135 // the properties are read correctly. 3136 ASSERT_EQ(DwarfFormat::DWARF64, Header.getFormat()); 3137 ASSERT_EQ(0x1122334455667788ULL, Header.getLength()); 3138 ASSERT_EQ(5, Header.getVersion()); 3139 ASSERT_EQ(DW_UT_compile, Header.getUnitType()); 3140 ASSERT_EQ(4, Header.getAddressByteSize()); 3141 3142 // Check that the length can be correctly read in the unit class. 3143 DWARFUnitVector DummyUnitVector; 3144 DWARFSection DummySec; 3145 DWARFCompileUnit CU(*Context, Sec, Header, /* DA = */ 0, /* RS = */ 0, 3146 /* LocSection = */ 0, /* SS = */ StringRef(), 3147 /* SOS = */ DummySec, /* AOS = */ 0, 3148 /* LS = */ DummySec, /* LE = */ true, 3149 /* isDWO= */ false, DummyUnitVector); 3150 ASSERT_EQ(0x1122334455667788ULL, CU.getLength()); 3151 }); 3152 } 3153 3154 } // end anonymous namespace 3155