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