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