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