2280 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "DwarfGenerator.h"10#include "DwarfUtils.h"11#include "llvm/ADT/ArrayRef.h"12#include "llvm/ADT/StringExtras.h"13#include "llvm/ADT/StringRef.h"14#include "llvm/BinaryFormat/Dwarf.h"15#include "llvm/CodeGen/AsmPrinter.h"16#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"17#include "llvm/DebugInfo/DWARF/DWARFContext.h"18#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"19#include "llvm/DebugInfo/DWARF/DWARFDie.h"20#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"21#include "llvm/DebugInfo/DWARF/DWARFVerifier.h"22#include "llvm/MC/MCContext.h"23#include "llvm/MC/MCSectionELF.h"24#include "llvm/MC/MCStreamer.h"25#include "llvm/MC/TargetRegistry.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/TargetSelect.h"31#include "llvm/TargetParser/Triple.h"32#include "llvm/Testing/Support/Error.h"33#include "gtest/gtest.h"34#include <string>35 36// AIX doesn't support debug_str_offsets or debug_addr sections37#ifdef _AIX38#define NO_SUPPORT_DEBUG_STR_OFFSETS39#define NO_SUPPORT_DEBUG_ADDR40#endif41 42using namespace llvm;43using namespace dwarf;44using namespace utils;45 46namespace {47 48template <uint16_t Version, class AddrType, class RefAddrType>49void TestAllForms() {50 Triple Triple = getDefaultTargetTripleForAddrSize(sizeof(AddrType));51 if (!isConfigurationSupported(Triple))52 GTEST_SKIP();53 54 // Test that we can decode all DW_FORM values correctly.55 const AddrType AddrValue = (AddrType)0x0123456789abcdefULL;56 const AddrType AddrxValue = (AddrType)0x4231abcd4231abcdULL;57 const AddrType Addrx1Value = (AddrType)0x0000aaaabbbbccccULL;58 const AddrType Addrx2Value = (AddrType)0xf00123f00456f000ULL;59 const AddrType Addrx3Value = (AddrType)0xABABA000B111C222ULL;60 const AddrType Addrx4Value = (AddrType)0xa1b2c3d4e5f6e5d4ULL;61 62 const uint8_t BlockData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};63 const uint32_t BlockSize = sizeof(BlockData);64 const RefAddrType RefAddr = 0x12345678;65 const uint8_t Data1 = 0x01U;66 const uint16_t Data2 = 0x2345U;67 const uint32_t Data4 = 0x6789abcdU;68 const uint64_t Data8 = 0x0011223344556677ULL;69 const uint64_t Data8_2 = 0xAABBCCDDEEFF0011ULL;70 const uint8_t Data16[16] = {1, 2, 3, 4, 5, 6, 7, 8,71 9, 10, 11, 12, 13, 14, 15, 16};72 const int64_t SData = INT64_MIN;73 const int64_t ICSData = INT64_MAX; // DW_FORM_implicit_const SData74 const uint64_t UData[] = {UINT64_MAX - 1, UINT64_MAX - 2, UINT64_MAX - 3,75 UINT64_MAX - 4, UINT64_MAX - 5, UINT64_MAX - 6,76 UINT64_MAX - 7, UINT64_MAX - 8, UINT64_MAX - 9};77#define UDATA_1 18446744073709551614ULL78 const uint32_t Dwarf32Values[] = {1, 2, 3, 4, 5, 6, 7, 8};79 const char *StringValue = "Hello";80 const char *StrpValue = "World";81 const char *StrxValue = "Indexed";82 const char *Strx1Value = "Indexed1";83 const char *Strx2Value = "Indexed2";84 const char *Strx3Value = "Indexed3";85 const char *Strx4Value = "Indexed4";86 87 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);88 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());89 dwarfgen::Generator *DG = ExpectedDG.get().get();90 dwarfgen::CompileUnit &CU = DG->addCompileUnit();91 dwarfgen::DIE CUDie = CU.getUnitDIE();92 93 if (Version >= 5) {94 CUDie.addStrOffsetsBaseAttribute();95 CUDie.addAddrBaseAttribute();96 }97 98 uint16_t Attr = DW_AT_lo_user;99 100 //----------------------------------------------------------------------101 // Test address forms102 //----------------------------------------------------------------------103 const auto Attr_DW_FORM_addr = static_cast<dwarf::Attribute>(Attr++);104 CUDie.addAttribute(Attr_DW_FORM_addr, DW_FORM_addr, AddrValue);105 106 const auto Attr_DW_FORM_addrx = static_cast<dwarf::Attribute>(Attr++);107 const auto Attr_DW_FORM_addrx1 = static_cast<dwarf::Attribute>(Attr++);108 const auto Attr_DW_FORM_addrx2 = static_cast<dwarf::Attribute>(Attr++);109 const auto Attr_DW_FORM_addrx3 = static_cast<dwarf::Attribute>(Attr++);110 const auto Attr_DW_FORM_addrx4 = static_cast<dwarf::Attribute>(Attr++);111 112 if (Version >= 5) {113 CUDie.addAttribute(Attr_DW_FORM_addrx, DW_FORM_addrx, AddrxValue);114 CUDie.addAttribute(Attr_DW_FORM_addrx1, DW_FORM_addrx1, Addrx1Value);115 CUDie.addAttribute(Attr_DW_FORM_addrx2, DW_FORM_addrx2, Addrx2Value);116 CUDie.addAttribute(Attr_DW_FORM_addrx3, DW_FORM_addrx3, Addrx3Value);117 CUDie.addAttribute(Attr_DW_FORM_addrx4, DW_FORM_addrx4, Addrx4Value);118 }119 120 //----------------------------------------------------------------------121 // Test block forms122 //----------------------------------------------------------------------123 const auto Attr_DW_FORM_block = static_cast<dwarf::Attribute>(Attr++);124 CUDie.addAttribute(Attr_DW_FORM_block, DW_FORM_block, BlockData, BlockSize);125 126 const auto Attr_DW_FORM_block1 = static_cast<dwarf::Attribute>(Attr++);127 CUDie.addAttribute(Attr_DW_FORM_block1, DW_FORM_block1, BlockData, BlockSize);128 129 const auto Attr_DW_FORM_block2 = static_cast<dwarf::Attribute>(Attr++);130 CUDie.addAttribute(Attr_DW_FORM_block2, DW_FORM_block2, BlockData, BlockSize);131 132 const auto Attr_DW_FORM_block4 = static_cast<dwarf::Attribute>(Attr++);133 CUDie.addAttribute(Attr_DW_FORM_block4, DW_FORM_block4, BlockData, BlockSize);134 135 // We handle data16 as a block form.136 const auto Attr_DW_FORM_data16 = static_cast<dwarf::Attribute>(Attr++);137 if (Version >= 5)138 CUDie.addAttribute(Attr_DW_FORM_data16, DW_FORM_data16, Data16, 16);139 140 //----------------------------------------------------------------------141 // Test data forms142 //----------------------------------------------------------------------143 const auto Attr_DW_FORM_data1 = static_cast<dwarf::Attribute>(Attr++);144 CUDie.addAttribute(Attr_DW_FORM_data1, DW_FORM_data1, Data1);145 146 const auto Attr_DW_FORM_data2 = static_cast<dwarf::Attribute>(Attr++);147 CUDie.addAttribute(Attr_DW_FORM_data2, DW_FORM_data2, Data2);148 149 const auto Attr_DW_FORM_data4 = static_cast<dwarf::Attribute>(Attr++);150 CUDie.addAttribute(Attr_DW_FORM_data4, DW_FORM_data4, Data4);151 152 const auto Attr_DW_FORM_data8 = static_cast<dwarf::Attribute>(Attr++);153 CUDie.addAttribute(Attr_DW_FORM_data8, DW_FORM_data8, Data8);154 155 //----------------------------------------------------------------------156 // Test string forms157 //----------------------------------------------------------------------158 const auto Attr_DW_FORM_string = static_cast<dwarf::Attribute>(Attr++);159 CUDie.addAttribute(Attr_DW_FORM_string, DW_FORM_string, StringValue);160 161 const auto Attr_DW_FORM_strx = static_cast<dwarf::Attribute>(Attr++);162 const auto Attr_DW_FORM_strx1 = static_cast<dwarf::Attribute>(Attr++);163 const auto Attr_DW_FORM_strx2 = static_cast<dwarf::Attribute>(Attr++);164 const auto Attr_DW_FORM_strx3 = static_cast<dwarf::Attribute>(Attr++);165 const auto Attr_DW_FORM_strx4 = static_cast<dwarf::Attribute>(Attr++);166 if (Version >= 5) {167 CUDie.addAttribute(Attr_DW_FORM_strx, DW_FORM_strx, StrxValue);168 CUDie.addAttribute(Attr_DW_FORM_strx1, DW_FORM_strx1, Strx1Value);169 CUDie.addAttribute(Attr_DW_FORM_strx2, DW_FORM_strx2, Strx2Value);170 CUDie.addAttribute(Attr_DW_FORM_strx3, DW_FORM_strx3, Strx3Value);171 CUDie.addAttribute(Attr_DW_FORM_strx4, DW_FORM_strx4, Strx4Value);172 }173 174 const auto Attr_DW_FORM_strp = static_cast<dwarf::Attribute>(Attr++);175 CUDie.addAttribute(Attr_DW_FORM_strp, DW_FORM_strp, StrpValue);176 177 //----------------------------------------------------------------------178 // Test reference forms179 //----------------------------------------------------------------------180 const auto Attr_DW_FORM_ref_addr = static_cast<dwarf::Attribute>(Attr++);181 CUDie.addAttribute(Attr_DW_FORM_ref_addr, DW_FORM_ref_addr, RefAddr);182 183 const auto Attr_DW_FORM_ref1 = static_cast<dwarf::Attribute>(Attr++);184 CUDie.addAttribute(Attr_DW_FORM_ref1, DW_FORM_ref1, Data1);185 186 const auto Attr_DW_FORM_ref2 = static_cast<dwarf::Attribute>(Attr++);187 CUDie.addAttribute(Attr_DW_FORM_ref2, DW_FORM_ref2, Data2);188 189 const auto Attr_DW_FORM_ref4 = static_cast<dwarf::Attribute>(Attr++);190 CUDie.addAttribute(Attr_DW_FORM_ref4, DW_FORM_ref4, Data4);191 192 const auto Attr_DW_FORM_ref8 = static_cast<dwarf::Attribute>(Attr++);193 CUDie.addAttribute(Attr_DW_FORM_ref8, DW_FORM_ref8, Data8);194 195 const auto Attr_DW_FORM_ref_sig8 = static_cast<dwarf::Attribute>(Attr++);196 if (Version >= 4)197 CUDie.addAttribute(Attr_DW_FORM_ref_sig8, DW_FORM_ref_sig8, Data8_2);198 199 const auto Attr_DW_FORM_ref_udata = static_cast<dwarf::Attribute>(Attr++);200 CUDie.addAttribute(Attr_DW_FORM_ref_udata, DW_FORM_ref_udata, UData[0]);201 202 //----------------------------------------------------------------------203 // Test flag forms204 //----------------------------------------------------------------------205 const auto Attr_DW_FORM_flag_true = static_cast<dwarf::Attribute>(Attr++);206 CUDie.addAttribute(Attr_DW_FORM_flag_true, DW_FORM_flag, true);207 208 const auto Attr_DW_FORM_flag_false = static_cast<dwarf::Attribute>(Attr++);209 CUDie.addAttribute(Attr_DW_FORM_flag_false, DW_FORM_flag, false);210 211 const auto Attr_DW_FORM_flag_present = static_cast<dwarf::Attribute>(Attr++);212 if (Version >= 4)213 CUDie.addAttribute(Attr_DW_FORM_flag_present, DW_FORM_flag_present);214 215 //----------------------------------------------------------------------216 // Test SLEB128 based forms217 //----------------------------------------------------------------------218 const auto Attr_DW_FORM_sdata = static_cast<dwarf::Attribute>(Attr++);219 CUDie.addAttribute(Attr_DW_FORM_sdata, DW_FORM_sdata, SData);220 221 const auto Attr_DW_FORM_implicit_const =222 static_cast<dwarf::Attribute>(Attr++);223 if (Version >= 5)224 CUDie.addAttribute(Attr_DW_FORM_implicit_const, DW_FORM_implicit_const,225 ICSData);226 227 //----------------------------------------------------------------------228 // Test ULEB128 based forms229 //----------------------------------------------------------------------230 const auto Attr_DW_FORM_udata = static_cast<dwarf::Attribute>(Attr++);231 CUDie.addAttribute(Attr_DW_FORM_udata, DW_FORM_udata, UData[0]);232 233 //----------------------------------------------------------------------234 // Test DWARF32/DWARF64 forms235 //----------------------------------------------------------------------236 const auto Attr_DW_FORM_GNU_ref_alt = static_cast<dwarf::Attribute>(Attr++);237 CUDie.addAttribute(Attr_DW_FORM_GNU_ref_alt, DW_FORM_GNU_ref_alt,238 Dwarf32Values[0]);239 240 const auto Attr_DW_FORM_sec_offset = static_cast<dwarf::Attribute>(Attr++);241 if (Version >= 4)242 CUDie.addAttribute(Attr_DW_FORM_sec_offset, DW_FORM_sec_offset,243 Dwarf32Values[1]);244 245 //----------------------------------------------------------------------246 // Add an address at the end to make sure we can decode this value247 //----------------------------------------------------------------------248 const auto Attr_Last = static_cast<dwarf::Attribute>(Attr++);249 CUDie.addAttribute(Attr_Last, DW_FORM_addr, AddrValue);250 251 //----------------------------------------------------------------------252 // Generate the DWARF253 //----------------------------------------------------------------------254 StringRef FileBytes = DG->generate();255 MemoryBufferRef FileBuffer(FileBytes, "dwarf");256 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);257 EXPECT_TRUE((bool)Obj);258 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);259 uint32_t NumCUs = DwarfContext->getNumCompileUnits();260 EXPECT_EQ(NumCUs, 1u);261 DWARFCompileUnit *U =262 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));263 auto DieDG = U->getUnitDIE(false);264 EXPECT_TRUE(DieDG.isValid());265 266 //----------------------------------------------------------------------267 // Test address forms268 //----------------------------------------------------------------------269 EXPECT_EQ(AddrValue, toAddress(DieDG.find(Attr_DW_FORM_addr), 0));270 271 if (Version >= 5) {272 auto ExtractedAddrxValue = toAddress(DieDG.find(Attr_DW_FORM_addrx));273 EXPECT_TRUE(ExtractedAddrxValue.has_value());274 EXPECT_EQ(AddrxValue, *ExtractedAddrxValue);275 276 auto ExtractedAddrx1Value = toAddress(DieDG.find(Attr_DW_FORM_addrx1));277 EXPECT_TRUE(ExtractedAddrx1Value.has_value());278 EXPECT_EQ(Addrx1Value, *ExtractedAddrx1Value);279 280 auto ExtractedAddrx2Value = toAddress(DieDG.find(Attr_DW_FORM_addrx2));281 EXPECT_TRUE(ExtractedAddrx2Value.has_value());282 EXPECT_EQ(Addrx2Value, *ExtractedAddrx2Value);283 284 auto ExtractedAddrx3Value = toAddress(DieDG.find(Attr_DW_FORM_addrx3));285 EXPECT_TRUE(ExtractedAddrx3Value.has_value());286 EXPECT_EQ(Addrx3Value, *ExtractedAddrx3Value);287 288 auto ExtractedAddrx4Value = toAddress(DieDG.find(Attr_DW_FORM_addrx4));289 EXPECT_TRUE(ExtractedAddrx1Value.has_value());290 EXPECT_EQ(Addrx4Value, *ExtractedAddrx4Value);291 }292 293 //----------------------------------------------------------------------294 // Test block forms295 //----------------------------------------------------------------------296 std::optional<DWARFFormValue> FormValue;297 ArrayRef<uint8_t> ExtractedBlockData;298 std::optional<ArrayRef<uint8_t>> BlockDataOpt;299 300 FormValue = DieDG.find(Attr_DW_FORM_block);301 EXPECT_TRUE((bool)FormValue);302 BlockDataOpt = FormValue->getAsBlock();303 EXPECT_TRUE(BlockDataOpt.has_value());304 ExtractedBlockData = *BlockDataOpt;305 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);306 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);307 308 FormValue = DieDG.find(Attr_DW_FORM_block1);309 EXPECT_TRUE((bool)FormValue);310 BlockDataOpt = FormValue->getAsBlock();311 EXPECT_TRUE(BlockDataOpt.has_value());312 ExtractedBlockData = *BlockDataOpt;313 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);314 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);315 316 FormValue = DieDG.find(Attr_DW_FORM_block2);317 EXPECT_TRUE((bool)FormValue);318 BlockDataOpt = FormValue->getAsBlock();319 EXPECT_TRUE(BlockDataOpt.has_value());320 ExtractedBlockData = *BlockDataOpt;321 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);322 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);323 324 FormValue = DieDG.find(Attr_DW_FORM_block4);325 EXPECT_TRUE((bool)FormValue);326 BlockDataOpt = FormValue->getAsBlock();327 EXPECT_TRUE(BlockDataOpt.has_value());328 ExtractedBlockData = *BlockDataOpt;329 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);330 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);331 332 // Data16 is handled like a block.333 if (Version >= 5) {334 FormValue = DieDG.find(Attr_DW_FORM_data16);335 EXPECT_TRUE((bool)FormValue);336 BlockDataOpt = FormValue->getAsBlock();337 EXPECT_TRUE(BlockDataOpt.has_value());338 ExtractedBlockData = *BlockDataOpt;339 EXPECT_EQ(ExtractedBlockData.size(), 16u);340 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), Data16, 16) == 0);341 }342 343 //----------------------------------------------------------------------344 // Test data forms345 //----------------------------------------------------------------------346 EXPECT_EQ(Data1, toUnsigned(DieDG.find(Attr_DW_FORM_data1), 0));347 EXPECT_EQ(Data2, toUnsigned(DieDG.find(Attr_DW_FORM_data2), 0));348 EXPECT_EQ(Data4, toUnsigned(DieDG.find(Attr_DW_FORM_data4), 0));349 EXPECT_EQ(Data8, toUnsigned(DieDG.find(Attr_DW_FORM_data8), 0));350 351 //----------------------------------------------------------------------352 // Test string forms353 //----------------------------------------------------------------------354 auto ExtractedStringValue = toString(DieDG.find(Attr_DW_FORM_string));355 EXPECT_TRUE((bool)ExtractedStringValue);356 EXPECT_STREQ(StringValue, *ExtractedStringValue);357 358 if (Version >= 5) {359 auto ExtractedStrxValue = toString(DieDG.find(Attr_DW_FORM_strx));360 EXPECT_TRUE((bool)ExtractedStrxValue);361 EXPECT_STREQ(StrxValue, *ExtractedStrxValue);362 363 auto ExtractedStrx1Value = toString(DieDG.find(Attr_DW_FORM_strx1));364 EXPECT_TRUE((bool)ExtractedStrx1Value);365 EXPECT_STREQ(Strx1Value, *ExtractedStrx1Value);366 367 auto ExtractedStrx2Value = toString(DieDG.find(Attr_DW_FORM_strx2));368 EXPECT_TRUE((bool)ExtractedStrx2Value);369 EXPECT_STREQ(Strx2Value, *ExtractedStrx2Value);370 371 auto ExtractedStrx3Value = toString(DieDG.find(Attr_DW_FORM_strx3));372 EXPECT_TRUE((bool)ExtractedStrx3Value);373 EXPECT_STREQ(Strx3Value, *ExtractedStrx3Value);374 375 auto ExtractedStrx4Value = toString(DieDG.find(Attr_DW_FORM_strx4));376 EXPECT_TRUE((bool)ExtractedStrx4Value);377 EXPECT_STREQ(Strx4Value, *ExtractedStrx4Value);378 }379 380 auto ExtractedStrpValue = toString(DieDG.find(Attr_DW_FORM_strp));381 EXPECT_TRUE((bool)ExtractedStrpValue);382 EXPECT_STREQ(StrpValue, *ExtractedStrpValue);383 384 //----------------------------------------------------------------------385 // Test reference forms386 //----------------------------------------------------------------------387 EXPECT_EQ(RefAddr,388 toDebugInfoReference(DieDG.find(Attr_DW_FORM_ref_addr), 0));389 EXPECT_EQ(Data1, toRelativeReference(DieDG.find(Attr_DW_FORM_ref1), 0));390 EXPECT_EQ(Data2, toRelativeReference(DieDG.find(Attr_DW_FORM_ref2), 0));391 EXPECT_EQ(Data4, toRelativeReference(DieDG.find(Attr_DW_FORM_ref4), 0));392 EXPECT_EQ(Data8, toRelativeReference(DieDG.find(Attr_DW_FORM_ref8), 0));393 if (Version >= 4) {394 EXPECT_EQ(Data8_2,395 toSignatureReference(DieDG.find(Attr_DW_FORM_ref_sig8), 0));396 }397 EXPECT_EQ(UData[0],398 toRelativeReference(DieDG.find(Attr_DW_FORM_ref_udata), 0));399 400 //----------------------------------------------------------------------401 // Test flag forms402 //----------------------------------------------------------------------403 EXPECT_EQ(1ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_true), 0));404 EXPECT_EQ(0ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_false), 1));405 if (Version >= 4) {406 EXPECT_EQ(1ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_present), 0));407 }408 409 //----------------------------------------------------------------------410 // Test SLEB128 based forms411 //----------------------------------------------------------------------412 EXPECT_EQ(SData, toSigned(DieDG.find(Attr_DW_FORM_sdata), 0));413 if (Version >= 5) {414 EXPECT_EQ(ICSData, toSigned(DieDG.find(Attr_DW_FORM_implicit_const), 0));415 }416 417 //----------------------------------------------------------------------418 // Test ULEB128 based forms419 //----------------------------------------------------------------------420 EXPECT_EQ(UData[0], toUnsigned(DieDG.find(Attr_DW_FORM_udata), 0));421 422 //----------------------------------------------------------------------423 // Test DWARF32/DWARF64 forms424 //----------------------------------------------------------------------425 EXPECT_EQ(Dwarf32Values[0],426 toSupplementaryReference(DieDG.find(Attr_DW_FORM_GNU_ref_alt), 0));427 if (Version >= 4) {428 EXPECT_EQ(Dwarf32Values[1],429 toSectionOffset(DieDG.find(Attr_DW_FORM_sec_offset), 0));430 }431 432 //----------------------------------------------------------------------433 // Add an address at the end to make sure we can decode this value434 //----------------------------------------------------------------------435 EXPECT_EQ(AddrValue, toAddress(DieDG.find(Attr_Last), 0));436}437 438TEST(DWARFDebugInfo, TestDWARF32Version2Addr4AllForms) {439 // Test that we can decode all forms for DWARF32, version 2, with 4 byte440 // addresses.441 typedef uint32_t AddrType;442 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2.443 typedef AddrType RefAddrType;444 TestAllForms<2, AddrType, RefAddrType>();445}446 447TEST(DWARFDebugInfo, TestDWARF32Version2Addr8AllForms) {448 // Test that we can decode all forms for DWARF32, version 2, with 4 byte449 // addresses.450 typedef uint64_t AddrType;451 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2.452 typedef AddrType RefAddrType;453 TestAllForms<2, AddrType, RefAddrType>();454}455 456TEST(DWARFDebugInfo, TestDWARF32Version3Addr4AllForms) {457 // Test that we can decode all forms for DWARF32, version 3, with 4 byte458 // addresses.459 typedef uint32_t AddrType;460 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later.461 typedef uint32_t RefAddrType;462 TestAllForms<3, AddrType, RefAddrType>();463}464 465TEST(DWARFDebugInfo, TestDWARF32Version3Addr8AllForms) {466 // Test that we can decode all forms for DWARF32, version 3, with 8 byte467 // addresses.468 typedef uint64_t AddrType;469 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later470 typedef uint32_t RefAddrType;471 TestAllForms<3, AddrType, RefAddrType>();472}473 474TEST(DWARFDebugInfo, TestDWARF32Version4Addr4AllForms) {475 // Test that we can decode all forms for DWARF32, version 4, with 4 byte476 // addresses.477 typedef uint32_t AddrType;478 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later479 typedef uint32_t RefAddrType;480 TestAllForms<4, AddrType, RefAddrType>();481}482 483TEST(DWARFDebugInfo, TestDWARF32Version4Addr8AllForms) {484 // Test that we can decode all forms for DWARF32, version 4, with 8 byte485 // addresses.486 typedef uint64_t AddrType;487 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later488 typedef uint32_t RefAddrType;489 TestAllForms<4, AddrType, RefAddrType>();490}491 492#ifdef NO_SUPPORT_DEBUG_STR_OFFSETS493TEST(DWARFDebugInfo, DISABLED_TestDWARF32Version5Addr4AllForms) {494#else495TEST(DWARFDebugInfo, TestDWARF32Version5Addr4AllForms) {496#endif497 // Test that we can decode all forms for DWARF32, version 5, with 4 byte498 // addresses.499 typedef uint32_t AddrType;500 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later501 typedef uint32_t RefAddrType;502 TestAllForms<5, AddrType, RefAddrType>();503}504 505#ifdef NO_SUPPORT_DEBUG_STR_OFFSETS506TEST(DWARFDebugInfo, DISABLED_TestDWARF32Version5Addr8AllForms) {507#else508TEST(DWARFDebugInfo, TestDWARF32Version5Addr8AllForms) {509#endif510 // Test that we can decode all forms for DWARF32, version 5, with 8 byte511 // addresses.512 typedef uint64_t AddrType;513 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later514 typedef uint32_t RefAddrType;515 TestAllForms<5, AddrType, RefAddrType>();516}517 518template <uint16_t Version, class AddrType> void TestChildren() {519 Triple Triple = getDefaultTargetTripleForAddrSize(sizeof(AddrType));520 if (!isConfigurationSupported(Triple))521 GTEST_SKIP();522 523 // Test that we can decode DW_FORM_ref_addr values correctly in DWARF 2 with524 // 4 byte addresses. DW_FORM_ref_addr values should be 4 bytes when using525 // 8 byte addresses.526 527 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);528 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());529 dwarfgen::Generator *DG = ExpectedDG.get().get();530 dwarfgen::CompileUnit &CU = DG->addCompileUnit();531 dwarfgen::DIE CUDie = CU.getUnitDIE();532 533 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");534 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);535 536 dwarfgen::DIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram);537 SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main");538 SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U);539 SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U);540 541 dwarfgen::DIE IntDie = CUDie.addChild(DW_TAG_base_type);542 IntDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");543 IntDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);544 IntDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);545 546 dwarfgen::DIE ArgcDie = SubprogramDie.addChild(DW_TAG_formal_parameter);547 ArgcDie.addAttribute(DW_AT_name, DW_FORM_strp, "argc");548 // ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref4, IntDie);549 ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, IntDie);550 551 StringRef FileBytes = DG->generate();552 MemoryBufferRef FileBuffer(FileBytes, "dwarf");553 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);554 EXPECT_TRUE((bool)Obj);555 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);556 557 // Verify the number of compile units is correct.558 uint32_t NumCUs = DwarfContext->getNumCompileUnits();559 EXPECT_EQ(NumCUs, 1u);560 DWARFCompileUnit *U =561 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));562 563 // Get the compile unit DIE is valid.564 auto DieDG = U->getUnitDIE(false);565 EXPECT_TRUE(DieDG.isValid());566 567 // Verify the first child of the compile unit DIE is our subprogram.568 auto SubprogramDieDG = DieDG.getFirstChild();569 EXPECT_TRUE(SubprogramDieDG.isValid());570 EXPECT_EQ(SubprogramDieDG.getTag(), DW_TAG_subprogram);571 572 // Verify the first child of the subprogram is our formal parameter.573 auto ArgcDieDG = SubprogramDieDG.getFirstChild();574 EXPECT_TRUE(ArgcDieDG.isValid());575 EXPECT_EQ(ArgcDieDG.getTag(), DW_TAG_formal_parameter);576 577 // Verify our formal parameter has a NULL tag sibling.578 auto NullDieDG = ArgcDieDG.getSibling();579 EXPECT_TRUE(NullDieDG.isValid());580 if (NullDieDG) {581 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null);582 EXPECT_TRUE(!NullDieDG.getSibling().isValid());583 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid());584 }585 586 // Verify the sibling of our subprogram is our integer base type.587 auto IntDieDG = SubprogramDieDG.getSibling();588 EXPECT_TRUE(IntDieDG.isValid());589 EXPECT_EQ(IntDieDG.getTag(), DW_TAG_base_type);590 591 // Verify the sibling of our subprogram is our integer base is a NULL tag.592 NullDieDG = IntDieDG.getSibling();593 EXPECT_TRUE(NullDieDG.isValid());594 if (NullDieDG) {595 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null);596 EXPECT_TRUE(!NullDieDG.getSibling().isValid());597 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid());598 }599 600 // Verify the previous sibling of our subprogram is our integer base type.601 IntDieDG = NullDieDG.getPreviousSibling();602 EXPECT_TRUE(IntDieDG.isValid());603 EXPECT_EQ(IntDieDG.getTag(), DW_TAG_base_type);604}605 606TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Children) {607 // Test that we can decode all forms for DWARF32, version 2, with 4 byte608 // addresses.609 typedef uint32_t AddrType;610 TestChildren<2, AddrType>();611}612 613TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Children) {614 // Test that we can decode all forms for DWARF32, version 2, with 8 byte615 // addresses.616 typedef uint64_t AddrType;617 TestChildren<2, AddrType>();618}619 620TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Children) {621 // Test that we can decode all forms for DWARF32, version 3, with 4 byte622 // addresses.623 typedef uint32_t AddrType;624 TestChildren<3, AddrType>();625}626 627TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Children) {628 // Test that we can decode all forms for DWARF32, version 3, with 8 byte629 // addresses.630 typedef uint64_t AddrType;631 TestChildren<3, AddrType>();632}633 634TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Children) {635 // Test that we can decode all forms for DWARF32, version 4, with 4 byte636 // addresses.637 typedef uint32_t AddrType;638 TestChildren<4, AddrType>();639}640 641TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Children) {642 // Test that we can decode all forms for DWARF32, version 4, with 8 byte643 // addresses.644 typedef uint64_t AddrType;645 TestChildren<4, AddrType>();646}647 648template <uint16_t Version, class AddrType> void TestReferences() {649 Triple Triple = getDefaultTargetTripleForAddrSize(sizeof(AddrType));650 if (!isConfigurationSupported(Triple))651 GTEST_SKIP();652 653 // Test that we can decode DW_FORM_refXXX values correctly in DWARF.654 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);655 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());656 dwarfgen::Generator *DG = ExpectedDG.get().get();657 dwarfgen::CompileUnit &CU1 = DG->addCompileUnit();658 dwarfgen::CompileUnit &CU2 = DG->addCompileUnit();659 660 dwarfgen::DIE CU1Die = CU1.getUnitDIE();661 CU1Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");662 CU1Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);663 664 dwarfgen::DIE CU1TypeDie = CU1Die.addChild(DW_TAG_base_type);665 CU1TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");666 CU1TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);667 CU1TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);668 669 dwarfgen::DIE CU1Ref1Die = CU1Die.addChild(DW_TAG_variable);670 CU1Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref1");671 CU1Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU1TypeDie);672 673 dwarfgen::DIE CU1Ref2Die = CU1Die.addChild(DW_TAG_variable);674 CU1Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref2");675 CU1Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU1TypeDie);676 677 dwarfgen::DIE CU1Ref4Die = CU1Die.addChild(DW_TAG_variable);678 CU1Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref4");679 CU1Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU1TypeDie);680 681 dwarfgen::DIE CU1Ref8Die = CU1Die.addChild(DW_TAG_variable);682 CU1Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref8");683 CU1Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU1TypeDie);684 685 dwarfgen::DIE CU1RefAddrDie = CU1Die.addChild(DW_TAG_variable);686 CU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1RefAddr");687 CU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie);688 689 dwarfgen::DIE CU2Die = CU2.getUnitDIE();690 CU2Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/foo.c");691 CU2Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);692 693 dwarfgen::DIE CU2TypeDie = CU2Die.addChild(DW_TAG_base_type);694 CU2TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "float");695 CU2TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_float);696 CU2TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);697 698 dwarfgen::DIE CU2Ref1Die = CU2Die.addChild(DW_TAG_variable);699 CU2Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref1");700 CU2Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU2TypeDie);701 702 dwarfgen::DIE CU2Ref2Die = CU2Die.addChild(DW_TAG_variable);703 CU2Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref2");704 CU2Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU2TypeDie);705 706 dwarfgen::DIE CU2Ref4Die = CU2Die.addChild(DW_TAG_variable);707 CU2Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref4");708 CU2Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU2TypeDie);709 710 dwarfgen::DIE CU2Ref8Die = CU2Die.addChild(DW_TAG_variable);711 CU2Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref8");712 CU2Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU2TypeDie);713 714 dwarfgen::DIE CU2RefAddrDie = CU2Die.addChild(DW_TAG_variable);715 CU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2RefAddr");716 CU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie);717 718 // Refer to a type in CU1 from CU2719 dwarfgen::DIE CU2ToCU1RefAddrDie = CU2Die.addChild(DW_TAG_variable);720 CU2ToCU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2ToCU1RefAddr");721 CU2ToCU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie);722 723 // Refer to a type in CU2 from CU1724 dwarfgen::DIE CU1ToCU2RefAddrDie = CU1Die.addChild(DW_TAG_variable);725 CU1ToCU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1ToCU2RefAddr");726 CU1ToCU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie);727 728 StringRef FileBytes = DG->generate();729 MemoryBufferRef FileBuffer(FileBytes, "dwarf");730 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);731 EXPECT_TRUE((bool)Obj);732 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);733 734 // Verify the number of compile units is correct.735 uint32_t NumCUs = DwarfContext->getNumCompileUnits();736 EXPECT_EQ(NumCUs, 2u);737 DWARFCompileUnit *U1 =738 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));739 DWARFCompileUnit *U2 =740 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(1));741 742 // Get the compile unit DIE is valid.743 auto Unit1DieDG = U1->getUnitDIE(false);744 EXPECT_TRUE(Unit1DieDG.isValid());745 746 auto Unit2DieDG = U2->getUnitDIE(false);747 EXPECT_TRUE(Unit2DieDG.isValid());748 749 // Verify the first child of the compile unit 1 DIE is our int base type.750 auto CU1TypeDieDG = Unit1DieDG.getFirstChild();751 EXPECT_TRUE(CU1TypeDieDG.isValid());752 EXPECT_EQ(CU1TypeDieDG.getTag(), DW_TAG_base_type);753 EXPECT_EQ(DW_ATE_signed, toUnsigned(CU1TypeDieDG.find(DW_AT_encoding), 0));754 755 // Verify the first child of the compile unit 2 DIE is our float base type.756 auto CU2TypeDieDG = Unit2DieDG.getFirstChild();757 EXPECT_TRUE(CU2TypeDieDG.isValid());758 EXPECT_EQ(CU2TypeDieDG.getTag(), DW_TAG_base_type);759 EXPECT_EQ(DW_ATE_float, toUnsigned(CU2TypeDieDG.find(DW_AT_encoding), 0));760 761 // Verify the sibling of the base type DIE is our Ref1 DIE and that its762 // DW_AT_type points to our base type DIE.763 auto CU1Ref1DieDG = CU1TypeDieDG.getSibling();764 EXPECT_TRUE(CU1Ref1DieDG.isValid());765 EXPECT_EQ(CU1Ref1DieDG.getTag(), DW_TAG_variable);766 EXPECT_EQ(CU1TypeDieDG.getOffset(),767 toRelativeReference(CU1Ref1DieDG.find(DW_AT_type), -1ULL));768 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our769 // base type DIE in CU1.770 auto CU1Ref2DieDG = CU1Ref1DieDG.getSibling();771 EXPECT_TRUE(CU1Ref2DieDG.isValid());772 EXPECT_EQ(CU1Ref2DieDG.getTag(), DW_TAG_variable);773 EXPECT_EQ(CU1TypeDieDG.getOffset(),774 toRelativeReference(CU1Ref2DieDG.find(DW_AT_type), -1ULL));775 776 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our777 // base type DIE in CU1.778 auto CU1Ref4DieDG = CU1Ref2DieDG.getSibling();779 EXPECT_TRUE(CU1Ref4DieDG.isValid());780 EXPECT_EQ(CU1Ref4DieDG.getTag(), DW_TAG_variable);781 EXPECT_EQ(CU1TypeDieDG.getOffset(),782 toRelativeReference(CU1Ref4DieDG.find(DW_AT_type), -1ULL));783 784 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our785 // base type DIE in CU1.786 auto CU1Ref8DieDG = CU1Ref4DieDG.getSibling();787 EXPECT_TRUE(CU1Ref8DieDG.isValid());788 EXPECT_EQ(CU1Ref8DieDG.getTag(), DW_TAG_variable);789 EXPECT_EQ(CU1TypeDieDG.getOffset(),790 toRelativeReference(CU1Ref8DieDG.find(DW_AT_type), -1ULL));791 792 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our793 // base type DIE in CU1.794 auto CU1RefAddrDieDG = CU1Ref8DieDG.getSibling();795 EXPECT_TRUE(CU1RefAddrDieDG.isValid());796 EXPECT_EQ(CU1RefAddrDieDG.getTag(), DW_TAG_variable);797 EXPECT_EQ(CU1TypeDieDG.getOffset(),798 toDebugInfoReference(CU1RefAddrDieDG.find(DW_AT_type), -1ULL));799 800 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its801 // DW_AT_type points to our base type DIE.802 auto CU1ToCU2RefAddrDieDG = CU1RefAddrDieDG.getSibling();803 EXPECT_TRUE(CU1ToCU2RefAddrDieDG.isValid());804 EXPECT_EQ(CU1ToCU2RefAddrDieDG.getTag(), DW_TAG_variable);805 EXPECT_EQ(CU2TypeDieDG.getOffset(),806 toDebugInfoReference(CU1ToCU2RefAddrDieDG.find(DW_AT_type), -1ULL));807 808 // Verify the sibling of the base type DIE is our Ref1 DIE and that its809 // DW_AT_type points to our base type DIE.810 auto CU2Ref1DieDG = CU2TypeDieDG.getSibling();811 EXPECT_TRUE(CU2Ref1DieDG.isValid());812 EXPECT_EQ(CU2Ref1DieDG.getTag(), DW_TAG_variable);813 EXPECT_EQ(CU2TypeDieDG.getOffset() - CU2TypeDieDG.getDwarfUnit()->getOffset(),814 toRelativeReference(CU2Ref1DieDG.find(DW_AT_type), -1ULL));815 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our816 // base type DIE in CU2.817 auto CU2Ref2DieDG = CU2Ref1DieDG.getSibling();818 EXPECT_TRUE(CU2Ref2DieDG.isValid());819 EXPECT_EQ(CU2Ref2DieDG.getTag(), DW_TAG_variable);820 EXPECT_EQ(CU2TypeDieDG.getOffset() - CU2TypeDieDG.getDwarfUnit()->getOffset(),821 toRelativeReference(CU2Ref2DieDG.find(DW_AT_type), -1ULL));822 823 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our824 // base type DIE in CU2.825 auto CU2Ref4DieDG = CU2Ref2DieDG.getSibling();826 EXPECT_TRUE(CU2Ref4DieDG.isValid());827 EXPECT_EQ(CU2Ref4DieDG.getTag(), DW_TAG_variable);828 EXPECT_EQ(CU2TypeDieDG.getOffset() - CU2TypeDieDG.getDwarfUnit()->getOffset(),829 toRelativeReference(CU2Ref4DieDG.find(DW_AT_type), -1ULL));830 831 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our832 // base type DIE in CU2.833 auto CU2Ref8DieDG = CU2Ref4DieDG.getSibling();834 EXPECT_TRUE(CU2Ref8DieDG.isValid());835 EXPECT_EQ(CU2Ref8DieDG.getTag(), DW_TAG_variable);836 EXPECT_EQ(CU2TypeDieDG.getOffset() - CU2TypeDieDG.getDwarfUnit()->getOffset(),837 toRelativeReference(CU2Ref8DieDG.find(DW_AT_type), -1ULL));838 839 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our840 // base type DIE in CU2.841 auto CU2RefAddrDieDG = CU2Ref8DieDG.getSibling();842 EXPECT_TRUE(CU2RefAddrDieDG.isValid());843 EXPECT_EQ(CU2RefAddrDieDG.getTag(), DW_TAG_variable);844 EXPECT_EQ(CU2TypeDieDG.getOffset(),845 toDebugInfoReference(CU2RefAddrDieDG.find(DW_AT_type), -1ULL));846 847 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its848 // DW_AT_type points to our base type DIE.849 auto CU2ToCU1RefAddrDieDG = CU2RefAddrDieDG.getSibling();850 EXPECT_TRUE(CU2ToCU1RefAddrDieDG.isValid());851 EXPECT_EQ(CU2ToCU1RefAddrDieDG.getTag(), DW_TAG_variable);852 EXPECT_EQ(CU1TypeDieDG.getOffset(),853 toDebugInfoReference(CU2ToCU1RefAddrDieDG.find(DW_AT_type), -1ULL));854}855 856TEST(DWARFDebugInfo, TestDWARF32Version2Addr4References) {857 // Test that we can decode all forms for DWARF32, version 2, with 4 byte858 // addresses.859 typedef uint32_t AddrType;860 TestReferences<2, AddrType>();861}862 863TEST(DWARFDebugInfo, TestDWARF32Version2Addr8References) {864 // Test that we can decode all forms for DWARF32, version 2, with 8 byte865 // addresses.866 typedef uint64_t AddrType;867 TestReferences<2, AddrType>();868}869 870TEST(DWARFDebugInfo, TestDWARF32Version3Addr4References) {871 // Test that we can decode all forms for DWARF32, version 3, with 4 byte872 // addresses.873 typedef uint32_t AddrType;874 TestReferences<3, AddrType>();875}876 877TEST(DWARFDebugInfo, TestDWARF32Version3Addr8References) {878 // Test that we can decode all forms for DWARF32, version 3, with 8 byte879 // addresses.880 typedef uint64_t AddrType;881 TestReferences<3, AddrType>();882}883 884TEST(DWARFDebugInfo, TestDWARF32Version4Addr4References) {885 // Test that we can decode all forms for DWARF32, version 4, with 4 byte886 // addresses.887 typedef uint32_t AddrType;888 TestReferences<4, AddrType>();889}890 891TEST(DWARFDebugInfo, TestDWARF32Version4Addr8References) {892 // Test that we can decode all forms for DWARF32, version 4, with 8 byte893 // addresses.894 typedef uint64_t AddrType;895 TestReferences<4, AddrType>();896}897 898template <uint16_t Version, class AddrType> void TestAddresses() {899 Triple Triple = getDefaultTargetTripleForAddrSize(sizeof(AddrType));900 if (!isConfigurationSupported(Triple))901 GTEST_SKIP();902 903 // Test the DWARF APIs related to accessing the DW_AT_low_pc and904 // DW_AT_high_pc.905 const bool SupportsHighPCAsOffset = Version >= 4;906 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);907 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());908 dwarfgen::Generator *DG = ExpectedDG.get().get();909 dwarfgen::CompileUnit &CU = DG->addCompileUnit();910 dwarfgen::DIE CUDie = CU.getUnitDIE();911 912 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");913 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);914 915 // Create a subprogram DIE with no low or high PC.916 dwarfgen::DIE SubprogramNoPC = CUDie.addChild(DW_TAG_subprogram);917 SubprogramNoPC.addAttribute(DW_AT_name, DW_FORM_strp, "no_pc");918 919 // Create a subprogram DIE with a low PC only.920 dwarfgen::DIE SubprogramLowPC = CUDie.addChild(DW_TAG_subprogram);921 SubprogramLowPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_pc");922 const uint64_t ActualLowPC = 0x1000;923 const uint64_t ActualHighPC = 0x2000;924 const uint64_t ActualHighPCOffset = ActualHighPC - ActualLowPC;925 SubprogramLowPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC);926 927 // Create a subprogram DIE with a low and high PC.928 dwarfgen::DIE SubprogramLowHighPC = CUDie.addChild(DW_TAG_subprogram);929 SubprogramLowHighPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_high_pc");930 SubprogramLowHighPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC);931 // Encode the high PC as an offset from the low PC if supported.932 if (SupportsHighPCAsOffset)933 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_data4,934 ActualHighPCOffset);935 else936 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_addr, ActualHighPC);937 938 StringRef FileBytes = DG->generate();939 MemoryBufferRef FileBuffer(FileBytes, "dwarf");940 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);941 EXPECT_TRUE((bool)Obj);942 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);943 944 // Verify the number of compile units is correct.945 uint32_t NumCUs = DwarfContext->getNumCompileUnits();946 EXPECT_EQ(NumCUs, 1u);947 DWARFCompileUnit *U =948 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));949 950 // Get the compile unit DIE is valid.951 auto DieDG = U->getUnitDIE(false);952 EXPECT_TRUE(DieDG.isValid());953 954 uint64_t LowPC, HighPC, SectionIndex;955 std::optional<uint64_t> OptU64;956 // Verify the that our subprogram with no PC value fails appropriately when957 // asked for any PC values.958 auto SubprogramDieNoPC = DieDG.getFirstChild();959 EXPECT_TRUE(SubprogramDieNoPC.isValid());960 EXPECT_EQ(SubprogramDieNoPC.getTag(), DW_TAG_subprogram);961 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_low_pc));962 EXPECT_FALSE((bool)OptU64);963 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_high_pc));964 EXPECT_FALSE((bool)OptU64);965 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC, SectionIndex));966 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_high_pc));967 EXPECT_FALSE((bool)OptU64);968 OptU64 = toUnsigned(SubprogramDieNoPC.find(DW_AT_high_pc));969 EXPECT_FALSE((bool)OptU64);970 OptU64 = SubprogramDieNoPC.getHighPC(ActualLowPC);971 EXPECT_FALSE((bool)OptU64);972 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC, SectionIndex));973 974 // Verify the that our subprogram with only a low PC value succeeds when975 // we ask for the Low PC, but fails appropriately when asked for the high PC976 // or both low and high PC values.977 auto SubprogramDieLowPC = SubprogramDieNoPC.getSibling();978 EXPECT_TRUE(SubprogramDieLowPC.isValid());979 EXPECT_EQ(SubprogramDieLowPC.getTag(), DW_TAG_subprogram);980 OptU64 = toAddress(SubprogramDieLowPC.find(DW_AT_low_pc));981 EXPECT_TRUE((bool)OptU64);982 EXPECT_EQ(*OptU64, ActualLowPC);983 OptU64 = toAddress(SubprogramDieLowPC.find(DW_AT_high_pc));984 EXPECT_FALSE((bool)OptU64);985 OptU64 = toUnsigned(SubprogramDieLowPC.find(DW_AT_high_pc));986 EXPECT_FALSE((bool)OptU64);987 OptU64 = SubprogramDieLowPC.getHighPC(ActualLowPC);988 EXPECT_FALSE((bool)OptU64);989 EXPECT_FALSE(SubprogramDieLowPC.getLowAndHighPC(LowPC, HighPC, SectionIndex));990 991 // Verify the that our subprogram with only a low PC value succeeds when992 // we ask for the Low PC, but fails appropriately when asked for the high PC993 // or both low and high PC values.994 auto SubprogramDieLowHighPC = SubprogramDieLowPC.getSibling();995 EXPECT_TRUE(SubprogramDieLowHighPC.isValid());996 EXPECT_EQ(SubprogramDieLowHighPC.getTag(), DW_TAG_subprogram);997 OptU64 = toAddress(SubprogramDieLowHighPC.find(DW_AT_low_pc));998 EXPECT_TRUE((bool)OptU64);999 EXPECT_EQ(*OptU64, ActualLowPC);1000 // Get the high PC as an address. This should succeed if the high PC was1001 // encoded as an address and fail if the high PC was encoded as an offset.1002 OptU64 = toAddress(SubprogramDieLowHighPC.find(DW_AT_high_pc));1003 if (SupportsHighPCAsOffset) {1004 EXPECT_FALSE((bool)OptU64);1005 } else {1006 EXPECT_TRUE((bool)OptU64);1007 EXPECT_EQ(*OptU64, ActualHighPC);1008 }1009 // Get the high PC as an unsigned constant. This should succeed if the high PC1010 // was encoded as an offset and fail if the high PC was encoded as an address.1011 OptU64 = toUnsigned(SubprogramDieLowHighPC.find(DW_AT_high_pc));1012 if (SupportsHighPCAsOffset) {1013 EXPECT_TRUE((bool)OptU64);1014 EXPECT_EQ(*OptU64, ActualHighPCOffset);1015 } else {1016 EXPECT_FALSE((bool)OptU64);1017 }1018 1019 OptU64 = SubprogramDieLowHighPC.getHighPC(ActualLowPC);1020 EXPECT_TRUE((bool)OptU64);1021 EXPECT_EQ(*OptU64, ActualHighPC);1022 1023 EXPECT_TRUE(SubprogramDieLowHighPC.getLowAndHighPC(LowPC, HighPC, SectionIndex));1024 EXPECT_EQ(LowPC, ActualLowPC);1025 EXPECT_EQ(HighPC, ActualHighPC);1026}1027 1028TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Addresses) {1029 // Test that we can decode address values in DWARF32, version 2, with 4 byte1030 // addresses.1031 typedef uint32_t AddrType;1032 TestAddresses<2, AddrType>();1033}1034 1035TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Addresses) {1036 // Test that we can decode address values in DWARF32, version 2, with 8 byte1037 // addresses.1038 typedef uint64_t AddrType;1039 TestAddresses<2, AddrType>();1040}1041 1042TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Addresses) {1043 // Test that we can decode address values in DWARF32, version 3, with 4 byte1044 // addresses.1045 typedef uint32_t AddrType;1046 TestAddresses<3, AddrType>();1047}1048 1049TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Addresses) {1050 // Test that we can decode address values in DWARF32, version 3, with 8 byte1051 // addresses.1052 typedef uint64_t AddrType;1053 TestAddresses<3, AddrType>();1054}1055 1056TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Addresses) {1057 // Test that we can decode address values in DWARF32, version 4, with 4 byte1058 // addresses.1059 typedef uint32_t AddrType;1060 TestAddresses<4, AddrType>();1061}1062 1063TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Addresses) {1064 // Test that we can decode address values in DWARF32, version 4, with 8 byte1065 // addresses.1066 typedef uint64_t AddrType;1067 TestAddresses<4, AddrType>();1068}1069 1070#ifdef NO_SUPPORT_DEBUG_STR_OFFSETS1071TEST(DWARFDebugInfo, DISABLED_TestStringOffsets) {1072#else1073TEST(DWARFDebugInfo, TestStringOffsets) {1074#endif1075 Triple Triple = getNormalizedDefaultTargetTriple();1076 if (!isConfigurationSupported(Triple))1077 GTEST_SKIP();1078 1079 const char *String1 = "Hello";1080 const char *String2 = "World";1081 1082 auto ExpectedDG = dwarfgen::Generator::create(Triple, 5);1083 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());1084 dwarfgen::Generator *DG = ExpectedDG.get().get();1085 dwarfgen::CompileUnit &CU = DG->addCompileUnit();1086 dwarfgen::DIE CUDie = CU.getUnitDIE();1087 1088 CUDie.addStrOffsetsBaseAttribute();1089 1090 uint16_t Attr = DW_AT_lo_user;1091 1092 // Create our strings. First we create a non-indexed reference to String1,1093 // followed by an indexed String2. Finally, we add an indexed reference to1094 // String1.1095 const auto Attr1 = static_cast<dwarf::Attribute>(Attr++);1096 CUDie.addAttribute(Attr1, DW_FORM_strp, String1);1097 1098 const auto Attr2 = static_cast<dwarf::Attribute>(Attr++);1099 CUDie.addAttribute(Attr2, DW_FORM_strx, String2);1100 1101 const auto Attr3 = static_cast<dwarf::Attribute>(Attr++);1102 CUDie.addAttribute(Attr3, DW_FORM_strx, String1);1103 1104 // Generate the DWARF1105 StringRef FileBytes = DG->generate();1106 MemoryBufferRef FileBuffer(FileBytes, "dwarf");1107 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);1108 ASSERT_TRUE((bool)Obj);1109 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);1110 uint32_t NumCUs = DwarfContext->getNumCompileUnits();1111 ASSERT_EQ(NumCUs, 1u);1112 DWARFUnit *U = DwarfContext->getUnitAtIndex(0);1113 auto DieDG = U->getUnitDIE(false);1114 ASSERT_TRUE(DieDG.isValid());1115 ASSERT_TRUE(U->isLittleEndian() == Triple.isLittleEndian());1116 1117 // Now make sure the string offsets came out properly. Attr2 should have index1118 // 0 (because it was the first indexed string) even though the string itself1119 // was added eariler.1120 auto Extracted1 = toString(DieDG.find(Attr1));1121 ASSERT_TRUE((bool)Extracted1);1122 EXPECT_STREQ(String1, *Extracted1);1123 1124 std::optional<DWARFFormValue> Form2 = DieDG.find(Attr2);1125 ASSERT_TRUE((bool)Form2);1126 EXPECT_EQ(0u, Form2->getRawUValue());1127 auto Extracted2 = toString(Form2);1128 ASSERT_TRUE((bool)Extracted2);1129 EXPECT_STREQ(String2, *Extracted2);1130 1131 std::optional<DWARFFormValue> Form3 = DieDG.find(Attr3);1132 ASSERT_TRUE((bool)Form3);1133 EXPECT_EQ(1u, Form3->getRawUValue());1134 auto Extracted3 = toString(Form3);1135 ASSERT_TRUE((bool)Extracted3);1136 EXPECT_STREQ(String1, *Extracted3);1137}1138 1139#ifdef NO_SUPPORT_DEBUG_ADDR1140TEST(DWARFDebugInfo, DISABLED_TestEmptyStringOffsets) {1141#else1142TEST(DWARFDebugInfo, TestEmptyStringOffsets) {1143#endif1144 1145 Triple Triple = getNormalizedDefaultTargetTriple();1146 if (!isConfigurationSupported(Triple))1147 GTEST_SKIP();1148 1149 const char *String1 = "Hello";1150 1151 auto ExpectedDG = dwarfgen::Generator::create(Triple, 5);1152 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());1153 dwarfgen::Generator *DG = ExpectedDG.get().get();1154 dwarfgen::CompileUnit &CU = DG->addCompileUnit();1155 dwarfgen::DIE CUDie = CU.getUnitDIE();1156 1157 uint16_t Attr = DW_AT_lo_user;1158 1159 // We shall insert only one string. It will be referenced directly.1160 const auto Attr1 = static_cast<dwarf::Attribute>(Attr++);1161 CUDie.addAttribute(Attr1, DW_FORM_strp, String1);1162 1163 // Generate the DWARF1164 StringRef FileBytes = DG->generate();1165 MemoryBufferRef FileBuffer(FileBytes, "dwarf");1166 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);1167 ASSERT_TRUE((bool)Obj);1168 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);1169 EXPECT_TRUE(1170 DwarfContext->getDWARFObj().getStrOffsetsSection().Data.empty());1171}1172 1173TEST(DWARFDebugInfo, TestRelations) {1174 Triple Triple = getNormalizedDefaultTargetTriple();1175 if (!isConfigurationSupported(Triple))1176 GTEST_SKIP();1177 1178 // Test the DWARF APIs related to accessing the DW_AT_low_pc and1179 // DW_AT_high_pc.1180 uint16_t Version = 4;1181 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);1182 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());1183 dwarfgen::Generator *DG = ExpectedDG.get().get();1184 dwarfgen::CompileUnit &CU = DG->addCompileUnit();1185 1186 enum class Tag: uint16_t {1187 A = dwarf::DW_TAG_lo_user,1188 B,1189 C,1190 C1,1191 C2,1192 D,1193 D11194 };1195 1196 // Scope to allow us to re-use the same DIE names1197 {1198 // Create DWARF tree that looks like:1199 //1200 // CU1201 // A1202 // B1203 // C1204 // C11205 // C21206 // D1207 // D11208 dwarfgen::DIE CUDie = CU.getUnitDIE();1209 dwarfgen::DIE A = CUDie.addChild((dwarf::Tag)Tag::A);1210 A.addChild((dwarf::Tag)Tag::B);1211 dwarfgen::DIE C = A.addChild((dwarf::Tag)Tag::C);1212 dwarfgen::DIE D = A.addChild((dwarf::Tag)Tag::D);1213 C.addChild((dwarf::Tag)Tag::C1);1214 C.addChild((dwarf::Tag)Tag::C2);1215 D.addChild((dwarf::Tag)Tag::D1);1216 }1217 1218 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");1219 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);1220 EXPECT_TRUE((bool)Obj);1221 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);1222 1223 // Verify the number of compile units is correct.1224 uint32_t NumCUs = DwarfContext->getNumCompileUnits();1225 EXPECT_EQ(NumCUs, 1u);1226 DWARFCompileUnit *U =1227 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));1228 1229 // Get the compile unit DIE is valid.1230 auto CUDie = U->getUnitDIE(false);1231 EXPECT_TRUE(CUDie.isValid());1232 1233 // The compile unit doesn't have a parent or a sibling.1234 auto ParentDie = CUDie.getParent();1235 EXPECT_FALSE(ParentDie.isValid());1236 auto SiblingDie = CUDie.getSibling();1237 EXPECT_FALSE(SiblingDie.isValid());1238 1239 // Get the children of the compile unit1240 auto A = CUDie.getFirstChild();1241 auto B = A.getFirstChild();1242 auto C = B.getSibling();1243 auto D = C.getSibling();1244 auto Null = D.getSibling();1245 1246 // Verify NULL Die is NULL and has no children or siblings1247 EXPECT_TRUE(Null.isNULL());1248 EXPECT_FALSE(Null.getSibling().isValid());1249 EXPECT_FALSE(Null.getFirstChild().isValid());1250 1251 // Verify all children of the compile unit DIE are correct.1252 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A);1253 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B);1254 EXPECT_EQ(C.getTag(), (dwarf::Tag)Tag::C);1255 EXPECT_EQ(D.getTag(), (dwarf::Tag)Tag::D);1256 1257 // Verify who has children1258 EXPECT_TRUE(A.hasChildren());1259 EXPECT_FALSE(B.hasChildren());1260 EXPECT_TRUE(C.hasChildren());1261 EXPECT_TRUE(D.hasChildren());1262 1263 // Make sure the parent of all the children of the compile unit are the1264 // compile unit.1265 EXPECT_EQ(A.getParent(), CUDie);1266 1267 // Make sure the parent of all the children of A are the A.1268 // B is the first child in A, so we need to verify we can get the previous1269 // DIE as the parent.1270 EXPECT_EQ(B.getParent(), A);1271 // C is the second child in A, so we need to make sure we can backup across1272 // other DIE (B) at the same level to get the correct parent.1273 EXPECT_EQ(C.getParent(), A);1274 // D is the third child of A. We need to verify we can backup across other DIE1275 // (B and C) including DIE that have children (D) to get the correct parent.1276 EXPECT_EQ(D.getParent(), A);1277 1278 // Verify that a DIE with no children returns an invalid DWARFDie.1279 EXPECT_FALSE(B.getFirstChild().isValid());1280 1281 // Verify the children of the B DIE1282 auto C1 = C.getFirstChild();1283 auto C2 = C1.getSibling();1284 EXPECT_TRUE(C2.getSibling().isNULL());1285 1286 // Verify all children of the B DIE correctly valid or invalid.1287 EXPECT_EQ(C1.getTag(), (dwarf::Tag)Tag::C1);1288 EXPECT_EQ(C2.getTag(), (dwarf::Tag)Tag::C2);1289 1290 // Make sure the parent of all the children of the B are the B.1291 EXPECT_EQ(C1.getParent(), C);1292 EXPECT_EQ(C2.getParent(), C);1293 1294 // Make sure iterators work as expected.1295 EXPECT_THAT(std::vector<DWARFDie>(A.begin(), A.end()),1296 testing::ElementsAre(B, C, D));1297 EXPECT_THAT(std::vector<DWARFDie>(A.rbegin(), A.rend()),1298 testing::ElementsAre(D, C, B));1299 1300 // Make sure conversion from reverse iterator works as expected.1301 EXPECT_EQ(A.rbegin().base(), A.end());1302 EXPECT_EQ(A.rend().base(), A.begin());1303 1304 // Make sure iterator is bidirectional.1305 {1306 auto Begin = A.begin();1307 auto End = A.end();1308 auto It = A.begin();1309 1310 EXPECT_EQ(It, Begin);1311 EXPECT_EQ(*It, B);1312 ++It;1313 EXPECT_EQ(*It, C);1314 ++It;1315 EXPECT_EQ(*It, D);1316 ++It;1317 EXPECT_EQ(It, End);1318 --It;1319 EXPECT_EQ(*It, D);1320 --It;1321 EXPECT_EQ(*It, C);1322 --It;1323 EXPECT_EQ(*It, B);1324 EXPECT_EQ(It, Begin);1325 }1326 1327 // Make sure reverse iterator is bidirectional.1328 {1329 auto Begin = A.rbegin();1330 auto End = A.rend();1331 auto It = A.rbegin();1332 1333 EXPECT_EQ(It, Begin);1334 EXPECT_EQ(*It, D);1335 ++It;1336 EXPECT_EQ(*It, C);1337 ++It;1338 EXPECT_EQ(*It, B);1339 ++It;1340 EXPECT_EQ(It, End);1341 --It;1342 EXPECT_EQ(*It, B);1343 --It;1344 EXPECT_EQ(*It, C);1345 --It;1346 EXPECT_EQ(*It, D);1347 EXPECT_EQ(It, Begin);1348 }1349}1350 1351TEST(DWARFDebugInfo, TestDWARFDie) {1352 // Make sure a default constructed DWARFDie doesn't have any parent, sibling1353 // or child;1354 DWARFDie DefaultDie;1355 EXPECT_FALSE(DefaultDie.getParent().isValid());1356 EXPECT_FALSE(DefaultDie.getFirstChild().isValid());1357 EXPECT_FALSE(DefaultDie.getSibling().isValid());1358}1359 1360TEST(DWARFDebugInfo, TestChildIterators) {1361 Triple Triple = getNormalizedDefaultTargetTriple();1362 if (!isConfigurationSupported(Triple))1363 GTEST_SKIP();1364 1365 // Test the DWARF APIs related to iterating across the children of a DIE using1366 // the DWARFDie::iterator class.1367 uint16_t Version = 4;1368 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);1369 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());1370 dwarfgen::Generator *DG = ExpectedDG.get().get();1371 dwarfgen::CompileUnit &CU = DG->addCompileUnit();1372 1373 enum class Tag: uint16_t {1374 A = dwarf::DW_TAG_lo_user,1375 B,1376 };1377 1378 // Scope to allow us to re-use the same DIE names1379 {1380 // Create DWARF tree that looks like:1381 //1382 // CU1383 // A1384 // B1385 auto CUDie = CU.getUnitDIE();1386 CUDie.addChild((dwarf::Tag)Tag::A);1387 CUDie.addChild((dwarf::Tag)Tag::B);1388 }1389 1390 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");1391 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);1392 EXPECT_TRUE((bool)Obj);1393 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);1394 1395 // Verify the number of compile units is correct.1396 uint32_t NumCUs = DwarfContext->getNumCompileUnits();1397 EXPECT_EQ(NumCUs, 1u);1398 DWARFCompileUnit *U =1399 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));1400 1401 // Get the compile unit DIE is valid.1402 auto CUDie = U->getUnitDIE(false);1403 EXPECT_TRUE(CUDie.isValid());1404 uint32_t Index;1405 DWARFDie A;1406 DWARFDie B;1407 1408 // Verify the compile unit DIE's children.1409 Index = 0;1410 for (auto Die : CUDie.children()) {1411 switch (Index++) {1412 case 0: A = Die; break;1413 case 1: B = Die; break;1414 }1415 }1416 1417 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A);1418 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B);1419 1420 // Verify that A has no children by verifying that the begin and end contain1421 // invalid DIEs and also that the iterators are equal.1422 EXPECT_EQ(A.begin(), A.end());1423}1424 1425TEST(DWARFDebugInfo, TestChildIteratorsOnInvalidDie) {1426 // Verify that an invalid DIE has no children.1427 DWARFDie Invalid;1428 auto begin = Invalid.begin();1429 auto end = Invalid.end();1430 EXPECT_FALSE(begin->isValid());1431 EXPECT_FALSE(end->isValid());1432 EXPECT_EQ(begin, end);1433}1434 1435TEST(DWARFDebugInfo, TestEmptyChildren) {1436 const char *yamldata = "debug_abbrev:\n"1437 " - Table:\n"1438 " - Code: 0x00000001\n"1439 " Tag: DW_TAG_compile_unit\n"1440 " Children: DW_CHILDREN_yes\n"1441 "debug_info:\n"1442 " - Version: 4\n"1443 " AddrSize: 8\n"1444 " Entries:\n"1445 " - AbbrCode: 0x00000001\n"1446 " - AbbrCode: 0x00000000\n";1447 1448 auto ErrOrSections = DWARFYAML::emitDebugSections(StringRef(yamldata));1449 ASSERT_TRUE((bool)ErrOrSections);1450 std::unique_ptr<DWARFContext> DwarfContext =1451 DWARFContext::create(*ErrOrSections, 8);1452 1453 // Verify the number of compile units is correct.1454 uint32_t NumCUs = DwarfContext->getNumCompileUnits();1455 EXPECT_EQ(NumCUs, 1u);1456 DWARFCompileUnit *U =1457 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));1458 1459 // Get the compile unit DIE is valid.1460 auto CUDie = U->getUnitDIE(false);1461 EXPECT_TRUE(CUDie.isValid());1462 1463 // Verify that the CU Die that says it has children, but doesn't, actually1464 // has begin and end iterators that are equal. We want to make sure we don't1465 // see the Null DIEs during iteration.1466 EXPECT_EQ(CUDie.begin(), CUDie.end());1467}1468 1469TEST(DWARFDebugInfo, TestAttributeIterators) {1470 Triple Triple = getNormalizedDefaultTargetTriple();1471 if (!isConfigurationSupported(Triple))1472 GTEST_SKIP();1473 1474 // Test the DWARF APIs related to iterating across all attribute values in a1475 // a DWARFDie.1476 uint16_t Version = 4;1477 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);1478 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());1479 dwarfgen::Generator *DG = ExpectedDG.get().get();1480 dwarfgen::CompileUnit &CU = DG->addCompileUnit();1481 const uint64_t CULowPC = 0x1000;1482 StringRef CUPath("/tmp/main.c");1483 1484 // Scope to allow us to re-use the same DIE names1485 {1486 auto CUDie = CU.getUnitDIE();1487 // Encode an attribute value before an attribute with no data.1488 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, CUPath.data());1489 // Encode an attribute value with no data in .debug_info/types to ensure1490 // the iteration works correctly.1491 CUDie.addAttribute(DW_AT_declaration, DW_FORM_flag_present);1492 // Encode an attribute value after an attribute with no data.1493 CUDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, CULowPC);1494 }1495 1496 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");1497 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);1498 EXPECT_TRUE((bool)Obj);1499 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);1500 1501 // Verify the number of compile units is correct.1502 uint32_t NumCUs = DwarfContext->getNumCompileUnits();1503 EXPECT_EQ(NumCUs, 1u);1504 DWARFCompileUnit *U =1505 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));1506 1507 // Get the compile unit DIE is valid.1508 auto CUDie = U->getUnitDIE(false);1509 EXPECT_TRUE(CUDie.isValid());1510 1511 auto R = CUDie.attributes();1512 auto I = R.begin();1513 auto E = R.end();1514 1515 ASSERT_NE(E, I);1516 EXPECT_EQ(I->Attr, DW_AT_name);1517 auto ActualCUPath = toString(I->Value);1518 EXPECT_EQ(CUPath, *ActualCUPath);1519 1520 ASSERT_NE(E, ++I);1521 EXPECT_EQ(I->Attr, DW_AT_declaration);1522 EXPECT_EQ(1ull, *I->Value.getAsUnsignedConstant());1523 1524 ASSERT_NE(E, ++I);1525 EXPECT_EQ(I->Attr, DW_AT_low_pc);1526 EXPECT_EQ(CULowPC, *I->Value.getAsAddress());1527 1528 EXPECT_EQ(E, ++I);1529}1530 1531TEST(DWARFDebugInfo, TestFindRecurse) {1532 Triple Triple = getNormalizedDefaultTargetTriple();1533 if (!isConfigurationSupported(Triple))1534 GTEST_SKIP();1535 1536 uint16_t Version = 4;1537 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);1538 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());1539 dwarfgen::Generator *DG = ExpectedDG.get().get();1540 dwarfgen::CompileUnit &CU = DG->addCompileUnit();1541 1542 StringRef SpecDieName = "spec";1543 StringRef SpecLinkageName = "spec_linkage";1544 StringRef AbsDieName = "abs";1545 // Scope to allow us to re-use the same DIE names1546 {1547 auto CUDie = CU.getUnitDIE();1548 auto FuncSpecDie = CUDie.addChild(DW_TAG_subprogram);1549 auto FuncAbsDie = CUDie.addChild(DW_TAG_subprogram);1550 // Put the linkage name in a second abstract origin DIE to ensure we1551 // recurse through more than just one DIE when looking for attributes.1552 auto FuncAbsDie2 = CUDie.addChild(DW_TAG_subprogram);1553 auto FuncDie = CUDie.addChild(DW_TAG_subprogram);1554 auto VarAbsDie = CUDie.addChild(DW_TAG_variable);1555 auto VarDie = CUDie.addChild(DW_TAG_variable);1556 FuncSpecDie.addAttribute(DW_AT_name, DW_FORM_strp, SpecDieName);1557 FuncAbsDie2.addAttribute(DW_AT_linkage_name, DW_FORM_strp, SpecLinkageName);1558 FuncAbsDie.addAttribute(DW_AT_specification, DW_FORM_ref4, FuncSpecDie);1559 FuncAbsDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, FuncAbsDie2);1560 FuncDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, FuncAbsDie);1561 VarAbsDie.addAttribute(DW_AT_name, DW_FORM_strp, AbsDieName);1562 VarDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, VarAbsDie);1563 }1564 1565 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");1566 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);1567 EXPECT_TRUE((bool)Obj);1568 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);1569 1570 // Verify the number of compile units is correct.1571 uint32_t NumCUs = DwarfContext->getNumCompileUnits();1572 EXPECT_EQ(NumCUs, 1u);1573 DWARFCompileUnit *U =1574 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));1575 1576 // Get the compile unit DIE is valid.1577 auto CUDie = U->getUnitDIE(false);1578 EXPECT_TRUE(CUDie.isValid());1579 1580 auto FuncSpecDie = CUDie.getFirstChild();1581 auto FuncAbsDie = FuncSpecDie.getSibling();1582 auto FuncAbsDie2 = FuncAbsDie.getSibling();1583 auto FuncDie = FuncAbsDie2.getSibling();1584 auto VarAbsDie = FuncDie.getSibling();1585 auto VarDie = VarAbsDie.getSibling();1586 1587 // Make sure we can't extract the name from the specification die when using1588 // DWARFDie::find() since it won't check the DW_AT_specification DIE.1589 EXPECT_FALSE(FuncDie.find(DW_AT_name));1590 1591 // Make sure we can extract the name from the specification die when using1592 // DWARFDie::findRecursively() since it should recurse through the1593 // DW_AT_specification DIE.1594 auto NameOpt = FuncDie.findRecursively(DW_AT_name);1595 EXPECT_TRUE(NameOpt);1596 // Test the dwarf::toString() helper function.1597 auto StringOpt = toString(NameOpt);1598 EXPECT_TRUE(StringOpt);1599 EXPECT_EQ(SpecDieName, StringOpt.value_or(nullptr));1600 // Test the dwarf::toString() helper function with a default value specified.1601 EXPECT_EQ(SpecDieName, toString(NameOpt, nullptr));1602 1603 auto LinkageNameOpt = FuncDie.findRecursively(DW_AT_linkage_name);1604 EXPECT_EQ(SpecLinkageName, toString(LinkageNameOpt).value_or(nullptr));1605 1606 // Make sure we can't extract the name from the abstract origin die when using1607 // DWARFDie::find() since it won't check the DW_AT_abstract_origin DIE.1608 EXPECT_FALSE(VarDie.find(DW_AT_name));1609 1610 // Make sure we can extract the name from the abstract origin die when using1611 // DWARFDie::findRecursively() since it should recurse through the1612 // DW_AT_abstract_origin DIE.1613 NameOpt = VarDie.findRecursively(DW_AT_name);1614 EXPECT_TRUE(NameOpt);1615 // Test the dwarf::toString() helper function.1616 StringOpt = toString(NameOpt);1617 EXPECT_TRUE(StringOpt);1618 EXPECT_EQ(AbsDieName, StringOpt.value_or(nullptr));1619}1620 1621TEST(DWARFDebugInfo, TestSelfRecursiveType) {1622 typedef uint32_t AddrType;1623 Triple Triple = getDefaultTargetTripleForAddrSize(sizeof(AddrType));1624 if (!isConfigurationSupported(Triple))1625 GTEST_SKIP();1626 1627 auto ExpectedDG = dwarfgen::Generator::create(Triple, 4);1628 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());1629 dwarfgen::Generator *DG = ExpectedDG.get().get();1630 dwarfgen::CompileUnit &CU = DG->addCompileUnit();1631 dwarfgen::DIE CUDie = CU.getUnitDIE();1632 1633 // Create an invalid self-recursive typedef.1634 dwarfgen::DIE TypedefDie = CUDie.addChild(DW_TAG_typedef);1635 TypedefDie.addAttribute(DW_AT_name, DW_FORM_strp, "illegal");1636 TypedefDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, TypedefDie);1637 1638 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");1639 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);1640 EXPECT_TRUE((bool)Obj);1641 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);1642 1643 // Verify the number of compile units is correct.1644 uint32_t NumCUs = DwarfContext->getNumCompileUnits();1645 EXPECT_EQ(NumCUs, 1u);1646 DWARFCompileUnit *U = cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));1647 {1648 DWARFDie CUDie = U->getUnitDIE(false);1649 EXPECT_TRUE(CUDie.isValid());1650 DWARFDie TypedefDie = CUDie.getFirstChild();1651 1652 // Test that getTypeSize doesn't get into an infinite loop.1653 EXPECT_EQ(TypedefDie.getTypeSize(sizeof(AddrType)), std::nullopt);1654 }1655}1656 1657TEST(DWARFDebugInfo, TestDwarfToFunctions) {1658 // Test all of the dwarf::toXXX functions that take a1659 // std::optional<DWARFFormValue> and extract the values from it.1660 uint64_t InvalidU64 = 0xBADBADBADBADBADB;1661 int64_t InvalidS64 = 0xBADBADBADBADBADB;1662 1663 // First test that we don't get valid values back when using an optional with1664 // no value.1665 std::optional<DWARFFormValue> FormValOpt1 = DWARFFormValue();1666 EXPECT_FALSE(toString(FormValOpt1).has_value());1667 EXPECT_FALSE(toUnsigned(FormValOpt1).has_value());1668 EXPECT_FALSE(toRelativeReference(FormValOpt1).has_value());1669 EXPECT_FALSE(toDebugInfoReference(FormValOpt1).has_value());1670 EXPECT_FALSE(toSignatureReference(FormValOpt1).has_value());1671 EXPECT_FALSE(toSupplementaryReference(FormValOpt1).has_value());1672 EXPECT_FALSE(toSigned(FormValOpt1).has_value());1673 EXPECT_FALSE(toAddress(FormValOpt1).has_value());1674 EXPECT_FALSE(toSectionOffset(FormValOpt1).has_value());1675 EXPECT_FALSE(toBlock(FormValOpt1).has_value());1676 EXPECT_EQ(nullptr, toString(FormValOpt1, nullptr));1677 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt1, InvalidU64));1678 EXPECT_EQ(InvalidU64, toRelativeReference(FormValOpt1, InvalidU64));1679 EXPECT_EQ(InvalidU64, toDebugInfoReference(FormValOpt1, InvalidU64));1680 EXPECT_EQ(InvalidU64, toSignatureReference(FormValOpt1, InvalidU64));1681 EXPECT_EQ(InvalidU64, toSupplementaryReference(FormValOpt1, InvalidU64));1682 EXPECT_EQ(InvalidU64, toAddress(FormValOpt1, InvalidU64));1683 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt1, InvalidU64));1684 EXPECT_EQ(InvalidS64, toSigned(FormValOpt1, InvalidS64));1685 1686 // Test successful and unsuccessful address decoding.1687 uint64_t Address = 0x100000000ULL;1688 std::optional<DWARFFormValue> FormValOpt2 =1689 DWARFFormValue::createFromUValue(DW_FORM_addr, Address);1690 1691 EXPECT_FALSE(toString(FormValOpt2).has_value());1692 EXPECT_FALSE(toUnsigned(FormValOpt2).has_value());1693 EXPECT_FALSE(toRelativeReference(FormValOpt2).has_value());1694 EXPECT_FALSE(toDebugInfoReference(FormValOpt2).has_value());1695 EXPECT_FALSE(toSignatureReference(FormValOpt2).has_value());1696 EXPECT_FALSE(toSupplementaryReference(FormValOpt2).has_value());1697 EXPECT_FALSE(toSigned(FormValOpt2).has_value());1698 EXPECT_TRUE(toAddress(FormValOpt2).has_value());1699 EXPECT_FALSE(toSectionOffset(FormValOpt2).has_value());1700 EXPECT_FALSE(toBlock(FormValOpt2).has_value());1701 EXPECT_EQ(nullptr, toString(FormValOpt2, nullptr));1702 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt2, InvalidU64));1703 EXPECT_EQ(InvalidU64, toRelativeReference(FormValOpt2, InvalidU64));1704 EXPECT_EQ(InvalidU64, toDebugInfoReference(FormValOpt2, InvalidU64));1705 EXPECT_EQ(InvalidU64, toSignatureReference(FormValOpt2, InvalidU64));1706 EXPECT_EQ(InvalidU64, toSupplementaryReference(FormValOpt2, InvalidU64));1707 EXPECT_EQ(Address, toAddress(FormValOpt2, InvalidU64));1708 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt2, InvalidU64));1709 EXPECT_EQ(InvalidS64, toSigned(FormValOpt2, InvalidU64));1710 1711 // Test successful and unsuccessful unsigned constant decoding.1712 uint64_t UData8 = 0x1020304050607080ULL;1713 std::optional<DWARFFormValue> FormValOpt3 =1714 DWARFFormValue::createFromUValue(DW_FORM_udata, UData8);1715 1716 EXPECT_FALSE(toString(FormValOpt3).has_value());1717 EXPECT_TRUE(toUnsigned(FormValOpt3).has_value());1718 EXPECT_FALSE(toRelativeReference(FormValOpt3).has_value());1719 EXPECT_FALSE(toDebugInfoReference(FormValOpt3).has_value());1720 EXPECT_FALSE(toSignatureReference(FormValOpt3).has_value());1721 EXPECT_FALSE(toSupplementaryReference(FormValOpt3).has_value());1722 EXPECT_TRUE(toSigned(FormValOpt3).has_value());1723 EXPECT_FALSE(toAddress(FormValOpt3).has_value());1724 EXPECT_FALSE(toSectionOffset(FormValOpt3).has_value());1725 EXPECT_FALSE(toBlock(FormValOpt3).has_value());1726 EXPECT_EQ(nullptr, toString(FormValOpt3, nullptr));1727 EXPECT_EQ(UData8, toUnsigned(FormValOpt3, InvalidU64));1728 EXPECT_EQ(InvalidU64, toRelativeReference(FormValOpt3, InvalidU64));1729 EXPECT_EQ(InvalidU64, toDebugInfoReference(FormValOpt3, InvalidU64));1730 EXPECT_EQ(InvalidU64, toSignatureReference(FormValOpt3, InvalidU64));1731 EXPECT_EQ(InvalidU64, toSupplementaryReference(FormValOpt3, InvalidU64));1732 EXPECT_EQ(InvalidU64, toAddress(FormValOpt3, InvalidU64));1733 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt3, InvalidU64));1734 EXPECT_EQ((int64_t)UData8, toSigned(FormValOpt3, InvalidU64));1735 1736 // Test successful and unsuccessful ref_addr decoding.1737 uint32_t RefData = 0x11223344U;1738 std::optional<DWARFFormValue> FormValOpt4Addr =1739 DWARFFormValue::createFromUValue(DW_FORM_ref_addr, RefData);1740 1741 EXPECT_FALSE(toString(FormValOpt4Addr).has_value());1742 EXPECT_FALSE(toUnsigned(FormValOpt4Addr).has_value());1743 EXPECT_FALSE(toRelativeReference(FormValOpt4Addr).has_value());1744 EXPECT_TRUE(toDebugInfoReference(FormValOpt4Addr).has_value());1745 EXPECT_FALSE(toSignatureReference(FormValOpt4Addr).has_value());1746 EXPECT_FALSE(toSupplementaryReference(FormValOpt4Addr).has_value());1747 EXPECT_FALSE(toSigned(FormValOpt4Addr).has_value());1748 EXPECT_FALSE(toAddress(FormValOpt4Addr).has_value());1749 EXPECT_FALSE(toSectionOffset(FormValOpt4Addr).has_value());1750 EXPECT_FALSE(toBlock(FormValOpt4Addr).has_value());1751 EXPECT_EQ(nullptr, toString(FormValOpt4Addr, nullptr));1752 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt4Addr, InvalidU64));1753 EXPECT_EQ(InvalidU64, toRelativeReference(FormValOpt4Addr, InvalidU64));1754 EXPECT_EQ(RefData, toDebugInfoReference(FormValOpt4Addr, InvalidU64));1755 EXPECT_EQ(InvalidU64, toSignatureReference(FormValOpt4Addr, InvalidU64));1756 EXPECT_EQ(InvalidU64, toSupplementaryReference(FormValOpt4Addr, InvalidU64));1757 EXPECT_EQ(InvalidU64, toAddress(FormValOpt4Addr, InvalidU64));1758 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt4Addr, InvalidU64));1759 EXPECT_EQ(InvalidS64, toSigned(FormValOpt4Addr, InvalidU64));1760 1761 // Test successful and unsuccessful ref_sig8 decoding.1762 std::optional<DWARFFormValue> FormValOpt4Sig =1763 DWARFFormValue::createFromUValue(DW_FORM_ref_sig8, RefData);1764 1765 EXPECT_FALSE(toString(FormValOpt4Sig).has_value());1766 EXPECT_FALSE(toUnsigned(FormValOpt4Sig).has_value());1767 EXPECT_FALSE(toRelativeReference(FormValOpt4Sig).has_value());1768 EXPECT_FALSE(toDebugInfoReference(FormValOpt4Sig).has_value());1769 EXPECT_TRUE(toSignatureReference(FormValOpt4Sig).has_value());1770 EXPECT_FALSE(toSupplementaryReference(FormValOpt4Sig).has_value());1771 EXPECT_FALSE(toSigned(FormValOpt4Sig).has_value());1772 EXPECT_FALSE(toAddress(FormValOpt4Sig).has_value());1773 EXPECT_FALSE(toSectionOffset(FormValOpt4Sig).has_value());1774 EXPECT_FALSE(toBlock(FormValOpt4Sig).has_value());1775 EXPECT_EQ(nullptr, toString(FormValOpt4Sig, nullptr));1776 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt4Sig, InvalidU64));1777 EXPECT_EQ(InvalidU64, toRelativeReference(FormValOpt4Sig, InvalidU64));1778 EXPECT_EQ(InvalidU64, toDebugInfoReference(FormValOpt4Sig, InvalidU64));1779 EXPECT_EQ(RefData, toSignatureReference(FormValOpt4Sig, InvalidU64));1780 EXPECT_EQ(InvalidU64, toSupplementaryReference(FormValOpt4Sig, InvalidU64));1781 EXPECT_EQ(InvalidU64, toAddress(FormValOpt4Sig, InvalidU64));1782 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt4Sig, InvalidU64));1783 EXPECT_EQ(InvalidS64, toSigned(FormValOpt4Sig, InvalidU64));1784 1785 // Test successful and unsuccessful ref_alt decoding.1786 // Not testing relative reference forms here, as they require a valid1787 // DWARFUnit object.1788 std::optional<DWARFFormValue> FormValOpt4Alt =1789 DWARFFormValue::createFromUValue(DW_FORM_GNU_ref_alt, RefData);1790 1791 EXPECT_FALSE(toString(FormValOpt4Alt).has_value());1792 EXPECT_FALSE(toUnsigned(FormValOpt4Alt).has_value());1793 EXPECT_FALSE(toRelativeReference(FormValOpt4Alt).has_value());1794 EXPECT_FALSE(toDebugInfoReference(FormValOpt4Alt).has_value());1795 EXPECT_FALSE(toSignatureReference(FormValOpt4Alt).has_value());1796 EXPECT_TRUE(toSupplementaryReference(FormValOpt4Alt).has_value());1797 EXPECT_FALSE(toSigned(FormValOpt4Alt).has_value());1798 EXPECT_FALSE(toAddress(FormValOpt4Alt).has_value());1799 EXPECT_FALSE(toSectionOffset(FormValOpt4Alt).has_value());1800 EXPECT_FALSE(toBlock(FormValOpt4Alt).has_value());1801 EXPECT_EQ(nullptr, toString(FormValOpt4Alt, nullptr));1802 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt4Alt, InvalidU64));1803 EXPECT_EQ(InvalidU64, toRelativeReference(FormValOpt4Alt, InvalidU64));1804 EXPECT_EQ(InvalidU64, toDebugInfoReference(FormValOpt4Alt, InvalidU64));1805 EXPECT_EQ(InvalidU64, toSignatureReference(FormValOpt4Alt, InvalidU64));1806 EXPECT_EQ(RefData, toSupplementaryReference(FormValOpt4Alt, InvalidU64));1807 EXPECT_EQ(InvalidU64, toAddress(FormValOpt4Alt, InvalidU64));1808 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt4Alt, InvalidU64));1809 EXPECT_EQ(InvalidS64, toSigned(FormValOpt4Alt, InvalidU64));1810 1811 // Test successful and unsuccessful signed constant decoding.1812 int64_t SData8 = 0x1020304050607080ULL;1813 std::optional<DWARFFormValue> FormValOpt5 =1814 DWARFFormValue::createFromSValue(DW_FORM_udata, SData8);1815 1816 EXPECT_FALSE(toString(FormValOpt5).has_value());1817 EXPECT_TRUE(toUnsigned(FormValOpt5).has_value());1818 EXPECT_FALSE(toRelativeReference(FormValOpt5).has_value());1819 EXPECT_FALSE(toDebugInfoReference(FormValOpt5).has_value());1820 EXPECT_FALSE(toSignatureReference(FormValOpt5).has_value());1821 EXPECT_FALSE(toSupplementaryReference(FormValOpt5).has_value());1822 EXPECT_TRUE(toSigned(FormValOpt5).has_value());1823 EXPECT_FALSE(toAddress(FormValOpt5).has_value());1824 EXPECT_FALSE(toSectionOffset(FormValOpt5).has_value());1825 EXPECT_FALSE(toBlock(FormValOpt5).has_value());1826 EXPECT_EQ(nullptr, toString(FormValOpt5, nullptr));1827 EXPECT_EQ((uint64_t)SData8, toUnsigned(FormValOpt5, InvalidU64));1828 EXPECT_EQ(InvalidU64, toRelativeReference(FormValOpt5, InvalidU64));1829 EXPECT_EQ(InvalidU64, toDebugInfoReference(FormValOpt5, InvalidU64));1830 EXPECT_EQ(InvalidU64, toSignatureReference(FormValOpt5, InvalidU64));1831 EXPECT_EQ(InvalidU64, toSupplementaryReference(FormValOpt5, InvalidU64));1832 EXPECT_EQ(InvalidU64, toAddress(FormValOpt5, InvalidU64));1833 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt5, InvalidU64));1834 EXPECT_EQ(SData8, toSigned(FormValOpt5, InvalidU64));1835 1836 // Test successful and unsuccessful block decoding.1837 uint8_t Data[] = { 2, 3, 4 };1838 ArrayRef<uint8_t> Array(Data);1839 std::optional<DWARFFormValue> FormValOpt6 =1840 DWARFFormValue::createFromBlockValue(DW_FORM_block1, Array);1841 1842 EXPECT_FALSE(toString(FormValOpt6).has_value());1843 EXPECT_FALSE(toUnsigned(FormValOpt6).has_value());1844 EXPECT_FALSE(toRelativeReference(FormValOpt6).has_value());1845 EXPECT_FALSE(toDebugInfoReference(FormValOpt6).has_value());1846 EXPECT_FALSE(toSignatureReference(FormValOpt6).has_value());1847 EXPECT_FALSE(toSupplementaryReference(FormValOpt6).has_value());1848 EXPECT_FALSE(toSigned(FormValOpt6).has_value());1849 EXPECT_FALSE(toAddress(FormValOpt6).has_value());1850 EXPECT_FALSE(toSectionOffset(FormValOpt6).has_value());1851 auto BlockOpt = toBlock(FormValOpt6);1852 EXPECT_TRUE(BlockOpt.has_value());1853 EXPECT_EQ(*BlockOpt, Array);1854 EXPECT_EQ(nullptr, toString(FormValOpt6, nullptr));1855 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt6, InvalidU64));1856 EXPECT_EQ(InvalidU64, toRelativeReference(FormValOpt6, InvalidU64));1857 EXPECT_EQ(InvalidU64, toDebugInfoReference(FormValOpt6, InvalidU64));1858 EXPECT_EQ(InvalidU64, toSignatureReference(FormValOpt6, InvalidU64));1859 EXPECT_EQ(InvalidU64, toSupplementaryReference(FormValOpt6, InvalidU64));1860 EXPECT_EQ(InvalidU64, toAddress(FormValOpt6, InvalidU64));1861 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt6, InvalidU64));1862 EXPECT_EQ(InvalidS64, toSigned(FormValOpt6, InvalidU64));1863 1864 // Test1865}1866 1867TEST(DWARFDebugInfo, TestFindAttrs) {1868 Triple Triple = getNormalizedDefaultTargetTriple();1869 if (!isConfigurationSupported(Triple))1870 GTEST_SKIP();1871 1872 // Test the DWARFDie::find() and DWARFDie::findRecursively() that take an1873 // ArrayRef<dwarf::Attribute> value to make sure they work correctly.1874 uint16_t Version = 4;1875 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);1876 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());1877 dwarfgen::Generator *DG = ExpectedDG.get().get();1878 dwarfgen::CompileUnit &CU = DG->addCompileUnit();1879 1880 StringRef DieMangled("_Z3fooi");1881 // Scope to allow us to re-use the same DIE names1882 {1883 auto CUDie = CU.getUnitDIE();1884 auto FuncSpecDie = CUDie.addChild(DW_TAG_subprogram);1885 auto FuncDie = CUDie.addChild(DW_TAG_subprogram);1886 FuncSpecDie.addAttribute(DW_AT_MIPS_linkage_name, DW_FORM_strp, DieMangled);1887 FuncDie.addAttribute(DW_AT_specification, DW_FORM_ref4, FuncSpecDie);1888 }1889 1890 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");1891 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);1892 EXPECT_TRUE((bool)Obj);1893 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);1894 1895 // Verify the number of compile units is correct.1896 uint32_t NumCUs = DwarfContext->getNumCompileUnits();1897 EXPECT_EQ(NumCUs, 1u);1898 DWARFCompileUnit *U =1899 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));1900 1901 // Get the compile unit DIE is valid.1902 auto CUDie = U->getUnitDIE(false);1903 EXPECT_TRUE(CUDie.isValid());1904 1905 auto FuncSpecDie = CUDie.getFirstChild();1906 auto FuncDie = FuncSpecDie.getSibling();1907 1908 // Make sure that passing in an empty attribute list behave correctly.1909 EXPECT_FALSE(FuncDie.find(ArrayRef<dwarf::Attribute>()).has_value());1910 1911 // Make sure that passing in a list of attribute that are not contained1912 // in the DIE returns nothing.1913 EXPECT_FALSE(FuncDie.find({DW_AT_low_pc, DW_AT_entry_pc}).has_value());1914 1915 const dwarf::Attribute Attrs[] = {DW_AT_linkage_name,1916 DW_AT_MIPS_linkage_name};1917 1918 // Make sure we can't extract the linkage name attributes when using1919 // DWARFDie::find() since it won't check the DW_AT_specification DIE.1920 EXPECT_FALSE(FuncDie.find(Attrs).has_value());1921 1922 // Make sure we can extract the name from the specification die when using1923 // DWARFDie::findRecursively() since it should recurse through the1924 // DW_AT_specification DIE.1925 auto NameOpt = FuncDie.findRecursively(Attrs);1926 EXPECT_TRUE(NameOpt.has_value());1927 EXPECT_EQ(DieMangled, toString(NameOpt, ""));1928}1929 1930#ifdef NO_SUPPORT_DEBUG_ADDR1931TEST(DWARFDebugInfo, DISABLED_TestImplicitConstAbbrevs) {1932#else1933TEST(DWARFDebugInfo, TestImplicitConstAbbrevs) {1934#endif1935 Triple Triple = getNormalizedDefaultTargetTriple();1936 if (!isConfigurationSupported(Triple))1937 GTEST_SKIP();1938 1939 uint16_t Version = 5;1940 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);1941 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());1942 dwarfgen::Generator *DG = ExpectedDG.get().get();1943 dwarfgen::CompileUnit &CU = DG->addCompileUnit();1944 dwarfgen::DIE CUDie = CU.getUnitDIE();1945 const dwarf::Attribute Attr = DW_AT_lo_user;1946 const int64_t Val1 = 42;1947 const int64_t Val2 = 43;1948 1949 auto FirstVal1DIE = CUDie.addChild(DW_TAG_class_type);1950 FirstVal1DIE.addAttribute(Attr, DW_FORM_implicit_const, Val1);1951 1952 auto SecondVal1DIE = CUDie.addChild(DW_TAG_class_type);1953 SecondVal1DIE.addAttribute(Attr, DW_FORM_implicit_const, Val1);1954 1955 auto Val2DIE = CUDie.addChild(DW_TAG_class_type);1956 Val2DIE.addAttribute(Attr, DW_FORM_implicit_const, Val2);1957 1958 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");1959 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);1960 EXPECT_TRUE((bool)Obj);1961 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);1962 DWARFCompileUnit *U =1963 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));1964 EXPECT_TRUE((bool)U);1965 1966 const auto *Abbrevs = U->getAbbreviations();1967 EXPECT_TRUE((bool)Abbrevs);1968 1969 // Let's find implicit_const abbrevs and verify,1970 // that there are exactly two of them and both of them1971 // can be dumped correctly.1972 typedef decltype(Abbrevs->begin()) AbbrevIt;1973 AbbrevIt Val1Abbrev = Abbrevs->end();1974 AbbrevIt Val2Abbrev = Abbrevs->end();1975 for(auto it = Abbrevs->begin(); it != Abbrevs->end(); ++it) {1976 if (it->getNumAttributes() == 0)1977 continue; // root abbrev for DW_TAG_compile_unit1978 1979 auto A = it->getAttrByIndex(0);1980 EXPECT_EQ(A, Attr);1981 1982 std::optional<uint32_t> AttrIndex = it->findAttributeIndex(A);1983 EXPECT_TRUE((bool)AttrIndex);1984 EXPECT_EQ(*AttrIndex, 0u);1985 uint64_t OffsetVal =1986 it->getAttributeOffsetFromIndex(*AttrIndex, /* offset */ 0, *U);1987 EXPECT_TRUE(it->getAttributeValueFromOffset(*AttrIndex, OffsetVal, *U));1988 1989 auto FormValue = it->getAttributeValue(/* offset */ 0, A, *U);1990 EXPECT_TRUE((bool)FormValue);1991 EXPECT_EQ(FormValue->getForm(), dwarf::DW_FORM_implicit_const);1992 1993 const auto V = FormValue->getAsSignedConstant();1994 EXPECT_TRUE((bool)V);1995 1996 auto VerifyAbbrevDump = [&V](AbbrevIt it) {1997 std::string S;1998 llvm::raw_string_ostream OS(S);1999 it->dump(OS);2000 auto FormPos = OS.str().find("DW_FORM_implicit_const");2001 EXPECT_NE(FormPos, std::string::npos);2002 auto ValPos = S.find_first_of("-0123456789", FormPos);2003 EXPECT_NE(ValPos, std::string::npos);2004 int64_t Val = std::atoll(S.substr(ValPos).c_str());2005 EXPECT_EQ(Val, *V);2006 };2007 2008 switch(*V) {2009 case Val1:2010 EXPECT_EQ(Val1Abbrev, Abbrevs->end());2011 Val1Abbrev = it;2012 VerifyAbbrevDump(it);2013 break;2014 case Val2:2015 EXPECT_EQ(Val2Abbrev, Abbrevs->end());2016 Val2Abbrev = it;2017 VerifyAbbrevDump(it);2018 break;2019 default:2020 FAIL() << "Unexpected attribute value: " << *V;2021 }2022 }2023 2024 // Now let's make sure that two Val1-DIEs refer to the same abbrev,2025 // and Val2-DIE refers to another one.2026 auto DieDG = U->getUnitDIE(false);2027 auto it = DieDG.begin();2028 std::multimap<int64_t, decltype(it->getAbbreviationDeclarationPtr())> DIEs;2029 const DWARFAbbreviationDeclaration *AbbrevPtrVal1 = nullptr;2030 const DWARFAbbreviationDeclaration *AbbrevPtrVal2 = nullptr;2031 for (; it != DieDG.end(); ++it) {2032 const auto *AbbrevPtr = it->getAbbreviationDeclarationPtr();2033 EXPECT_TRUE((bool)AbbrevPtr);2034 auto FormValue = it->find(Attr);2035 EXPECT_TRUE((bool)FormValue);2036 const auto V = FormValue->getAsSignedConstant();2037 EXPECT_TRUE((bool)V);2038 switch(*V) {2039 case Val1:2040 AbbrevPtrVal1 = AbbrevPtr;2041 break;2042 case Val2:2043 AbbrevPtrVal2 = AbbrevPtr;2044 break;2045 default:2046 FAIL() << "Unexpected attribute value: " << *V;2047 }2048 DIEs.insert(std::make_pair(*V, AbbrevPtr));2049 }2050 EXPECT_EQ(DIEs.count(Val1), 2u);2051 EXPECT_EQ(DIEs.count(Val2), 1u);2052 auto Val1Range = DIEs.equal_range(Val1);2053 for (auto it = Val1Range.first; it != Val1Range.second; ++it)2054 EXPECT_EQ(it->second, AbbrevPtrVal1);2055 EXPECT_EQ(DIEs.find(Val2)->second, AbbrevPtrVal2);2056}2057 2058TEST(DWARFDebugInfo, TestDWARFDieRangeInfoContains) {2059 DWARFVerifier::DieRangeInfo Empty;2060 ASSERT_TRUE(Empty.contains(Empty));2061 2062 DWARFVerifier::DieRangeInfo Ranges(2063 {{0x10, 0x20}, {0x30, 0x40}, {0x40, 0x50}});2064 2065 ASSERT_TRUE(Ranges.contains(Empty));2066 ASSERT_FALSE(Ranges.contains({{{0x0f, 0x10}}}));2067 ASSERT_FALSE(Ranges.contains({{{0x0f, 0x20}}}));2068 ASSERT_FALSE(Ranges.contains({{{0x0f, 0x21}}}));2069 2070 // Test ranges that start at R's start address2071 ASSERT_TRUE(Ranges.contains({{{0x10, 0x10}}}));2072 ASSERT_TRUE(Ranges.contains({{{0x10, 0x11}}}));2073 ASSERT_TRUE(Ranges.contains({{{0x10, 0x20}}}));2074 ASSERT_FALSE(Ranges.contains({{{0x10, 0x21}}}));2075 2076 ASSERT_TRUE(Ranges.contains({{{0x11, 0x12}}}));2077 2078 // Test ranges that start at last bytes of Range2079 ASSERT_TRUE(Ranges.contains({{{0x1f, 0x20}}}));2080 ASSERT_FALSE(Ranges.contains({{{0x1f, 0x21}}}));2081 2082 // Test ranges that start after Range2083 ASSERT_TRUE(Ranges.contains({{{0x20, 0x20}}}));2084 ASSERT_FALSE(Ranges.contains({{{0x20, 0x21}}}));2085 2086 ASSERT_TRUE(Ranges.contains({{{0x31, 0x32}}}));2087 ASSERT_TRUE(Ranges.contains({{{0x3f, 0x40}}}));2088 ASSERT_TRUE(Ranges.contains({{{0x10, 0x20}, {0x30, 0x40}}}));2089 ASSERT_TRUE(Ranges.contains({{{0x11, 0x12}, {0x31, 0x32}}}));2090 ASSERT_TRUE(Ranges.contains(2091 {{{0x11, 0x12}, {0x12, 0x13}, {0x31, 0x32}, {0x32, 0x33}}}));2092 ASSERT_FALSE(Ranges.contains({{{0x11, 0x12},2093 {0x12, 0x13},2094 {0x20, 0x21},2095 {0x31, 0x32},2096 {0x32, 0x33}}}));2097 ASSERT_FALSE(Ranges.contains(2098 {{{0x11, 0x12}, {0x12, 0x13}, {0x31, 0x32}, {0x32, 0x51}}}));2099 ASSERT_TRUE(Ranges.contains({{{0x11, 0x12}, {0x30, 0x50}}}));2100 ASSERT_FALSE(Ranges.contains({{{0x30, 0x51}}}));2101 ASSERT_FALSE(Ranges.contains({{{0x50, 0x51}}}));2102}2103 2104namespace {2105 2106void AssertRangesIntersect(const DWARFAddressRange &LHS,2107 const DWARFAddressRange &RHS) {2108 ASSERT_TRUE(LHS.intersects(RHS));2109 ASSERT_TRUE(RHS.intersects(LHS));2110}2111void AssertRangesDontIntersect(const DWARFAddressRange &LHS,2112 const DWARFAddressRange &RHS) {2113 ASSERT_FALSE(LHS.intersects(RHS));2114 ASSERT_FALSE(RHS.intersects(LHS));2115}2116 2117void AssertRangesIntersect(const DWARFVerifier::DieRangeInfo &LHS,2118 const DWARFAddressRangesVector &Ranges) {2119 DWARFVerifier::DieRangeInfo RHS(Ranges);2120 ASSERT_TRUE(LHS.intersects(RHS));2121 ASSERT_TRUE(RHS.intersects(LHS));2122}2123 2124void AssertRangesDontIntersect(const DWARFVerifier::DieRangeInfo &LHS,2125 const DWARFAddressRangesVector &Ranges) {2126 DWARFVerifier::DieRangeInfo RHS(Ranges);2127 ASSERT_FALSE(LHS.intersects(RHS));2128 ASSERT_FALSE(RHS.intersects(LHS));2129}2130 2131} // namespace2132TEST(DWARFDebugInfo, TestDwarfRangesIntersect) {2133 DWARFAddressRange R(0x10, 0x20);2134 2135 //----------------------------------------------------------------------2136 // Test ranges that start before R...2137 //----------------------------------------------------------------------2138 // Other range ends before start of R2139 AssertRangesDontIntersect(R, {0x00, 0x10});2140 // Other range end address is start of a R2141 AssertRangesIntersect(R, {0x00, 0x11});2142 // Other range end address is in R2143 AssertRangesIntersect(R, {0x00, 0x15});2144 // Other range end address is at and of R2145 AssertRangesIntersect(R, {0x00, 0x20});2146 // Other range end address is past end of R2147 AssertRangesIntersect(R, {0x00, 0x40});2148 2149 //----------------------------------------------------------------------2150 // Test ranges that start at R's start address2151 //----------------------------------------------------------------------2152 // Ensure empty ranges doesn't match2153 AssertRangesDontIntersect(R, {0x10, 0x10});2154 // 1 byte of Range2155 AssertRangesIntersect(R, {0x10, 0x11});2156 // same as Range2157 AssertRangesIntersect(R, {0x10, 0x20});2158 // 1 byte past Range2159 AssertRangesIntersect(R, {0x10, 0x21});2160 2161 //----------------------------------------------------------------------2162 // Test ranges that start inside Range2163 //----------------------------------------------------------------------2164 // empty in range2165 AssertRangesDontIntersect(R, {0x11, 0x11});2166 // all in Range2167 AssertRangesIntersect(R, {0x11, 0x1f});2168 // ends at end of Range2169 AssertRangesIntersect(R, {0x11, 0x20});2170 // ends past Range2171 AssertRangesIntersect(R, {0x11, 0x21});2172 2173 //----------------------------------------------------------------------2174 // Test ranges that start at last bytes of Range2175 //----------------------------------------------------------------------2176 // ends at end of Range2177 AssertRangesIntersect(R, {0x1f, 0x20});2178 // ends past Range2179 AssertRangesIntersect(R, {0x1f, 0x21});2180 2181 //----------------------------------------------------------------------2182 // Test ranges that start after Range2183 //----------------------------------------------------------------------2184 // empty just past in Range2185 AssertRangesDontIntersect(R, {0x20, 0x20});2186 // valid past Range2187 AssertRangesDontIntersect(R, {0x20, 0x21});2188}2189 2190TEST(DWARFDebugInfo, TestDWARFDieRangeInfoIntersects) {2191 2192 DWARFVerifier::DieRangeInfo Ranges({{0x10, 0x20}, {0x30, 0x40}});2193 2194 // Test empty range2195 AssertRangesDontIntersect(Ranges, {});2196 // Test range that appears before all ranges in Ranges2197 AssertRangesDontIntersect(Ranges, {{0x00, 0x10}});2198 // Test range that appears between ranges in Ranges2199 AssertRangesDontIntersect(Ranges, {{0x20, 0x30}});2200 // Test range that appears after ranges in Ranges2201 AssertRangesDontIntersect(Ranges, {{0x40, 0x50}});2202 2203 // Test range that start before first range2204 AssertRangesIntersect(Ranges, {{0x00, 0x11}});2205 // Test range that start at first range2206 AssertRangesIntersect(Ranges, {{0x10, 0x11}});2207 // Test range that start in first range2208 AssertRangesIntersect(Ranges, {{0x11, 0x12}});2209 // Test range that start at end of first range2210 AssertRangesIntersect(Ranges, {{0x1f, 0x20}});2211 // Test range that starts at end of first range2212 AssertRangesDontIntersect(Ranges, {{0x20, 0x21}});2213 // Test range that starts at end of first range2214 AssertRangesIntersect(Ranges, {{0x20, 0x31}});2215 2216 // Test range that start before second range and ends before second2217 AssertRangesDontIntersect(Ranges, {{0x2f, 0x30}});2218 // Test range that start before second range and ends in second2219 AssertRangesIntersect(Ranges, {{0x2f, 0x31}});2220 // Test range that start at second range2221 AssertRangesIntersect(Ranges, {{0x30, 0x31}});2222 // Test range that start in second range2223 AssertRangesIntersect(Ranges, {{0x31, 0x32}});2224 // Test range that start at end of second range2225 AssertRangesIntersect(Ranges, {{0x3f, 0x40}});2226 // Test range that starts at end of second range2227 AssertRangesDontIntersect(Ranges, {{0x40, 0x41}});2228 2229 AssertRangesDontIntersect(Ranges, {{0x20, 0x21}, {0x2f, 0x30}});2230 AssertRangesIntersect(Ranges, {{0x20, 0x21}, {0x2f, 0x31}});2231}2232 2233TEST(DWARFDebugInfo, TestDWARF64UnitLength) {2234 static const char DebugInfoSecRaw[] =2235 "\xff\xff\xff\xff" // DWARF64 mark2236 "\x88\x77\x66\x55\x44\x33\x22\x11" // Length2237 "\x05\x00" // Version2238 "\x01" // DW_UT_compile2239 "\x04" // Address size2240 "\0\0\0\0\0\0\0\0"; // Offset Into Abbrev. Sec.2241 StringMap<std::unique_ptr<MemoryBuffer>> Sections;2242 Sections.insert(std::make_pair(2243 "debug_info", MemoryBuffer::getMemBuffer(StringRef(2244 DebugInfoSecRaw, sizeof(DebugInfoSecRaw) - 1))));2245 auto Context = DWARFContext::create(Sections, /* AddrSize = */ 4,2246 /* isLittleEndian = */ true);2247 const auto &Obj = Context->getDWARFObj();2248 Obj.forEachInfoSections([&](const DWARFSection &Sec) {2249 DWARFUnitHeader Header;2250 DWARFDataExtractor Data(Obj, Sec, /* IsLittleEndian = */ true,2251 /* AddressSize = */ 4);2252 uint64_t Offset = 0;2253 ASSERT_THAT_ERROR(2254 Header.extract(*Context, Data, &Offset, DW_SECT_INFO),2255 FailedWithMessage(2256 "DWARF unit from offset 0x00000000 incl. to offset "2257 "0x1122334455667794 excl. extends past section size 0x00000018"));2258 // Header.extract() returns false because there is not enough space2259 // in the section for the declared length. Anyway, we can check that2260 // the properties are read correctly.2261 ASSERT_EQ(DwarfFormat::DWARF64, Header.getFormat());2262 ASSERT_EQ(0x1122334455667788ULL, Header.getLength());2263 ASSERT_EQ(5, Header.getVersion());2264 ASSERT_EQ(DW_UT_compile, Header.getUnitType());2265 ASSERT_EQ(4, Header.getAddressByteSize());2266 2267 // Check that the length can be correctly read in the unit class.2268 DWARFUnitVector DummyUnitVector;2269 DWARFSection DummySec;2270 DWARFCompileUnit CU(*Context, Sec, Header, /* DA = */ 0, /* RS = */ 0,2271 /* LocSection = */ 0, /* SS = */ StringRef(),2272 /* SOS = */ DummySec, /* AOS = */ 0,2273 /* LS = */ DummySec, /* LE = */ true,2274 /* isDWO= */ false, DummyUnitVector);2275 ASSERT_EQ(0x1122334455667788ULL, CU.getLength());2276 });2277}2278 2279} // end anonymous namespace2280