blob: 3a418f8a985accc177943458707fd88e77206ea2 [file] [log] [blame]
Greg Clayton3462a422016-12-08 01:03:48 +00001//===- 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 Jasper0f778692016-12-08 12:45:29 +000010#include "DwarfGenerator.h"
Greg Clayton3462a422016-12-08 01:03:48 +000011#include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
12#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000013#include "llvm/DebugInfo/DWARF/DWARFDie.h"
Greg Clayton3462a422016-12-08 01:03:48 +000014#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
15#include "llvm/DebugInfo/DWARF/DWARFUnit.h"
16#include "llvm/Support/Dwarf.h"
17#include "llvm/Support/Host.h"
18#include "llvm/Support/TargetSelect.h"
19#include "gtest/gtest.h"
20#include <climits>
21
22using namespace llvm;
23using namespace dwarf;
24
25namespace {
26
27void initLLVMIfNeeded() {
28 static bool gInitialized = false;
29 if (!gInitialized) {
30 gInitialized = true;
31 InitializeAllTargets();
32 InitializeAllTargetMCs();
33 InitializeAllAsmPrinters();
34 InitializeAllAsmParsers();
35 }
36}
37
38Triple getHostTripleForAddrSize(uint8_t AddrSize) {
39 Triple PT(Triple::normalize(LLVM_HOST_TRIPLE));
40
41 if (AddrSize == 8 && PT.isArch32Bit())
42 return PT.get64BitArchVariant();
43 if (AddrSize == 4 && PT.isArch64Bit())
44 return PT.get32BitArchVariant();
45 return PT;
46}
47
48/// Take any llvm::Expected and check and handle any errors.
49///
50/// \param Expected a llvm::Excepted instance to check.
51/// \returns true if there were errors, false otherwise.
52template <typename T>
53static bool HandleExpectedError(T &Expected) {
Greg Clayton3462a422016-12-08 01:03:48 +000054 std::string ErrorMsg;
55 handleAllErrors(Expected.takeError(), [&](const llvm::ErrorInfoBase &EI) {
56 ErrorMsg = EI.message();
57 });
Greg Claytonfd461fe2016-12-08 02:11:03 +000058 if (!ErrorMsg.empty()) {
59 ::testing::AssertionFailure() << "error: " << ErrorMsg;
60 return true;
61 }
62 return false;
Greg Clayton3462a422016-12-08 01:03:48 +000063}
64
65template <uint16_t Version, class AddrType, class RefAddrType>
66void TestAllForms() {
67 // Test that we can decode all DW_FORM values correctly.
68
69 const uint8_t AddrSize = sizeof(AddrType);
70 const AddrType AddrValue = (AddrType)0x0123456789abcdefULL;
71 const uint8_t BlockData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
72 const uint32_t BlockSize = sizeof(BlockData);
73 const RefAddrType RefAddr = 0x12345678;
74 const uint8_t Data1 = 0x01U;
75 const uint16_t Data2 = 0x2345U;
76 const uint32_t Data4 = 0x6789abcdU;
77 const uint64_t Data8 = 0x0011223344556677ULL;
78 const uint64_t Data8_2 = 0xAABBCCDDEEFF0011ULL;
79 const int64_t SData = INT64_MIN;
80 const uint64_t UData[] = {UINT64_MAX - 1, UINT64_MAX - 2, UINT64_MAX - 3,
81 UINT64_MAX - 4, UINT64_MAX - 5, UINT64_MAX - 6,
82 UINT64_MAX - 7, UINT64_MAX - 8, UINT64_MAX - 9};
83#define UDATA_1 18446744073709551614ULL
84 const uint32_t Dwarf32Values[] = {1, 2, 3, 4, 5, 6, 7, 8};
85 const char *StringValue = "Hello";
86 const char *StrpValue = "World";
87 initLLVMIfNeeded();
88 Triple Triple = getHostTripleForAddrSize(AddrSize);
89 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
90 if (HandleExpectedError(ExpectedDG))
91 return;
92 dwarfgen::Generator *DG = ExpectedDG.get().get();
93 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
94 dwarfgen::DIE CUDie = CU.getUnitDIE();
95 uint16_t Attr = DW_AT_lo_user;
96
97 //----------------------------------------------------------------------
98 // Test address forms
99 //----------------------------------------------------------------------
100 const auto Attr_DW_FORM_addr = static_cast<dwarf::Attribute>(Attr++);
101 CUDie.addAttribute(Attr_DW_FORM_addr, DW_FORM_addr, AddrValue);
102
103 //----------------------------------------------------------------------
104 // Test block forms
105 //----------------------------------------------------------------------
106 const auto Attr_DW_FORM_block = static_cast<dwarf::Attribute>(Attr++);
107 CUDie.addAttribute(Attr_DW_FORM_block, DW_FORM_block, BlockData, BlockSize);
108
109 const auto Attr_DW_FORM_block1 = static_cast<dwarf::Attribute>(Attr++);
110 CUDie.addAttribute(Attr_DW_FORM_block1, DW_FORM_block1, BlockData, BlockSize);
111
112 const auto Attr_DW_FORM_block2 = static_cast<dwarf::Attribute>(Attr++);
113 CUDie.addAttribute(Attr_DW_FORM_block2, DW_FORM_block2, BlockData, BlockSize);
114
115 const auto Attr_DW_FORM_block4 = static_cast<dwarf::Attribute>(Attr++);
116 CUDie.addAttribute(Attr_DW_FORM_block4, DW_FORM_block4, BlockData, BlockSize);
117
118 //----------------------------------------------------------------------
119 // Test data forms
120 //----------------------------------------------------------------------
121 const auto Attr_DW_FORM_data1 = static_cast<dwarf::Attribute>(Attr++);
122 CUDie.addAttribute(Attr_DW_FORM_data1, DW_FORM_data1, Data1);
123
124 const auto Attr_DW_FORM_data2 = static_cast<dwarf::Attribute>(Attr++);
125 CUDie.addAttribute(Attr_DW_FORM_data2, DW_FORM_data2, Data2);
126
127 const auto Attr_DW_FORM_data4 = static_cast<dwarf::Attribute>(Attr++);
128 CUDie.addAttribute(Attr_DW_FORM_data4, DW_FORM_data4, Data4);
129
130 const auto Attr_DW_FORM_data8 = static_cast<dwarf::Attribute>(Attr++);
131 CUDie.addAttribute(Attr_DW_FORM_data8, DW_FORM_data8, Data8);
132
133 //----------------------------------------------------------------------
134 // Test string forms
135 //----------------------------------------------------------------------
136 const auto Attr_DW_FORM_string = static_cast<dwarf::Attribute>(Attr++);
137 CUDie.addAttribute(Attr_DW_FORM_string, DW_FORM_string, StringValue);
138
139 const auto Attr_DW_FORM_strp = static_cast<dwarf::Attribute>(Attr++);
140 CUDie.addAttribute(Attr_DW_FORM_strp, DW_FORM_strp, StrpValue);
141
142 //----------------------------------------------------------------------
143 // Test reference forms
144 //----------------------------------------------------------------------
145 const auto Attr_DW_FORM_ref_addr = static_cast<dwarf::Attribute>(Attr++);
146 CUDie.addAttribute(Attr_DW_FORM_ref_addr, DW_FORM_ref_addr, RefAddr);
147
148 const auto Attr_DW_FORM_ref1 = static_cast<dwarf::Attribute>(Attr++);
149 CUDie.addAttribute(Attr_DW_FORM_ref1, DW_FORM_ref1, Data1);
150
151 const auto Attr_DW_FORM_ref2 = static_cast<dwarf::Attribute>(Attr++);
152 CUDie.addAttribute(Attr_DW_FORM_ref2, DW_FORM_ref2, Data2);
153
154 const auto Attr_DW_FORM_ref4 = static_cast<dwarf::Attribute>(Attr++);
155 CUDie.addAttribute(Attr_DW_FORM_ref4, DW_FORM_ref4, Data4);
156
157 const auto Attr_DW_FORM_ref8 = static_cast<dwarf::Attribute>(Attr++);
158 CUDie.addAttribute(Attr_DW_FORM_ref8, DW_FORM_ref8, Data8);
159
160 const auto Attr_DW_FORM_ref_sig8 = static_cast<dwarf::Attribute>(Attr++);
161 CUDie.addAttribute(Attr_DW_FORM_ref_sig8, DW_FORM_ref_sig8, Data8_2);
162
163 const auto Attr_DW_FORM_ref_udata = static_cast<dwarf::Attribute>(Attr++);
164 CUDie.addAttribute(Attr_DW_FORM_ref_udata, DW_FORM_ref_udata, UData[0]);
165
166 //----------------------------------------------------------------------
167 // Test flag forms
168 //----------------------------------------------------------------------
169 const auto Attr_DW_FORM_flag_true = static_cast<dwarf::Attribute>(Attr++);
170 CUDie.addAttribute(Attr_DW_FORM_flag_true, DW_FORM_flag, true);
171
172 const auto Attr_DW_FORM_flag_false = static_cast<dwarf::Attribute>(Attr++);
173 CUDie.addAttribute(Attr_DW_FORM_flag_false, DW_FORM_flag, false);
174
175 const auto Attr_DW_FORM_flag_present = static_cast<dwarf::Attribute>(Attr++);
176 CUDie.addAttribute(Attr_DW_FORM_flag_present, DW_FORM_flag_present);
177
178 //----------------------------------------------------------------------
179 // Test SLEB128 based forms
180 //----------------------------------------------------------------------
181 const auto Attr_DW_FORM_sdata = static_cast<dwarf::Attribute>(Attr++);
182 CUDie.addAttribute(Attr_DW_FORM_sdata, DW_FORM_sdata, SData);
183
184 //----------------------------------------------------------------------
185 // Test ULEB128 based forms
186 //----------------------------------------------------------------------
187 const auto Attr_DW_FORM_udata = static_cast<dwarf::Attribute>(Attr++);
188 CUDie.addAttribute(Attr_DW_FORM_udata, DW_FORM_udata, UData[0]);
189
190 //----------------------------------------------------------------------
191 // Test DWARF32/DWARF64 forms
192 //----------------------------------------------------------------------
193 const auto Attr_DW_FORM_GNU_ref_alt = static_cast<dwarf::Attribute>(Attr++);
194 CUDie.addAttribute(Attr_DW_FORM_GNU_ref_alt, DW_FORM_GNU_ref_alt,
195 Dwarf32Values[0]);
196
197 const auto Attr_DW_FORM_sec_offset = static_cast<dwarf::Attribute>(Attr++);
198 CUDie.addAttribute(Attr_DW_FORM_sec_offset, DW_FORM_sec_offset,
199 Dwarf32Values[1]);
200
201 //----------------------------------------------------------------------
202 // Add an address at the end to make sure we can decode this value
203 //----------------------------------------------------------------------
204 const auto Attr_Last = static_cast<dwarf::Attribute>(Attr++);
205 CUDie.addAttribute(Attr_Last, DW_FORM_addr, AddrValue);
206
207 //----------------------------------------------------------------------
208 // Generate the DWARF
209 //----------------------------------------------------------------------
210 StringRef FileBytes = DG->generate();
211 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
212 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
213 EXPECT_TRUE((bool)Obj);
214 DWARFContextInMemory DwarfContext(*Obj.get());
215 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
216 EXPECT_EQ(NumCUs, 1u);
217 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
Greg Claytonc8c10322016-12-13 18:25:19 +0000218 auto DieDG = U->getUnitDIE(false);
219 EXPECT_TRUE(DieDG.isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000220
221 //----------------------------------------------------------------------
222 // Test address forms
223 //----------------------------------------------------------------------
Greg Claytonc8c10322016-12-13 18:25:19 +0000224 EXPECT_EQ(DieDG.getAttributeValueAsAddress(Attr_DW_FORM_addr, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000225 AddrValue);
226
227 //----------------------------------------------------------------------
228 // Test block forms
229 //----------------------------------------------------------------------
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000230 Optional<DWARFFormValue> FormValue;
Greg Clayton3462a422016-12-08 01:03:48 +0000231 ArrayRef<uint8_t> ExtractedBlockData;
232 Optional<ArrayRef<uint8_t>> BlockDataOpt;
233
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000234 FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block);
235 EXPECT_TRUE((bool)FormValue);
236 BlockDataOpt = FormValue->getAsBlock();
Greg Clayton3462a422016-12-08 01:03:48 +0000237 EXPECT_TRUE(BlockDataOpt.hasValue());
238 ExtractedBlockData = BlockDataOpt.getValue();
239 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
240 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
241
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000242 FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block1);
243 EXPECT_TRUE((bool)FormValue);
244 BlockDataOpt = FormValue->getAsBlock();
Greg Clayton3462a422016-12-08 01:03:48 +0000245 EXPECT_TRUE(BlockDataOpt.hasValue());
246 ExtractedBlockData = BlockDataOpt.getValue();
247 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
248 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
249
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000250 FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block2);
251 EXPECT_TRUE((bool)FormValue);
252 BlockDataOpt = FormValue->getAsBlock();
Greg Clayton3462a422016-12-08 01:03:48 +0000253 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 Clayton1cbf3fa2016-12-13 23:20:56 +0000258 FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block4);
259 EXPECT_TRUE((bool)FormValue);
260 BlockDataOpt = FormValue->getAsBlock();
Greg Clayton3462a422016-12-08 01:03:48 +0000261 EXPECT_TRUE(BlockDataOpt.hasValue());
262 ExtractedBlockData = BlockDataOpt.getValue();
263 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
264 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
265
266 //----------------------------------------------------------------------
267 // Test data forms
268 //----------------------------------------------------------------------
269 EXPECT_EQ(
Greg Claytonc8c10322016-12-13 18:25:19 +0000270 DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data1, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000271 Data1);
272 EXPECT_EQ(
Greg Claytonc8c10322016-12-13 18:25:19 +0000273 DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data2, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000274 Data2);
275 EXPECT_EQ(
Greg Claytonc8c10322016-12-13 18:25:19 +0000276 DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data4, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000277 Data4);
278 EXPECT_EQ(
Greg Claytonc8c10322016-12-13 18:25:19 +0000279 DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data8, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000280 Data8);
281
282 //----------------------------------------------------------------------
283 // Test string forms
284 //----------------------------------------------------------------------
285 const char *ExtractedStringValue =
Greg Claytonc8c10322016-12-13 18:25:19 +0000286 DieDG.getAttributeValueAsString(Attr_DW_FORM_string, nullptr);
Greg Clayton3462a422016-12-08 01:03:48 +0000287 EXPECT_TRUE(ExtractedStringValue != nullptr);
288 EXPECT_TRUE(strcmp(StringValue, ExtractedStringValue) == 0);
289
290 const char *ExtractedStrpValue =
Greg Claytonc8c10322016-12-13 18:25:19 +0000291 DieDG.getAttributeValueAsString(Attr_DW_FORM_strp, nullptr);
Greg Clayton3462a422016-12-08 01:03:48 +0000292 EXPECT_TRUE(ExtractedStrpValue != nullptr);
293 EXPECT_TRUE(strcmp(StrpValue, ExtractedStrpValue) == 0);
294
295 //----------------------------------------------------------------------
296 // Test reference forms
297 //----------------------------------------------------------------------
Greg Claytonc8c10322016-12-13 18:25:19 +0000298 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_addr, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000299 RefAddr);
Greg Claytonc8c10322016-12-13 18:25:19 +0000300 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref1, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000301 Data1);
Greg Claytonc8c10322016-12-13 18:25:19 +0000302 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref2, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000303 Data2);
Greg Claytonc8c10322016-12-13 18:25:19 +0000304 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref4, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000305 Data4);
Greg Claytonc8c10322016-12-13 18:25:19 +0000306 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref8, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000307 Data8);
Greg Claytonc8c10322016-12-13 18:25:19 +0000308 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_sig8, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000309 Data8_2);
Greg Claytonc8c10322016-12-13 18:25:19 +0000310 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_udata, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000311 UData[0]);
312
313 //----------------------------------------------------------------------
314 // Test flag forms
315 //----------------------------------------------------------------------
Greg Claytonc8c10322016-12-13 18:25:19 +0000316 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(
317 Attr_DW_FORM_flag_true, 0ULL),
Greg Clayton3462a422016-12-08 01:03:48 +0000318 1ULL);
Greg Claytonc8c10322016-12-13 18:25:19 +0000319 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(
320 Attr_DW_FORM_flag_false, 1ULL),
Greg Clayton3462a422016-12-08 01:03:48 +0000321 0ULL);
Greg Claytonc8c10322016-12-13 18:25:19 +0000322 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(
323 Attr_DW_FORM_flag_present, 0ULL),
Greg Clayton3462a422016-12-08 01:03:48 +0000324 1ULL);
325
326 // TODO: test Attr_DW_FORM_implicit_const extraction
327
328 //----------------------------------------------------------------------
329 // Test SLEB128 based forms
330 //----------------------------------------------------------------------
Greg Claytonc8c10322016-12-13 18:25:19 +0000331 EXPECT_EQ(DieDG.getAttributeValueAsSignedConstant(Attr_DW_FORM_sdata, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000332 SData);
333
334 //----------------------------------------------------------------------
335 // Test ULEB128 based forms
336 //----------------------------------------------------------------------
337 EXPECT_EQ(
Greg Claytonc8c10322016-12-13 18:25:19 +0000338 DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_udata, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000339 UData[0]);
340
341 //----------------------------------------------------------------------
342 // Test DWARF32/DWARF64 forms
343 //----------------------------------------------------------------------
344 EXPECT_EQ(
Greg Claytonc8c10322016-12-13 18:25:19 +0000345 DieDG.getAttributeValueAsReference(Attr_DW_FORM_GNU_ref_alt, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000346 Dwarf32Values[0]);
347 EXPECT_EQ(
Greg Claytonc8c10322016-12-13 18:25:19 +0000348 DieDG.getAttributeValueAsSectionOffset(Attr_DW_FORM_sec_offset, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000349 Dwarf32Values[1]);
350
351 //----------------------------------------------------------------------
352 // Add an address at the end to make sure we can decode this value
353 //----------------------------------------------------------------------
Greg Claytonc8c10322016-12-13 18:25:19 +0000354 EXPECT_EQ(DieDG.getAttributeValueAsAddress(Attr_Last, 0), AddrValue);
Greg Clayton3462a422016-12-08 01:03:48 +0000355}
356
357TEST(DWARFDebugInfo, TestDWARF32Version2Addr4AllForms) {
358 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
359 // addresses.
360 typedef uint32_t AddrType;
361 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2.
362 typedef AddrType RefAddrType;
363 TestAllForms<2, AddrType, RefAddrType>();
364}
365
366TEST(DWARFDebugInfo, TestDWARF32Version2Addr8AllForms) {
367 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
368 // addresses.
369 typedef uint64_t AddrType;
370 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2.
371 typedef AddrType RefAddrType;
372 TestAllForms<2, AddrType, RefAddrType>();
373}
374
375TEST(DWARFDebugInfo, TestDWARF32Version3Addr4AllForms) {
376 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
377 // addresses.
378 typedef uint32_t AddrType;
379 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later.
380 typedef uint32_t RefAddrType;
381 TestAllForms<3, AddrType, RefAddrType>();
382}
383
384TEST(DWARFDebugInfo, TestDWARF32Version3Addr8AllForms) {
385 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
386 // addresses.
387 typedef uint64_t AddrType;
388 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
389 typedef uint32_t RefAddrType;
390 TestAllForms<3, AddrType, RefAddrType>();
391}
392
393TEST(DWARFDebugInfo, TestDWARF32Version4Addr4AllForms) {
394 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
395 // addresses.
396 typedef uint32_t AddrType;
397 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
398 typedef uint32_t RefAddrType;
399 TestAllForms<4, AddrType, RefAddrType>();
400}
401
402TEST(DWARFDebugInfo, TestDWARF32Version4Addr8AllForms) {
403 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
404 // addresses.
405 typedef uint64_t AddrType;
406 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
407 typedef uint32_t RefAddrType;
408 TestAllForms<4, AddrType, RefAddrType>();
409}
410
411template <uint16_t Version, class AddrType> void TestChildren() {
412 // Test that we can decode DW_FORM_ref_addr values correctly in DWARF 2 with
413 // 4 byte addresses. DW_FORM_ref_addr values should be 4 bytes when using
414 // 8 byte addresses.
415
416 const uint8_t AddrSize = sizeof(AddrType);
417 initLLVMIfNeeded();
418 Triple Triple = getHostTripleForAddrSize(AddrSize);
419 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
420 if (HandleExpectedError(ExpectedDG))
421 return;
422 dwarfgen::Generator *DG = ExpectedDG.get().get();
423 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
424 dwarfgen::DIE CUDie = CU.getUnitDIE();
425
426 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
427 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
428
429 dwarfgen::DIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram);
430 SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main");
431 SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U);
432 SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U);
433
434 dwarfgen::DIE IntDie = CUDie.addChild(DW_TAG_base_type);
435 IntDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");
436 IntDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
437 IntDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
438
439 dwarfgen::DIE ArgcDie = SubprogramDie.addChild(DW_TAG_formal_parameter);
440 ArgcDie.addAttribute(DW_AT_name, DW_FORM_strp, "argc");
441 // ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref4, IntDie);
442 ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, IntDie);
443
444 StringRef FileBytes = DG->generate();
445 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
446 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
447 EXPECT_TRUE((bool)Obj);
448 DWARFContextInMemory DwarfContext(*Obj.get());
449
450 // Verify the number of compile units is correct.
451 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
452 EXPECT_EQ(NumCUs, 1u);
453 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
454
455 // Get the compile unit DIE is valid.
Greg Claytonc8c10322016-12-13 18:25:19 +0000456 auto DieDG = U->getUnitDIE(false);
457 EXPECT_TRUE(DieDG.isValid());
458 // DieDG.dump(llvm::outs(), U, UINT32_MAX);
Greg Clayton3462a422016-12-08 01:03:48 +0000459
460 // Verify the first child of the compile unit DIE is our subprogram.
Greg Claytonc8c10322016-12-13 18:25:19 +0000461 auto SubprogramDieDG = DieDG.getFirstChild();
462 EXPECT_TRUE(SubprogramDieDG.isValid());
463 EXPECT_EQ(SubprogramDieDG.getTag(), DW_TAG_subprogram);
Greg Clayton3462a422016-12-08 01:03:48 +0000464
465 // Verify the first child of the subprogram is our formal parameter.
Greg Claytonc8c10322016-12-13 18:25:19 +0000466 auto ArgcDieDG = SubprogramDieDG.getFirstChild();
467 EXPECT_TRUE(ArgcDieDG.isValid());
468 EXPECT_EQ(ArgcDieDG.getTag(), DW_TAG_formal_parameter);
Greg Clayton3462a422016-12-08 01:03:48 +0000469
470 // Verify our formal parameter has a NULL tag sibling.
Greg Claytonc8c10322016-12-13 18:25:19 +0000471 auto NullDieDG = ArgcDieDG.getSibling();
472 EXPECT_TRUE(NullDieDG.isValid());
473 if (NullDieDG) {
474 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null);
475 EXPECT_TRUE(!NullDieDG.getSibling().isValid());
476 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000477 }
478
479 // Verify the sibling of our subprogram is our integer base type.
Greg Claytonc8c10322016-12-13 18:25:19 +0000480 auto IntDieDG = SubprogramDieDG.getSibling();
481 EXPECT_TRUE(IntDieDG.isValid());
482 EXPECT_EQ(IntDieDG.getTag(), DW_TAG_base_type);
Greg Clayton3462a422016-12-08 01:03:48 +0000483
484 // Verify the sibling of our subprogram is our integer base is a NULL tag.
Greg Claytonc8c10322016-12-13 18:25:19 +0000485 NullDieDG = IntDieDG.getSibling();
486 EXPECT_TRUE(NullDieDG.isValid());
487 if (NullDieDG) {
488 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null);
489 EXPECT_TRUE(!NullDieDG.getSibling().isValid());
490 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000491 }
492}
493
494TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Children) {
495 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
496 // addresses.
497 typedef uint32_t AddrType;
498 TestChildren<2, AddrType>();
499}
500
501TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Children) {
502 // Test that we can decode all forms for DWARF32, version 2, with 8 byte
503 // addresses.
504 typedef uint64_t AddrType;
505 TestChildren<2, AddrType>();
506}
507
508TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Children) {
509 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
510 // addresses.
511 typedef uint32_t AddrType;
512 TestChildren<3, AddrType>();
513}
514
515TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Children) {
516 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
517 // addresses.
518 typedef uint64_t AddrType;
519 TestChildren<3, AddrType>();
520}
521
522TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Children) {
523 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
524 // addresses.
525 typedef uint32_t AddrType;
526 TestChildren<4, AddrType>();
527}
528
529TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Children) {
530 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
531 // addresses.
532 typedef uint64_t AddrType;
533 TestChildren<4, AddrType>();
534}
535
536template <uint16_t Version, class AddrType> void TestReferences() {
537 // Test that we can decode DW_FORM_refXXX values correctly in DWARF.
538
539 const uint8_t AddrSize = sizeof(AddrType);
540 initLLVMIfNeeded();
541 Triple Triple = getHostTripleForAddrSize(AddrSize);
542 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
543 if (HandleExpectedError(ExpectedDG))
544 return;
545 dwarfgen::Generator *DG = ExpectedDG.get().get();
546 dwarfgen::CompileUnit &CU1 = DG->addCompileUnit();
547 dwarfgen::CompileUnit &CU2 = DG->addCompileUnit();
548
549 dwarfgen::DIE CU1Die = CU1.getUnitDIE();
550 CU1Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
551 CU1Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
552
553 dwarfgen::DIE CU1TypeDie = CU1Die.addChild(DW_TAG_base_type);
554 CU1TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");
555 CU1TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
556 CU1TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
557
558 dwarfgen::DIE CU1Ref1Die = CU1Die.addChild(DW_TAG_variable);
559 CU1Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref1");
560 CU1Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU1TypeDie);
561
562 dwarfgen::DIE CU1Ref2Die = CU1Die.addChild(DW_TAG_variable);
563 CU1Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref2");
564 CU1Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU1TypeDie);
565
566 dwarfgen::DIE CU1Ref4Die = CU1Die.addChild(DW_TAG_variable);
567 CU1Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref4");
568 CU1Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU1TypeDie);
569
570 dwarfgen::DIE CU1Ref8Die = CU1Die.addChild(DW_TAG_variable);
571 CU1Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref8");
572 CU1Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU1TypeDie);
573
574 dwarfgen::DIE CU1RefAddrDie = CU1Die.addChild(DW_TAG_variable);
575 CU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1RefAddr");
576 CU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie);
577
578 dwarfgen::DIE CU2Die = CU2.getUnitDIE();
579 CU2Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/foo.c");
580 CU2Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
581
582 dwarfgen::DIE CU2TypeDie = CU2Die.addChild(DW_TAG_base_type);
583 CU2TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "float");
584 CU2TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_float);
585 CU2TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
586
587 dwarfgen::DIE CU2Ref1Die = CU2Die.addChild(DW_TAG_variable);
588 CU2Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref1");
589 CU2Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU2TypeDie);
590
591 dwarfgen::DIE CU2Ref2Die = CU2Die.addChild(DW_TAG_variable);
592 CU2Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref2");
593 CU2Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU2TypeDie);
594
595 dwarfgen::DIE CU2Ref4Die = CU2Die.addChild(DW_TAG_variable);
596 CU2Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref4");
597 CU2Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU2TypeDie);
598
599 dwarfgen::DIE CU2Ref8Die = CU2Die.addChild(DW_TAG_variable);
600 CU2Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref8");
601 CU2Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU2TypeDie);
602
603 dwarfgen::DIE CU2RefAddrDie = CU2Die.addChild(DW_TAG_variable);
604 CU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2RefAddr");
605 CU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie);
606
607 // Refer to a type in CU1 from CU2
608 dwarfgen::DIE CU2ToCU1RefAddrDie = CU2Die.addChild(DW_TAG_variable);
609 CU2ToCU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2ToCU1RefAddr");
610 CU2ToCU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie);
611
612 // Refer to a type in CU2 from CU1
613 dwarfgen::DIE CU1ToCU2RefAddrDie = CU1Die.addChild(DW_TAG_variable);
614 CU1ToCU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1ToCU2RefAddr");
615 CU1ToCU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie);
616
617 StringRef FileBytes = DG->generate();
618 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
619 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
620 EXPECT_TRUE((bool)Obj);
621 DWARFContextInMemory DwarfContext(*Obj.get());
622
623 // Verify the number of compile units is correct.
624 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
625 EXPECT_EQ(NumCUs, 2u);
626 DWARFCompileUnit *U1 = DwarfContext.getCompileUnitAtIndex(0);
627 DWARFCompileUnit *U2 = DwarfContext.getCompileUnitAtIndex(1);
628
629 // Get the compile unit DIE is valid.
Greg Claytonc8c10322016-12-13 18:25:19 +0000630 auto Unit1DieDG = U1->getUnitDIE(false);
631 EXPECT_TRUE(Unit1DieDG.isValid());
632 // Unit1DieDG.dump(llvm::outs(), UINT32_MAX);
Greg Clayton3462a422016-12-08 01:03:48 +0000633
Greg Claytonc8c10322016-12-13 18:25:19 +0000634 auto Unit2DieDG = U2->getUnitDIE(false);
635 EXPECT_TRUE(Unit2DieDG.isValid());
636 // Unit2DieDG.dump(llvm::outs(), UINT32_MAX);
Greg Clayton3462a422016-12-08 01:03:48 +0000637
638 // Verify the first child of the compile unit 1 DIE is our int base type.
Greg Claytonc8c10322016-12-13 18:25:19 +0000639 auto CU1TypeDieDG = Unit1DieDG.getFirstChild();
640 EXPECT_TRUE(CU1TypeDieDG.isValid());
641 EXPECT_EQ(CU1TypeDieDG.getTag(), DW_TAG_base_type);
Greg Clayton3462a422016-12-08 01:03:48 +0000642 EXPECT_EQ(
Greg Claytonc8c10322016-12-13 18:25:19 +0000643 CU1TypeDieDG.getAttributeValueAsUnsignedConstant(DW_AT_encoding, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000644 DW_ATE_signed);
645
646 // Verify the first child of the compile unit 2 DIE is our float base type.
Greg Claytonc8c10322016-12-13 18:25:19 +0000647 auto CU2TypeDieDG = Unit2DieDG.getFirstChild();
648 EXPECT_TRUE(CU2TypeDieDG.isValid());
649 EXPECT_EQ(CU2TypeDieDG.getTag(), DW_TAG_base_type);
Greg Clayton3462a422016-12-08 01:03:48 +0000650 EXPECT_EQ(
Greg Claytonc8c10322016-12-13 18:25:19 +0000651 CU2TypeDieDG.getAttributeValueAsUnsignedConstant(DW_AT_encoding, 0),
Greg Clayton3462a422016-12-08 01:03:48 +0000652 DW_ATE_float);
653
654 // Verify the sibling of the base type DIE is our Ref1 DIE and that its
655 // DW_AT_type points to our base type DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000656 auto CU1Ref1DieDG = CU1TypeDieDG.getSibling();
657 EXPECT_TRUE(CU1Ref1DieDG.isValid());
658 EXPECT_EQ(CU1Ref1DieDG.getTag(), DW_TAG_variable);
659 EXPECT_EQ(CU1Ref1DieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
660 CU1TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000661 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our
662 // base type DIE in CU1.
Greg Claytonc8c10322016-12-13 18:25:19 +0000663 auto CU1Ref2DieDG = CU1Ref1DieDG.getSibling();
664 EXPECT_TRUE(CU1Ref2DieDG.isValid());
665 EXPECT_EQ(CU1Ref2DieDG.getTag(), DW_TAG_variable);
666 EXPECT_EQ(CU1Ref2DieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
667 CU1TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000668
669 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our
670 // base type DIE in CU1.
Greg Claytonc8c10322016-12-13 18:25:19 +0000671 auto CU1Ref4DieDG = CU1Ref2DieDG.getSibling();
672 EXPECT_TRUE(CU1Ref4DieDG.isValid());
673 EXPECT_EQ(CU1Ref4DieDG.getTag(), DW_TAG_variable);
674 EXPECT_EQ(CU1Ref4DieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
675 CU1TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000676
677 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our
678 // base type DIE in CU1.
Greg Claytonc8c10322016-12-13 18:25:19 +0000679 auto CU1Ref8DieDG = CU1Ref4DieDG.getSibling();
680 EXPECT_TRUE(CU1Ref8DieDG.isValid());
681 EXPECT_EQ(CU1Ref8DieDG.getTag(), DW_TAG_variable);
682 EXPECT_EQ(CU1Ref8DieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
683 CU1TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000684
685 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our
686 // base type DIE in CU1.
Greg Claytonc8c10322016-12-13 18:25:19 +0000687 auto CU1RefAddrDieDG = CU1Ref8DieDG.getSibling();
688 EXPECT_TRUE(CU1RefAddrDieDG.isValid());
689 EXPECT_EQ(CU1RefAddrDieDG.getTag(), DW_TAG_variable);
Greg Clayton3462a422016-12-08 01:03:48 +0000690 EXPECT_EQ(
Greg Claytonc8c10322016-12-13 18:25:19 +0000691 CU1RefAddrDieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
692 CU1TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000693
694 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its
695 // DW_AT_type points to our base type DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000696 auto CU1ToCU2RefAddrDieDG = CU1RefAddrDieDG.getSibling();
697 EXPECT_TRUE(CU1ToCU2RefAddrDieDG.isValid());
698 EXPECT_EQ(CU1ToCU2RefAddrDieDG.getTag(), DW_TAG_variable);
699 EXPECT_EQ(CU1ToCU2RefAddrDieDG.getAttributeValueAsReference(DW_AT_type,
Greg Clayton3462a422016-12-08 01:03:48 +0000700 -1ULL),
Greg Claytonc8c10322016-12-13 18:25:19 +0000701 CU2TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000702
703 // Verify the sibling of the base type DIE is our Ref1 DIE and that its
704 // DW_AT_type points to our base type DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000705 auto CU2Ref1DieDG = CU2TypeDieDG.getSibling();
706 EXPECT_TRUE(CU2Ref1DieDG.isValid());
707 EXPECT_EQ(CU2Ref1DieDG.getTag(), DW_TAG_variable);
708 EXPECT_EQ(CU2Ref1DieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
709 CU2TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000710 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our
711 // base type DIE in CU2.
Greg Claytonc8c10322016-12-13 18:25:19 +0000712 auto CU2Ref2DieDG = CU2Ref1DieDG.getSibling();
713 EXPECT_TRUE(CU2Ref2DieDG.isValid());
714 EXPECT_EQ(CU2Ref2DieDG.getTag(), DW_TAG_variable);
715 EXPECT_EQ(CU2Ref2DieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
716 CU2TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000717
718 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our
719 // base type DIE in CU2.
Greg Claytonc8c10322016-12-13 18:25:19 +0000720 auto CU2Ref4DieDG = CU2Ref2DieDG.getSibling();
721 EXPECT_TRUE(CU2Ref4DieDG.isValid());
722 EXPECT_EQ(CU2Ref4DieDG.getTag(), DW_TAG_variable);
723 EXPECT_EQ(CU2Ref4DieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
724 CU2TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000725
726 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our
727 // base type DIE in CU2.
Greg Claytonc8c10322016-12-13 18:25:19 +0000728 auto CU2Ref8DieDG = CU2Ref4DieDG.getSibling();
729 EXPECT_TRUE(CU2Ref8DieDG.isValid());
730 EXPECT_EQ(CU2Ref8DieDG.getTag(), DW_TAG_variable);
731 EXPECT_EQ(CU2Ref8DieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
732 CU2TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000733
734 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our
735 // base type DIE in CU2.
Greg Claytonc8c10322016-12-13 18:25:19 +0000736 auto CU2RefAddrDieDG = CU2Ref8DieDG.getSibling();
737 EXPECT_TRUE(CU2RefAddrDieDG.isValid());
738 EXPECT_EQ(CU2RefAddrDieDG.getTag(), DW_TAG_variable);
Greg Clayton3462a422016-12-08 01:03:48 +0000739 EXPECT_EQ(
Greg Claytonc8c10322016-12-13 18:25:19 +0000740 CU2RefAddrDieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
741 CU2TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000742
743 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its
744 // DW_AT_type points to our base type DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000745 auto CU2ToCU1RefAddrDieDG = CU2RefAddrDieDG.getSibling();
746 EXPECT_TRUE(CU2ToCU1RefAddrDieDG.isValid());
747 EXPECT_EQ(CU2ToCU1RefAddrDieDG.getTag(), DW_TAG_variable);
748 EXPECT_EQ(CU2ToCU1RefAddrDieDG.getAttributeValueAsReference(DW_AT_type,
Greg Clayton3462a422016-12-08 01:03:48 +0000749 -1ULL),
Greg Claytonc8c10322016-12-13 18:25:19 +0000750 CU1TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000751}
752
753TEST(DWARFDebugInfo, TestDWARF32Version2Addr4References) {
754 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
755 // addresses.
756 typedef uint32_t AddrType;
757 TestReferences<2, AddrType>();
758}
759
760TEST(DWARFDebugInfo, TestDWARF32Version2Addr8References) {
761 // Test that we can decode all forms for DWARF32, version 2, with 8 byte
762 // addresses.
763 typedef uint64_t AddrType;
764 TestReferences<2, AddrType>();
765}
766
767TEST(DWARFDebugInfo, TestDWARF32Version3Addr4References) {
768 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
769 // addresses.
770 typedef uint32_t AddrType;
771 TestReferences<3, AddrType>();
772}
773
774TEST(DWARFDebugInfo, TestDWARF32Version3Addr8References) {
775 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
776 // addresses.
777 typedef uint64_t AddrType;
778 TestReferences<3, AddrType>();
779}
780
781TEST(DWARFDebugInfo, TestDWARF32Version4Addr4References) {
782 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
783 // addresses.
784 typedef uint32_t AddrType;
785 TestReferences<4, AddrType>();
786}
787
788TEST(DWARFDebugInfo, TestDWARF32Version4Addr8References) {
789 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
790 // addresses.
791 typedef uint64_t AddrType;
792 TestReferences<4, AddrType>();
793}
794
795} // end anonymous namespace