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