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