blob: 723d56a43301f2563336082163f47c0484f5babb [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;
Victor Leschukcbddae72017-01-10 21:18:26 +000080 const int64_t ICSData = INT64_MAX; // DW_FORM_implicit_const SData
Greg Clayton3462a422016-12-08 01:03:48 +000081 const uint64_t UData[] = {UINT64_MAX - 1, UINT64_MAX - 2, UINT64_MAX - 3,
82 UINT64_MAX - 4, UINT64_MAX - 5, UINT64_MAX - 6,
83 UINT64_MAX - 7, UINT64_MAX - 8, UINT64_MAX - 9};
84#define UDATA_1 18446744073709551614ULL
85 const uint32_t Dwarf32Values[] = {1, 2, 3, 4, 5, 6, 7, 8};
86 const char *StringValue = "Hello";
87 const char *StrpValue = "World";
88 initLLVMIfNeeded();
89 Triple Triple = getHostTripleForAddrSize(AddrSize);
90 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
91 if (HandleExpectedError(ExpectedDG))
92 return;
93 dwarfgen::Generator *DG = ExpectedDG.get().get();
94 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
95 dwarfgen::DIE CUDie = CU.getUnitDIE();
96 uint16_t Attr = DW_AT_lo_user;
97
98 //----------------------------------------------------------------------
99 // Test address forms
100 //----------------------------------------------------------------------
101 const auto Attr_DW_FORM_addr = static_cast<dwarf::Attribute>(Attr++);
102 CUDie.addAttribute(Attr_DW_FORM_addr, DW_FORM_addr, AddrValue);
103
104 //----------------------------------------------------------------------
105 // Test block forms
106 //----------------------------------------------------------------------
107 const auto Attr_DW_FORM_block = static_cast<dwarf::Attribute>(Attr++);
108 CUDie.addAttribute(Attr_DW_FORM_block, DW_FORM_block, BlockData, BlockSize);
109
110 const auto Attr_DW_FORM_block1 = static_cast<dwarf::Attribute>(Attr++);
111 CUDie.addAttribute(Attr_DW_FORM_block1, DW_FORM_block1, BlockData, BlockSize);
112
113 const auto Attr_DW_FORM_block2 = static_cast<dwarf::Attribute>(Attr++);
114 CUDie.addAttribute(Attr_DW_FORM_block2, DW_FORM_block2, BlockData, BlockSize);
115
116 const auto Attr_DW_FORM_block4 = static_cast<dwarf::Attribute>(Attr++);
117 CUDie.addAttribute(Attr_DW_FORM_block4, DW_FORM_block4, BlockData, BlockSize);
118
119 //----------------------------------------------------------------------
120 // Test data forms
121 //----------------------------------------------------------------------
122 const auto Attr_DW_FORM_data1 = static_cast<dwarf::Attribute>(Attr++);
123 CUDie.addAttribute(Attr_DW_FORM_data1, DW_FORM_data1, Data1);
124
125 const auto Attr_DW_FORM_data2 = static_cast<dwarf::Attribute>(Attr++);
126 CUDie.addAttribute(Attr_DW_FORM_data2, DW_FORM_data2, Data2);
127
128 const auto Attr_DW_FORM_data4 = static_cast<dwarf::Attribute>(Attr++);
129 CUDie.addAttribute(Attr_DW_FORM_data4, DW_FORM_data4, Data4);
130
131 const auto Attr_DW_FORM_data8 = static_cast<dwarf::Attribute>(Attr++);
132 CUDie.addAttribute(Attr_DW_FORM_data8, DW_FORM_data8, Data8);
133
134 //----------------------------------------------------------------------
135 // Test string forms
136 //----------------------------------------------------------------------
137 const auto Attr_DW_FORM_string = static_cast<dwarf::Attribute>(Attr++);
138 CUDie.addAttribute(Attr_DW_FORM_string, DW_FORM_string, StringValue);
139
140 const auto Attr_DW_FORM_strp = static_cast<dwarf::Attribute>(Attr++);
141 CUDie.addAttribute(Attr_DW_FORM_strp, DW_FORM_strp, StrpValue);
142
143 //----------------------------------------------------------------------
144 // Test reference forms
145 //----------------------------------------------------------------------
146 const auto Attr_DW_FORM_ref_addr = static_cast<dwarf::Attribute>(Attr++);
147 CUDie.addAttribute(Attr_DW_FORM_ref_addr, DW_FORM_ref_addr, RefAddr);
148
149 const auto Attr_DW_FORM_ref1 = static_cast<dwarf::Attribute>(Attr++);
150 CUDie.addAttribute(Attr_DW_FORM_ref1, DW_FORM_ref1, Data1);
151
152 const auto Attr_DW_FORM_ref2 = static_cast<dwarf::Attribute>(Attr++);
153 CUDie.addAttribute(Attr_DW_FORM_ref2, DW_FORM_ref2, Data2);
154
155 const auto Attr_DW_FORM_ref4 = static_cast<dwarf::Attribute>(Attr++);
156 CUDie.addAttribute(Attr_DW_FORM_ref4, DW_FORM_ref4, Data4);
157
158 const auto Attr_DW_FORM_ref8 = static_cast<dwarf::Attribute>(Attr++);
159 CUDie.addAttribute(Attr_DW_FORM_ref8, DW_FORM_ref8, Data8);
160
161 const auto Attr_DW_FORM_ref_sig8 = static_cast<dwarf::Attribute>(Attr++);
162 CUDie.addAttribute(Attr_DW_FORM_ref_sig8, DW_FORM_ref_sig8, Data8_2);
163
164 const auto Attr_DW_FORM_ref_udata = static_cast<dwarf::Attribute>(Attr++);
165 CUDie.addAttribute(Attr_DW_FORM_ref_udata, DW_FORM_ref_udata, UData[0]);
166
167 //----------------------------------------------------------------------
168 // Test flag forms
169 //----------------------------------------------------------------------
170 const auto Attr_DW_FORM_flag_true = static_cast<dwarf::Attribute>(Attr++);
171 CUDie.addAttribute(Attr_DW_FORM_flag_true, DW_FORM_flag, true);
172
173 const auto Attr_DW_FORM_flag_false = static_cast<dwarf::Attribute>(Attr++);
174 CUDie.addAttribute(Attr_DW_FORM_flag_false, DW_FORM_flag, false);
175
176 const auto Attr_DW_FORM_flag_present = static_cast<dwarf::Attribute>(Attr++);
177 CUDie.addAttribute(Attr_DW_FORM_flag_present, DW_FORM_flag_present);
178
179 //----------------------------------------------------------------------
180 // Test SLEB128 based forms
181 //----------------------------------------------------------------------
182 const auto Attr_DW_FORM_sdata = static_cast<dwarf::Attribute>(Attr++);
183 CUDie.addAttribute(Attr_DW_FORM_sdata, DW_FORM_sdata, SData);
184
Victor Leschukcbddae72017-01-10 21:18:26 +0000185 const auto Attr_DW_FORM_implicit_const =
186 static_cast<dwarf::Attribute>(Attr++);
187 if (Version >= 5)
188 CUDie.addAttribute(Attr_DW_FORM_implicit_const, DW_FORM_implicit_const,
189 ICSData);
190
Greg Clayton3462a422016-12-08 01:03:48 +0000191 //----------------------------------------------------------------------
192 // Test ULEB128 based forms
193 //----------------------------------------------------------------------
194 const auto Attr_DW_FORM_udata = static_cast<dwarf::Attribute>(Attr++);
195 CUDie.addAttribute(Attr_DW_FORM_udata, DW_FORM_udata, UData[0]);
196
197 //----------------------------------------------------------------------
198 // Test DWARF32/DWARF64 forms
199 //----------------------------------------------------------------------
200 const auto Attr_DW_FORM_GNU_ref_alt = static_cast<dwarf::Attribute>(Attr++);
201 CUDie.addAttribute(Attr_DW_FORM_GNU_ref_alt, DW_FORM_GNU_ref_alt,
202 Dwarf32Values[0]);
203
204 const auto Attr_DW_FORM_sec_offset = static_cast<dwarf::Attribute>(Attr++);
205 CUDie.addAttribute(Attr_DW_FORM_sec_offset, DW_FORM_sec_offset,
206 Dwarf32Values[1]);
207
208 //----------------------------------------------------------------------
209 // Add an address at the end to make sure we can decode this value
210 //----------------------------------------------------------------------
211 const auto Attr_Last = static_cast<dwarf::Attribute>(Attr++);
212 CUDie.addAttribute(Attr_Last, DW_FORM_addr, AddrValue);
213
214 //----------------------------------------------------------------------
215 // Generate the DWARF
216 //----------------------------------------------------------------------
217 StringRef FileBytes = DG->generate();
218 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
219 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
220 EXPECT_TRUE((bool)Obj);
221 DWARFContextInMemory DwarfContext(*Obj.get());
222 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
223 EXPECT_EQ(NumCUs, 1u);
224 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
Greg Claytonc8c10322016-12-13 18:25:19 +0000225 auto DieDG = U->getUnitDIE(false);
226 EXPECT_TRUE(DieDG.isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000227
228 //----------------------------------------------------------------------
229 // Test address forms
230 //----------------------------------------------------------------------
Greg Claytond1efea82017-01-11 17:43:37 +0000231 EXPECT_EQ(DieDG.getAttributeValueAsAddress(Attr_DW_FORM_addr).getValueOr(0),
Greg Clayton3462a422016-12-08 01:03:48 +0000232 AddrValue);
233
234 //----------------------------------------------------------------------
235 // Test block forms
236 //----------------------------------------------------------------------
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000237 Optional<DWARFFormValue> FormValue;
Greg Clayton3462a422016-12-08 01:03:48 +0000238 ArrayRef<uint8_t> ExtractedBlockData;
239 Optional<ArrayRef<uint8_t>> BlockDataOpt;
240
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000241 FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block);
242 EXPECT_TRUE((bool)FormValue);
243 BlockDataOpt = FormValue->getAsBlock();
Greg Clayton3462a422016-12-08 01:03:48 +0000244 EXPECT_TRUE(BlockDataOpt.hasValue());
245 ExtractedBlockData = BlockDataOpt.getValue();
246 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
247 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
248
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000249 FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block1);
250 EXPECT_TRUE((bool)FormValue);
251 BlockDataOpt = FormValue->getAsBlock();
Greg Clayton3462a422016-12-08 01:03:48 +0000252 EXPECT_TRUE(BlockDataOpt.hasValue());
253 ExtractedBlockData = BlockDataOpt.getValue();
254 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
255 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
256
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000257 FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block2);
258 EXPECT_TRUE((bool)FormValue);
259 BlockDataOpt = FormValue->getAsBlock();
Greg Clayton3462a422016-12-08 01:03:48 +0000260 EXPECT_TRUE(BlockDataOpt.hasValue());
261 ExtractedBlockData = BlockDataOpt.getValue();
262 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
263 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
264
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000265 FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block4);
266 EXPECT_TRUE((bool)FormValue);
267 BlockDataOpt = FormValue->getAsBlock();
Greg Clayton3462a422016-12-08 01:03:48 +0000268 EXPECT_TRUE(BlockDataOpt.hasValue());
269 ExtractedBlockData = BlockDataOpt.getValue();
270 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
271 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
272
273 //----------------------------------------------------------------------
274 // Test data forms
275 //----------------------------------------------------------------------
Greg Claytond1efea82017-01-11 17:43:37 +0000276 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data1)
277 .getValueOr(0),
278 Data1);
279 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data2)
280 .getValueOr(0),
281 Data2);
282 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data4)
283 .getValueOr(0),
284 Data4);
285 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data8)
286 .getValueOr(0),
287 Data8);
Greg Clayton3462a422016-12-08 01:03:48 +0000288
289 //----------------------------------------------------------------------
290 // Test string forms
291 //----------------------------------------------------------------------
292 const char *ExtractedStringValue =
Greg Claytonc8c10322016-12-13 18:25:19 +0000293 DieDG.getAttributeValueAsString(Attr_DW_FORM_string, nullptr);
Greg Clayton3462a422016-12-08 01:03:48 +0000294 EXPECT_TRUE(ExtractedStringValue != nullptr);
295 EXPECT_TRUE(strcmp(StringValue, ExtractedStringValue) == 0);
296
297 const char *ExtractedStrpValue =
Greg Claytonc8c10322016-12-13 18:25:19 +0000298 DieDG.getAttributeValueAsString(Attr_DW_FORM_strp, nullptr);
Greg Clayton3462a422016-12-08 01:03:48 +0000299 EXPECT_TRUE(ExtractedStrpValue != nullptr);
300 EXPECT_TRUE(strcmp(StrpValue, ExtractedStrpValue) == 0);
301
302 //----------------------------------------------------------------------
303 // Test reference forms
304 //----------------------------------------------------------------------
Greg Claytond1efea82017-01-11 17:43:37 +0000305 EXPECT_EQ(
306 DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_addr).getValueOr(0),
307 RefAddr);
308 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref1).getValueOr(0),
Greg Clayton3462a422016-12-08 01:03:48 +0000309 Data1);
Greg Claytond1efea82017-01-11 17:43:37 +0000310 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref2).getValueOr(0),
Greg Clayton3462a422016-12-08 01:03:48 +0000311 Data2);
Greg Claytond1efea82017-01-11 17:43:37 +0000312 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref4).getValueOr(0),
Greg Clayton3462a422016-12-08 01:03:48 +0000313 Data4);
Greg Claytond1efea82017-01-11 17:43:37 +0000314 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref8).getValueOr(0),
Greg Clayton3462a422016-12-08 01:03:48 +0000315 Data8);
Greg Claytond1efea82017-01-11 17:43:37 +0000316 EXPECT_EQ(
317 DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_sig8).getValueOr(0),
318 Data8_2);
319 EXPECT_EQ(
320 DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_udata).getValueOr(0),
321 UData[0]);
Greg Clayton3462a422016-12-08 01:03:48 +0000322
323 //----------------------------------------------------------------------
324 // Test flag forms
325 //----------------------------------------------------------------------
Greg Claytond1efea82017-01-11 17:43:37 +0000326 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_flag_true)
327 .getValueOr(0),
Greg Clayton3462a422016-12-08 01:03:48 +0000328 1ULL);
Greg Claytond1efea82017-01-11 17:43:37 +0000329 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_flag_false)
330 .getValueOr(1),
Greg Clayton3462a422016-12-08 01:03:48 +0000331 0ULL);
Greg Claytond1efea82017-01-11 17:43:37 +0000332 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_flag_present)
333 .getValueOr(0ULL),
Greg Clayton3462a422016-12-08 01:03:48 +0000334 1ULL);
335
Greg Clayton3462a422016-12-08 01:03:48 +0000336 //----------------------------------------------------------------------
337 // Test SLEB128 based forms
338 //----------------------------------------------------------------------
Greg Claytond1efea82017-01-11 17:43:37 +0000339 EXPECT_EQ(
340 DieDG.getAttributeValueAsSignedConstant(Attr_DW_FORM_sdata).getValueOr(0),
341 SData);
Victor Leschukcbddae72017-01-10 21:18:26 +0000342 if (Version >= 5)
Greg Claytond1efea82017-01-11 17:43:37 +0000343 EXPECT_EQ(
344 DieDG.getAttributeValueAsSignedConstant(Attr_DW_FORM_implicit_const)
345 .getValueOr(0),
346 ICSData);
Greg Clayton3462a422016-12-08 01:03:48 +0000347
348 //----------------------------------------------------------------------
349 // Test ULEB128 based forms
350 //----------------------------------------------------------------------
Greg Claytond1efea82017-01-11 17:43:37 +0000351 EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_udata)
352 .getValueOr(0),
353 UData[0]);
Greg Clayton3462a422016-12-08 01:03:48 +0000354
355 //----------------------------------------------------------------------
356 // Test DWARF32/DWARF64 forms
357 //----------------------------------------------------------------------
Greg Claytond1efea82017-01-11 17:43:37 +0000358 EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_GNU_ref_alt)
359 .getValueOr(0),
360 Dwarf32Values[0]);
361 EXPECT_EQ(DieDG.getAttributeValueAsSectionOffset(Attr_DW_FORM_sec_offset)
362 .getValueOr(0),
363 Dwarf32Values[1]);
Greg Clayton3462a422016-12-08 01:03:48 +0000364
365 //----------------------------------------------------------------------
366 // Add an address at the end to make sure we can decode this value
367 //----------------------------------------------------------------------
Greg Claytond1efea82017-01-11 17:43:37 +0000368 EXPECT_EQ(DieDG.getAttributeValueAsAddress(Attr_Last).getValueOr(0),
369 AddrValue);
Greg Clayton3462a422016-12-08 01:03:48 +0000370}
371
372TEST(DWARFDebugInfo, TestDWARF32Version2Addr4AllForms) {
373 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
374 // addresses.
375 typedef uint32_t AddrType;
376 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2.
377 typedef AddrType RefAddrType;
378 TestAllForms<2, AddrType, RefAddrType>();
379}
380
381TEST(DWARFDebugInfo, TestDWARF32Version2Addr8AllForms) {
382 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
383 // addresses.
384 typedef uint64_t AddrType;
385 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2.
386 typedef AddrType RefAddrType;
387 TestAllForms<2, AddrType, RefAddrType>();
388}
389
390TEST(DWARFDebugInfo, TestDWARF32Version3Addr4AllForms) {
391 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
392 // addresses.
393 typedef uint32_t AddrType;
394 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later.
395 typedef uint32_t RefAddrType;
396 TestAllForms<3, AddrType, RefAddrType>();
397}
398
399TEST(DWARFDebugInfo, TestDWARF32Version3Addr8AllForms) {
400 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
401 // addresses.
402 typedef uint64_t AddrType;
403 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
404 typedef uint32_t RefAddrType;
405 TestAllForms<3, AddrType, RefAddrType>();
406}
407
408TEST(DWARFDebugInfo, TestDWARF32Version4Addr4AllForms) {
409 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
410 // addresses.
411 typedef uint32_t AddrType;
412 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
413 typedef uint32_t RefAddrType;
414 TestAllForms<4, AddrType, RefAddrType>();
415}
416
417TEST(DWARFDebugInfo, TestDWARF32Version4Addr8AllForms) {
418 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
419 // addresses.
420 typedef uint64_t AddrType;
421 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
422 typedef uint32_t RefAddrType;
423 TestAllForms<4, AddrType, RefAddrType>();
424}
425
Victor Leschukcbddae72017-01-10 21:18:26 +0000426TEST(DWARFDebugInfo, TestDWARF32Version5Addr4AllForms) {
427 // Test that we can decode all forms for DWARF32, version 5, with 4 byte
428 // addresses.
429 typedef uint32_t AddrType;
430 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
431 typedef uint32_t RefAddrType;
432 TestAllForms<5, AddrType, RefAddrType>();
433}
434
435TEST(DWARFDebugInfo, TestDWARF32Version5Addr8AllForms) {
436 // Test that we can decode all forms for DWARF32, version 5, with 8 byte
437 // addresses.
438 typedef uint64_t AddrType;
439 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
440 typedef uint32_t RefAddrType;
441 TestAllForms<5, AddrType, RefAddrType>();
442}
443
Greg Clayton3462a422016-12-08 01:03:48 +0000444template <uint16_t Version, class AddrType> void TestChildren() {
445 // Test that we can decode DW_FORM_ref_addr values correctly in DWARF 2 with
446 // 4 byte addresses. DW_FORM_ref_addr values should be 4 bytes when using
447 // 8 byte addresses.
448
449 const uint8_t AddrSize = sizeof(AddrType);
450 initLLVMIfNeeded();
451 Triple Triple = getHostTripleForAddrSize(AddrSize);
452 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
453 if (HandleExpectedError(ExpectedDG))
454 return;
455 dwarfgen::Generator *DG = ExpectedDG.get().get();
456 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
457 dwarfgen::DIE CUDie = CU.getUnitDIE();
458
459 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
460 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
461
462 dwarfgen::DIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram);
463 SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main");
464 SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U);
465 SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U);
466
467 dwarfgen::DIE IntDie = CUDie.addChild(DW_TAG_base_type);
468 IntDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");
469 IntDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
470 IntDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
471
472 dwarfgen::DIE ArgcDie = SubprogramDie.addChild(DW_TAG_formal_parameter);
473 ArgcDie.addAttribute(DW_AT_name, DW_FORM_strp, "argc");
474 // ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref4, IntDie);
475 ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, IntDie);
476
477 StringRef FileBytes = DG->generate();
478 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
479 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
480 EXPECT_TRUE((bool)Obj);
481 DWARFContextInMemory DwarfContext(*Obj.get());
482
483 // Verify the number of compile units is correct.
484 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
485 EXPECT_EQ(NumCUs, 1u);
486 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
487
488 // Get the compile unit DIE is valid.
Greg Claytonc8c10322016-12-13 18:25:19 +0000489 auto DieDG = U->getUnitDIE(false);
490 EXPECT_TRUE(DieDG.isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000491
492 // Verify the first child of the compile unit DIE is our subprogram.
Greg Claytonc8c10322016-12-13 18:25:19 +0000493 auto SubprogramDieDG = DieDG.getFirstChild();
494 EXPECT_TRUE(SubprogramDieDG.isValid());
495 EXPECT_EQ(SubprogramDieDG.getTag(), DW_TAG_subprogram);
Greg Clayton3462a422016-12-08 01:03:48 +0000496
497 // Verify the first child of the subprogram is our formal parameter.
Greg Claytonc8c10322016-12-13 18:25:19 +0000498 auto ArgcDieDG = SubprogramDieDG.getFirstChild();
499 EXPECT_TRUE(ArgcDieDG.isValid());
500 EXPECT_EQ(ArgcDieDG.getTag(), DW_TAG_formal_parameter);
Greg Clayton3462a422016-12-08 01:03:48 +0000501
502 // Verify our formal parameter has a NULL tag sibling.
Greg Claytonc8c10322016-12-13 18:25:19 +0000503 auto NullDieDG = ArgcDieDG.getSibling();
504 EXPECT_TRUE(NullDieDG.isValid());
505 if (NullDieDG) {
506 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null);
507 EXPECT_TRUE(!NullDieDG.getSibling().isValid());
508 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000509 }
510
511 // Verify the sibling of our subprogram is our integer base type.
Greg Claytonc8c10322016-12-13 18:25:19 +0000512 auto IntDieDG = SubprogramDieDG.getSibling();
513 EXPECT_TRUE(IntDieDG.isValid());
514 EXPECT_EQ(IntDieDG.getTag(), DW_TAG_base_type);
Greg Clayton3462a422016-12-08 01:03:48 +0000515
516 // Verify the sibling of our subprogram is our integer base is a NULL tag.
Greg Claytonc8c10322016-12-13 18:25:19 +0000517 NullDieDG = IntDieDG.getSibling();
518 EXPECT_TRUE(NullDieDG.isValid());
519 if (NullDieDG) {
520 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null);
521 EXPECT_TRUE(!NullDieDG.getSibling().isValid());
522 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000523 }
524}
525
526TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Children) {
527 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
528 // addresses.
529 typedef uint32_t AddrType;
530 TestChildren<2, AddrType>();
531}
532
533TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Children) {
534 // Test that we can decode all forms for DWARF32, version 2, with 8 byte
535 // addresses.
536 typedef uint64_t AddrType;
537 TestChildren<2, AddrType>();
538}
539
540TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Children) {
541 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
542 // addresses.
543 typedef uint32_t AddrType;
544 TestChildren<3, AddrType>();
545}
546
547TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Children) {
548 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
549 // addresses.
550 typedef uint64_t AddrType;
551 TestChildren<3, AddrType>();
552}
553
554TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Children) {
555 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
556 // addresses.
557 typedef uint32_t AddrType;
558 TestChildren<4, AddrType>();
559}
560
561TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Children) {
562 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
563 // addresses.
564 typedef uint64_t AddrType;
565 TestChildren<4, AddrType>();
566}
567
568template <uint16_t Version, class AddrType> void TestReferences() {
569 // Test that we can decode DW_FORM_refXXX values correctly in DWARF.
570
571 const uint8_t AddrSize = sizeof(AddrType);
572 initLLVMIfNeeded();
573 Triple Triple = getHostTripleForAddrSize(AddrSize);
574 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
575 if (HandleExpectedError(ExpectedDG))
576 return;
577 dwarfgen::Generator *DG = ExpectedDG.get().get();
578 dwarfgen::CompileUnit &CU1 = DG->addCompileUnit();
579 dwarfgen::CompileUnit &CU2 = DG->addCompileUnit();
580
581 dwarfgen::DIE CU1Die = CU1.getUnitDIE();
582 CU1Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
583 CU1Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
584
585 dwarfgen::DIE CU1TypeDie = CU1Die.addChild(DW_TAG_base_type);
586 CU1TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");
587 CU1TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
588 CU1TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
589
590 dwarfgen::DIE CU1Ref1Die = CU1Die.addChild(DW_TAG_variable);
591 CU1Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref1");
592 CU1Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU1TypeDie);
593
594 dwarfgen::DIE CU1Ref2Die = CU1Die.addChild(DW_TAG_variable);
595 CU1Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref2");
596 CU1Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU1TypeDie);
597
598 dwarfgen::DIE CU1Ref4Die = CU1Die.addChild(DW_TAG_variable);
599 CU1Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref4");
600 CU1Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU1TypeDie);
601
602 dwarfgen::DIE CU1Ref8Die = CU1Die.addChild(DW_TAG_variable);
603 CU1Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref8");
604 CU1Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU1TypeDie);
605
606 dwarfgen::DIE CU1RefAddrDie = CU1Die.addChild(DW_TAG_variable);
607 CU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1RefAddr");
608 CU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie);
609
610 dwarfgen::DIE CU2Die = CU2.getUnitDIE();
611 CU2Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/foo.c");
612 CU2Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
613
614 dwarfgen::DIE CU2TypeDie = CU2Die.addChild(DW_TAG_base_type);
615 CU2TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "float");
616 CU2TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_float);
617 CU2TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
618
619 dwarfgen::DIE CU2Ref1Die = CU2Die.addChild(DW_TAG_variable);
620 CU2Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref1");
621 CU2Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU2TypeDie);
622
623 dwarfgen::DIE CU2Ref2Die = CU2Die.addChild(DW_TAG_variable);
624 CU2Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref2");
625 CU2Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU2TypeDie);
626
627 dwarfgen::DIE CU2Ref4Die = CU2Die.addChild(DW_TAG_variable);
628 CU2Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref4");
629 CU2Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU2TypeDie);
630
631 dwarfgen::DIE CU2Ref8Die = CU2Die.addChild(DW_TAG_variable);
632 CU2Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref8");
633 CU2Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU2TypeDie);
634
635 dwarfgen::DIE CU2RefAddrDie = CU2Die.addChild(DW_TAG_variable);
636 CU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2RefAddr");
637 CU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie);
638
639 // Refer to a type in CU1 from CU2
640 dwarfgen::DIE CU2ToCU1RefAddrDie = CU2Die.addChild(DW_TAG_variable);
641 CU2ToCU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2ToCU1RefAddr");
642 CU2ToCU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie);
643
644 // Refer to a type in CU2 from CU1
645 dwarfgen::DIE CU1ToCU2RefAddrDie = CU1Die.addChild(DW_TAG_variable);
646 CU1ToCU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1ToCU2RefAddr");
647 CU1ToCU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie);
648
649 StringRef FileBytes = DG->generate();
650 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
651 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
652 EXPECT_TRUE((bool)Obj);
653 DWARFContextInMemory DwarfContext(*Obj.get());
654
655 // Verify the number of compile units is correct.
656 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
657 EXPECT_EQ(NumCUs, 2u);
658 DWARFCompileUnit *U1 = DwarfContext.getCompileUnitAtIndex(0);
659 DWARFCompileUnit *U2 = DwarfContext.getCompileUnitAtIndex(1);
660
661 // Get the compile unit DIE is valid.
Greg Claytonc8c10322016-12-13 18:25:19 +0000662 auto Unit1DieDG = U1->getUnitDIE(false);
663 EXPECT_TRUE(Unit1DieDG.isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000664
Greg Claytonc8c10322016-12-13 18:25:19 +0000665 auto Unit2DieDG = U2->getUnitDIE(false);
666 EXPECT_TRUE(Unit2DieDG.isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000667
668 // Verify the first child of the compile unit 1 DIE is our int base type.
Greg Claytonc8c10322016-12-13 18:25:19 +0000669 auto CU1TypeDieDG = Unit1DieDG.getFirstChild();
670 EXPECT_TRUE(CU1TypeDieDG.isValid());
671 EXPECT_EQ(CU1TypeDieDG.getTag(), DW_TAG_base_type);
Greg Claytond1efea82017-01-11 17:43:37 +0000672 EXPECT_EQ(CU1TypeDieDG.getAttributeValueAsUnsignedConstant(DW_AT_encoding)
673 .getValueOr(0),
674 DW_ATE_signed);
Greg Clayton3462a422016-12-08 01:03:48 +0000675
676 // Verify the first child of the compile unit 2 DIE is our float base type.
Greg Claytonc8c10322016-12-13 18:25:19 +0000677 auto CU2TypeDieDG = Unit2DieDG.getFirstChild();
678 EXPECT_TRUE(CU2TypeDieDG.isValid());
679 EXPECT_EQ(CU2TypeDieDG.getTag(), DW_TAG_base_type);
Greg Claytond1efea82017-01-11 17:43:37 +0000680 EXPECT_EQ(CU2TypeDieDG.getAttributeValueAsUnsignedConstant(DW_AT_encoding)
681 .getValueOr(0),
682 DW_ATE_float);
Greg Clayton3462a422016-12-08 01:03:48 +0000683
684 // Verify the sibling of the base type DIE is our Ref1 DIE and that its
685 // DW_AT_type points to our base type DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000686 auto CU1Ref1DieDG = CU1TypeDieDG.getSibling();
687 EXPECT_TRUE(CU1Ref1DieDG.isValid());
688 EXPECT_EQ(CU1Ref1DieDG.getTag(), DW_TAG_variable);
Greg Claytond1efea82017-01-11 17:43:37 +0000689 EXPECT_EQ(
690 CU1Ref1DieDG.getAttributeValueAsReference(DW_AT_type).getValueOr(-1ULL),
691 CU1TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000692 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our
693 // base type DIE in CU1.
Greg Claytonc8c10322016-12-13 18:25:19 +0000694 auto CU1Ref2DieDG = CU1Ref1DieDG.getSibling();
695 EXPECT_TRUE(CU1Ref2DieDG.isValid());
696 EXPECT_EQ(CU1Ref2DieDG.getTag(), DW_TAG_variable);
Greg Claytond1efea82017-01-11 17:43:37 +0000697 EXPECT_EQ(
698 CU1Ref2DieDG.getAttributeValueAsReference(DW_AT_type).getValueOr(-1ULL),
699 CU1TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000700
701 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our
702 // base type DIE in CU1.
Greg Claytonc8c10322016-12-13 18:25:19 +0000703 auto CU1Ref4DieDG = CU1Ref2DieDG.getSibling();
704 EXPECT_TRUE(CU1Ref4DieDG.isValid());
705 EXPECT_EQ(CU1Ref4DieDG.getTag(), DW_TAG_variable);
Greg Claytond1efea82017-01-11 17:43:37 +0000706 EXPECT_EQ(
707 CU1Ref4DieDG.getAttributeValueAsReference(DW_AT_type).getValueOr(-1ULL),
708 CU1TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000709
710 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our
711 // base type DIE in CU1.
Greg Claytonc8c10322016-12-13 18:25:19 +0000712 auto CU1Ref8DieDG = CU1Ref4DieDG.getSibling();
713 EXPECT_TRUE(CU1Ref8DieDG.isValid());
714 EXPECT_EQ(CU1Ref8DieDG.getTag(), DW_TAG_variable);
Greg Claytond1efea82017-01-11 17:43:37 +0000715 EXPECT_EQ(
716 CU1Ref8DieDG.getAttributeValueAsReference(DW_AT_type).getValueOr(-1ULL),
717 CU1TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000718
719 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our
720 // base type DIE in CU1.
Greg Claytonc8c10322016-12-13 18:25:19 +0000721 auto CU1RefAddrDieDG = CU1Ref8DieDG.getSibling();
722 EXPECT_TRUE(CU1RefAddrDieDG.isValid());
723 EXPECT_EQ(CU1RefAddrDieDG.getTag(), DW_TAG_variable);
Greg Claytond1efea82017-01-11 17:43:37 +0000724 EXPECT_EQ(CU1RefAddrDieDG.getAttributeValueAsReference(DW_AT_type)
725 .getValueOr(-1ULL),
726 CU1TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000727
728 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its
729 // DW_AT_type points to our base type DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000730 auto CU1ToCU2RefAddrDieDG = CU1RefAddrDieDG.getSibling();
731 EXPECT_TRUE(CU1ToCU2RefAddrDieDG.isValid());
732 EXPECT_EQ(CU1ToCU2RefAddrDieDG.getTag(), DW_TAG_variable);
Greg Claytond1efea82017-01-11 17:43:37 +0000733 EXPECT_EQ(CU1ToCU2RefAddrDieDG.getAttributeValueAsReference(DW_AT_type)
734 .getValueOr(-1ULL),
Greg Claytonc8c10322016-12-13 18:25:19 +0000735 CU2TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000736
737 // Verify the sibling of the base type DIE is our Ref1 DIE and that its
738 // DW_AT_type points to our base type DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000739 auto CU2Ref1DieDG = CU2TypeDieDG.getSibling();
740 EXPECT_TRUE(CU2Ref1DieDG.isValid());
741 EXPECT_EQ(CU2Ref1DieDG.getTag(), DW_TAG_variable);
Greg Claytond1efea82017-01-11 17:43:37 +0000742 EXPECT_EQ(
743 CU2Ref1DieDG.getAttributeValueAsReference(DW_AT_type).getValueOr(-1ULL),
744 CU2TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000745 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our
746 // base type DIE in CU2.
Greg Claytonc8c10322016-12-13 18:25:19 +0000747 auto CU2Ref2DieDG = CU2Ref1DieDG.getSibling();
748 EXPECT_TRUE(CU2Ref2DieDG.isValid());
749 EXPECT_EQ(CU2Ref2DieDG.getTag(), DW_TAG_variable);
Greg Claytond1efea82017-01-11 17:43:37 +0000750 EXPECT_EQ(
751 CU2Ref2DieDG.getAttributeValueAsReference(DW_AT_type).getValueOr(-1ULL),
752 CU2TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000753
754 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our
755 // base type DIE in CU2.
Greg Claytonc8c10322016-12-13 18:25:19 +0000756 auto CU2Ref4DieDG = CU2Ref2DieDG.getSibling();
757 EXPECT_TRUE(CU2Ref4DieDG.isValid());
758 EXPECT_EQ(CU2Ref4DieDG.getTag(), DW_TAG_variable);
Greg Claytond1efea82017-01-11 17:43:37 +0000759 EXPECT_EQ(
760 CU2Ref4DieDG.getAttributeValueAsReference(DW_AT_type).getValueOr(-1ULL),
761 CU2TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000762
763 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our
764 // base type DIE in CU2.
Greg Claytonc8c10322016-12-13 18:25:19 +0000765 auto CU2Ref8DieDG = CU2Ref4DieDG.getSibling();
766 EXPECT_TRUE(CU2Ref8DieDG.isValid());
767 EXPECT_EQ(CU2Ref8DieDG.getTag(), DW_TAG_variable);
Greg Claytond1efea82017-01-11 17:43:37 +0000768 EXPECT_EQ(
769 CU2Ref8DieDG.getAttributeValueAsReference(DW_AT_type).getValueOr(-1ULL),
770 CU2TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000771
772 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our
773 // base type DIE in CU2.
Greg Claytonc8c10322016-12-13 18:25:19 +0000774 auto CU2RefAddrDieDG = CU2Ref8DieDG.getSibling();
775 EXPECT_TRUE(CU2RefAddrDieDG.isValid());
776 EXPECT_EQ(CU2RefAddrDieDG.getTag(), DW_TAG_variable);
Greg Claytond1efea82017-01-11 17:43:37 +0000777 EXPECT_EQ(CU2RefAddrDieDG.getAttributeValueAsReference(DW_AT_type)
778 .getValueOr(-1ULL),
779 CU2TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000780
781 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its
782 // DW_AT_type points to our base type DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000783 auto CU2ToCU1RefAddrDieDG = CU2RefAddrDieDG.getSibling();
784 EXPECT_TRUE(CU2ToCU1RefAddrDieDG.isValid());
785 EXPECT_EQ(CU2ToCU1RefAddrDieDG.getTag(), DW_TAG_variable);
Greg Claytond1efea82017-01-11 17:43:37 +0000786 EXPECT_EQ(CU2ToCU1RefAddrDieDG.getAttributeValueAsReference(DW_AT_type)
787 .getValueOr(-1ULL),
Greg Claytonc8c10322016-12-13 18:25:19 +0000788 CU1TypeDieDG.getOffset());
Greg Clayton3462a422016-12-08 01:03:48 +0000789}
790
791TEST(DWARFDebugInfo, TestDWARF32Version2Addr4References) {
792 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
793 // addresses.
794 typedef uint32_t AddrType;
795 TestReferences<2, AddrType>();
796}
797
798TEST(DWARFDebugInfo, TestDWARF32Version2Addr8References) {
799 // Test that we can decode all forms for DWARF32, version 2, with 8 byte
800 // addresses.
801 typedef uint64_t AddrType;
802 TestReferences<2, AddrType>();
803}
804
805TEST(DWARFDebugInfo, TestDWARF32Version3Addr4References) {
806 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
807 // addresses.
808 typedef uint32_t AddrType;
809 TestReferences<3, AddrType>();
810}
811
812TEST(DWARFDebugInfo, TestDWARF32Version3Addr8References) {
813 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
814 // addresses.
815 typedef uint64_t AddrType;
816 TestReferences<3, AddrType>();
817}
818
819TEST(DWARFDebugInfo, TestDWARF32Version4Addr4References) {
820 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
821 // addresses.
822 typedef uint32_t AddrType;
823 TestReferences<4, AddrType>();
824}
825
826TEST(DWARFDebugInfo, TestDWARF32Version4Addr8References) {
827 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
828 // addresses.
829 typedef uint64_t AddrType;
830 TestReferences<4, AddrType>();
831}
832
Greg Clayton2520c9e2016-12-19 20:36:41 +0000833template <uint16_t Version, class AddrType> void TestAddresses() {
834 // Test the DWARF APIs related to accessing the DW_AT_low_pc and
835 // DW_AT_high_pc.
836 const uint8_t AddrSize = sizeof(AddrType);
837 const bool SupportsHighPCAsOffset = Version >= 4;
838 initLLVMIfNeeded();
839 Triple Triple = getHostTripleForAddrSize(AddrSize);
840 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
841 if (HandleExpectedError(ExpectedDG))
842 return;
843 dwarfgen::Generator *DG = ExpectedDG.get().get();
844 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
845 dwarfgen::DIE CUDie = CU.getUnitDIE();
846
847 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
848 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
849
850 // Create a subprogram DIE with no low or high PC.
851 dwarfgen::DIE SubprogramNoPC = CUDie.addChild(DW_TAG_subprogram);
852 SubprogramNoPC.addAttribute(DW_AT_name, DW_FORM_strp, "no_pc");
853
854 // Create a subprogram DIE with a low PC only.
855 dwarfgen::DIE SubprogramLowPC = CUDie.addChild(DW_TAG_subprogram);
856 SubprogramLowPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_pc");
857 const uint64_t ActualLowPC = 0x1000;
858 const uint64_t ActualHighPC = 0x2000;
859 const uint64_t ActualHighPCOffset = ActualHighPC - ActualLowPC;
860 SubprogramLowPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC);
861
862 // Create a subprogram DIE with a low and high PC.
863 dwarfgen::DIE SubprogramLowHighPC = CUDie.addChild(DW_TAG_subprogram);
864 SubprogramLowHighPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_high_pc");
865 SubprogramLowHighPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC);
866 // Encode the high PC as an offset from the low PC if supported.
867 if (SupportsHighPCAsOffset)
868 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_data4,
869 ActualHighPCOffset);
870 else
871 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_addr, ActualHighPC);
872
873 StringRef FileBytes = DG->generate();
874 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
875 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
876 EXPECT_TRUE((bool)Obj);
877 DWARFContextInMemory DwarfContext(*Obj.get());
878
879 // Verify the number of compile units is correct.
880 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
881 EXPECT_EQ(NumCUs, 1u);
882 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
883
884 // Get the compile unit DIE is valid.
885 auto DieDG = U->getUnitDIE(false);
886 EXPECT_TRUE(DieDG.isValid());
Greg Clayton2520c9e2016-12-19 20:36:41 +0000887
888 uint64_t LowPC, HighPC;
889 Optional<uint64_t> OptU64;
890 // Verify the that our subprogram with no PC value fails appropriately when
891 // asked for any PC values.
892 auto SubprogramDieNoPC = DieDG.getFirstChild();
893 EXPECT_TRUE(SubprogramDieNoPC.isValid());
894 EXPECT_EQ(SubprogramDieNoPC.getTag(), DW_TAG_subprogram);
895 OptU64 = SubprogramDieNoPC.getAttributeValueAsAddress(DW_AT_low_pc);
896 EXPECT_FALSE((bool)OptU64);
897 OptU64 = SubprogramDieNoPC.getAttributeValueAsAddress(DW_AT_high_pc);
898 EXPECT_FALSE((bool)OptU64);
899 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC));
900 OptU64 = SubprogramDieNoPC.getAttributeValueAsAddress(DW_AT_high_pc);
901 EXPECT_FALSE((bool)OptU64);
902 OptU64 = SubprogramDieNoPC.getAttributeValueAsUnsignedConstant(DW_AT_high_pc);
903 EXPECT_FALSE((bool)OptU64);
904 OptU64 = SubprogramDieNoPC.getHighPC(ActualLowPC);
905 EXPECT_FALSE((bool)OptU64);
906 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC));
907
908
909 // Verify the that our subprogram with only a low PC value succeeds when
910 // we ask for the Low PC, but fails appropriately when asked for the high PC
911 // or both low and high PC values.
912 auto SubprogramDieLowPC = SubprogramDieNoPC.getSibling();
913 EXPECT_TRUE(SubprogramDieLowPC.isValid());
914 EXPECT_EQ(SubprogramDieLowPC.getTag(), DW_TAG_subprogram);
915 OptU64 = SubprogramDieLowPC.getAttributeValueAsAddress(DW_AT_low_pc);
916 EXPECT_TRUE((bool)OptU64);
917 EXPECT_EQ(OptU64.getValue(), ActualLowPC);
918 OptU64 = SubprogramDieLowPC.getAttributeValueAsAddress(DW_AT_high_pc);
919 EXPECT_FALSE((bool)OptU64);
920 OptU64 = SubprogramDieLowPC.getAttributeValueAsUnsignedConstant(DW_AT_high_pc);
921 EXPECT_FALSE((bool)OptU64);
922 OptU64 = SubprogramDieLowPC.getHighPC(ActualLowPC);
923 EXPECT_FALSE((bool)OptU64);
924 EXPECT_FALSE(SubprogramDieLowPC.getLowAndHighPC(LowPC, HighPC));
925
926
927 // Verify the that our subprogram with only a low PC value succeeds when
928 // we ask for the Low PC, but fails appropriately when asked for the high PC
929 // or both low and high PC values.
930 auto SubprogramDieLowHighPC = SubprogramDieLowPC.getSibling();
931 EXPECT_TRUE(SubprogramDieLowHighPC.isValid());
932 EXPECT_EQ(SubprogramDieLowHighPC.getTag(), DW_TAG_subprogram);
933 OptU64 = SubprogramDieLowHighPC.getAttributeValueAsAddress(DW_AT_low_pc);
934 EXPECT_TRUE((bool)OptU64);
935 EXPECT_EQ(OptU64.getValue(), ActualLowPC);
936 // Get the high PC as an address. This should succeed if the high PC was
937 // encoded as an address and fail if the high PC was encoded as an offset.
938 OptU64 = SubprogramDieLowHighPC.getAttributeValueAsAddress(DW_AT_high_pc);
939 if (SupportsHighPCAsOffset) {
940 EXPECT_FALSE((bool)OptU64);
941 } else {
942 EXPECT_TRUE((bool)OptU64);
943 EXPECT_EQ(OptU64.getValue(), ActualHighPC);
944 }
945 // Get the high PC as an unsigned constant. This should succeed if the high PC
946 // was encoded as an offset and fail if the high PC was encoded as an address.
947 OptU64 = SubprogramDieLowHighPC.getAttributeValueAsUnsignedConstant(
948 DW_AT_high_pc);
949 if (SupportsHighPCAsOffset) {
950 EXPECT_TRUE((bool)OptU64);
951 EXPECT_EQ(OptU64.getValue(), ActualHighPCOffset);
952 } else {
953 EXPECT_FALSE((bool)OptU64);
954 }
955
956 OptU64 = SubprogramDieLowHighPC.getHighPC(ActualLowPC);
957 EXPECT_TRUE((bool)OptU64);
958 EXPECT_EQ(OptU64.getValue(), ActualHighPC);
959
960 EXPECT_TRUE(SubprogramDieLowHighPC.getLowAndHighPC(LowPC, HighPC));
961 EXPECT_EQ(LowPC, ActualLowPC);
962 EXPECT_EQ(HighPC, ActualHighPC);
963}
964
965TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Addresses) {
966 // Test that we can decode address values in DWARF32, version 2, with 4 byte
967 // addresses.
968 typedef uint32_t AddrType;
969 TestAddresses<2, AddrType>();
970}
971
972TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Addresses) {
973 // Test that we can decode address values in DWARF32, version 2, with 8 byte
974 // addresses.
975 typedef uint64_t AddrType;
976 TestAddresses<2, AddrType>();
977}
978
979TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Addresses) {
980 // Test that we can decode address values in DWARF32, version 3, with 4 byte
981 // addresses.
982 typedef uint32_t AddrType;
983 TestAddresses<3, AddrType>();
984}
985
986TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Addresses) {
987 // Test that we can decode address values in DWARF32, version 3, with 8 byte
988 // addresses.
989 typedef uint64_t AddrType;
990 TestAddresses<3, AddrType>();
991}
992
993TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Addresses) {
994 // Test that we can decode address values in DWARF32, version 4, with 4 byte
995 // addresses.
996 typedef uint32_t AddrType;
997 TestAddresses<4, AddrType>();
998}
999
1000TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Addresses) {
1001 // Test that we can decode address values in DWARF32, version 4, with 8 byte
1002 // addresses.
1003 typedef uint64_t AddrType;
1004 TestAddresses<4, AddrType>();
1005}
1006
Greg Clayton78a07bf2016-12-21 21:37:06 +00001007TEST(DWARFDebugInfo, TestRelations) {
1008 // Test the DWARF APIs related to accessing the DW_AT_low_pc and
1009 // DW_AT_high_pc.
1010 uint16_t Version = 4;
1011
1012 const uint8_t AddrSize = sizeof(void *);
1013 initLLVMIfNeeded();
1014 Triple Triple = getHostTripleForAddrSize(AddrSize);
1015 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1016 if (HandleExpectedError(ExpectedDG))
1017 return;
1018 dwarfgen::Generator *DG = ExpectedDG.get().get();
1019 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1020
1021 enum class Tag: uint16_t {
1022 A = dwarf::DW_TAG_lo_user,
1023 B,
Greg Clayton78a07bf2016-12-21 21:37:06 +00001024 C,
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001025 C1,
1026 C2,
1027 D,
1028 D1
Greg Clayton78a07bf2016-12-21 21:37:06 +00001029 };
1030
1031 // Scope to allow us to re-use the same DIE names
1032 {
1033 // Create DWARF tree that looks like:
1034 //
1035 // CU
1036 // A
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001037 // B
1038 // C
1039 // C1
1040 // C2
1041 // D
1042 // D1
Greg Clayton78a07bf2016-12-21 21:37:06 +00001043 dwarfgen::DIE CUDie = CU.getUnitDIE();
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001044 dwarfgen::DIE A = CUDie.addChild((dwarf::Tag)Tag::A);
1045 A.addChild((dwarf::Tag)Tag::B);
1046 dwarfgen::DIE C = A.addChild((dwarf::Tag)Tag::C);
1047 dwarfgen::DIE D = A.addChild((dwarf::Tag)Tag::D);
Greg Clayton78a07bf2016-12-21 21:37:06 +00001048 C.addChild((dwarf::Tag)Tag::C1);
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001049 C.addChild((dwarf::Tag)Tag::C2);
1050 D.addChild((dwarf::Tag)Tag::D1);
Greg Clayton78a07bf2016-12-21 21:37:06 +00001051 }
1052
1053 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1054 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1055 EXPECT_TRUE((bool)Obj);
1056 DWARFContextInMemory DwarfContext(*Obj.get());
1057
1058 // Verify the number of compile units is correct.
1059 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
1060 EXPECT_EQ(NumCUs, 1u);
1061 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
1062
1063 // Get the compile unit DIE is valid.
1064 auto CUDie = U->getUnitDIE(false);
1065 EXPECT_TRUE(CUDie.isValid());
Greg Clayton78a07bf2016-12-21 21:37:06 +00001066
1067 // The compile unit doesn't have a parent or a sibling.
1068 auto ParentDie = CUDie.getParent();
1069 EXPECT_FALSE(ParentDie.isValid());
1070 auto SiblingDie = CUDie.getSibling();
1071 EXPECT_FALSE(SiblingDie.isValid());
1072
1073 // Get the children of the compile unit
1074 auto A = CUDie.getFirstChild();
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001075 auto B = A.getFirstChild();
Greg Clayton78a07bf2016-12-21 21:37:06 +00001076 auto C = B.getSibling();
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001077 auto D = C.getSibling();
1078 auto Null = D.getSibling();
Greg Clayton78a07bf2016-12-21 21:37:06 +00001079
1080 // Verify NULL Die is NULL and has no children or siblings
1081 EXPECT_TRUE(Null.isNULL());
1082 EXPECT_FALSE(Null.getSibling().isValid());
1083 EXPECT_FALSE(Null.getFirstChild().isValid());
1084
1085 // Verify all children of the compile unit DIE are correct.
1086 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A);
1087 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B);
1088 EXPECT_EQ(C.getTag(), (dwarf::Tag)Tag::C);
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001089 EXPECT_EQ(D.getTag(), (dwarf::Tag)Tag::D);
Greg Clayton78a07bf2016-12-21 21:37:06 +00001090
1091 // Verify who has children
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001092 EXPECT_TRUE(A.hasChildren());
1093 EXPECT_FALSE(B.hasChildren());
1094 EXPECT_TRUE(C.hasChildren());
1095 EXPECT_TRUE(D.hasChildren());
Greg Clayton78a07bf2016-12-21 21:37:06 +00001096
1097 // Make sure the parent of all the children of the compile unit are the
1098 // compile unit.
1099 EXPECT_EQ(A.getParent(), CUDie);
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001100
1101 // Make sure the parent of all the children of A are the A.
1102 // B is the first child in A, so we need to verify we can get the previous
1103 // DIE as the parent.
1104 EXPECT_EQ(B.getParent(), A);
1105 // C is the second child in A, so we need to make sure we can backup across
1106 // other DIE (B) at the same level to get the correct parent.
1107 EXPECT_EQ(C.getParent(), A);
1108 // D is the third child of A. We need to verify we can backup across other DIE
1109 // (B and C) including DIE that have children (D) to get the correct parent.
1110 EXPECT_EQ(D.getParent(), A);
Greg Clayton78a07bf2016-12-21 21:37:06 +00001111
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001112 // Verify that a DIE with no children returns an invalid DWARFDie.
1113 EXPECT_FALSE(B.getFirstChild().isValid());
Greg Clayton78a07bf2016-12-21 21:37:06 +00001114
1115 // Verify the children of the B DIE
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001116 auto C1 = C.getFirstChild();
1117 auto C2 = C1.getSibling();
1118 EXPECT_TRUE(C2.getSibling().isNULL());
Greg Clayton78a07bf2016-12-21 21:37:06 +00001119
1120 // Verify all children of the B DIE correctly valid or invalid.
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001121 EXPECT_EQ(C1.getTag(), (dwarf::Tag)Tag::C1);
1122 EXPECT_EQ(C2.getTag(), (dwarf::Tag)Tag::C2);
Greg Clayton78a07bf2016-12-21 21:37:06 +00001123
1124 // Make sure the parent of all the children of the B are the B.
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001125 EXPECT_EQ(C1.getParent(), C);
1126 EXPECT_EQ(C2.getParent(), C);
Greg Clayton78a07bf2016-12-21 21:37:06 +00001127}
1128
1129TEST(DWARFDebugInfo, TestDWARFDie) {
1130
1131 // Make sure a default constructed DWARFDie doesn't have any parent, sibling
1132 // or child;
1133 DWARFDie DefaultDie;
1134 EXPECT_FALSE(DefaultDie.getParent().isValid());
1135 EXPECT_FALSE(DefaultDie.getFirstChild().isValid());
1136 EXPECT_FALSE(DefaultDie.getSibling().isValid());
1137}
1138
Greg Clayton93e4fe82017-01-05 23:47:37 +00001139TEST(DWARFDebugInfo, TestChildIterators) {
1140 // Test the DWARF APIs related to iterating across the children of a DIE using
1141 // the DWARFDie::iterator class.
1142 uint16_t Version = 4;
1143
1144 const uint8_t AddrSize = sizeof(void *);
1145 initLLVMIfNeeded();
1146 Triple Triple = getHostTripleForAddrSize(AddrSize);
1147 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1148 if (HandleExpectedError(ExpectedDG))
1149 return;
1150 dwarfgen::Generator *DG = ExpectedDG.get().get();
1151 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1152
1153 enum class Tag: uint16_t {
1154 A = dwarf::DW_TAG_lo_user,
1155 B,
1156 };
1157
1158 // Scope to allow us to re-use the same DIE names
1159 {
1160 // Create DWARF tree that looks like:
1161 //
1162 // CU
1163 // A
1164 // B
1165 auto CUDie = CU.getUnitDIE();
1166 CUDie.addChild((dwarf::Tag)Tag::A);
1167 CUDie.addChild((dwarf::Tag)Tag::B);
1168 }
1169
1170 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1171 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1172 EXPECT_TRUE((bool)Obj);
1173 DWARFContextInMemory DwarfContext(*Obj.get());
1174
1175 // Verify the number of compile units is correct.
1176 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
1177 EXPECT_EQ(NumCUs, 1u);
1178 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
1179
1180 // Get the compile unit DIE is valid.
1181 auto CUDie = U->getUnitDIE(false);
1182 EXPECT_TRUE(CUDie.isValid());
Greg Clayton93e4fe82017-01-05 23:47:37 +00001183 uint32_t Index;
1184 DWARFDie A;
1185 DWARFDie B;
1186
1187 // Verify the compile unit DIE's children.
1188 Index = 0;
1189 for (auto Die : CUDie.children()) {
1190 switch (Index++) {
1191 case 0: A = Die; break;
1192 case 1: B = Die; break;
1193 }
1194 }
1195
1196 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A);
1197 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B);
1198
1199 // Verify that A has no children by verifying that the begin and end contain
1200 // invalid DIEs and also that the iterators are equal.
1201 EXPECT_EQ(A.begin(), A.end());
1202}
1203
1204TEST(DWARFDebugInfo, TestChildIteratorsOnInvalidDie) {
1205 // Verify that an invalid DIE has no children.
1206 DWARFDie Invalid;
1207 auto begin = Invalid.begin();
1208 auto end = Invalid.end();
1209 EXPECT_FALSE(begin->isValid());
1210 EXPECT_FALSE(end->isValid());
1211 EXPECT_EQ(begin, end);
1212}
1213
1214
1215TEST(DWARFDebugInfo, TestEmptyChildren) {
1216 // Test a DIE that says it has children in the abbreviation, but actually
1217 // doesn't have any attributes, will not return anything during iteration.
1218 // We do this by making sure the begin and end iterators are equal.
1219 uint16_t Version = 4;
1220
1221 const uint8_t AddrSize = sizeof(void *);
1222 initLLVMIfNeeded();
1223 Triple Triple = getHostTripleForAddrSize(AddrSize);
1224 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1225 if (HandleExpectedError(ExpectedDG))
1226 return;
1227 dwarfgen::Generator *DG = ExpectedDG.get().get();
1228 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1229
1230 // Scope to allow us to re-use the same DIE names
1231 {
1232 // Create a compile unit DIE that has an abbreviation that says it has
1233 // children, but doesn't have any actual attributes. This helps us test
1234 // a DIE that has only one child: a NULL DIE.
1235 auto CUDie = CU.getUnitDIE();
1236 CUDie.setForceChildren();
1237 }
1238
1239 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1240 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1241 EXPECT_TRUE((bool)Obj);
1242 DWARFContextInMemory DwarfContext(*Obj.get());
1243
1244 // Verify the number of compile units is correct.
1245 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
1246 EXPECT_EQ(NumCUs, 1u);
1247 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
1248
1249 // Get the compile unit DIE is valid.
1250 auto CUDie = U->getUnitDIE(false);
1251 EXPECT_TRUE(CUDie.isValid());
Greg Clayton93e4fe82017-01-05 23:47:37 +00001252
1253 // Verify that the CU Die that says it has children, but doesn't, actually
1254 // has begin and end iterators that are equal. We want to make sure we don't
1255 // see the Null DIEs during iteration.
1256 EXPECT_EQ(CUDie.begin(), CUDie.end());
1257}
Greg Clayton2520c9e2016-12-19 20:36:41 +00001258
Greg Clayton0e62ee72017-01-13 00:13:42 +00001259TEST(DWARFDebugInfo, TestAttributeIterators) {
1260 // Test the DWARF APIs related to iterating across all attribute values in a
1261 // a DWARFDie.
1262 uint16_t Version = 4;
1263
1264 const uint8_t AddrSize = sizeof(void *);
1265 initLLVMIfNeeded();
1266 Triple Triple = getHostTripleForAddrSize(AddrSize);
1267 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1268 if (HandleExpectedError(ExpectedDG))
1269 return;
1270 dwarfgen::Generator *DG = ExpectedDG.get().get();
1271 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1272 const uint64_t CULowPC = 0x1000;
1273 StringRef CUPath("/tmp/main.c");
1274
1275 // Scope to allow us to re-use the same DIE names
1276 {
1277 auto CUDie = CU.getUnitDIE();
1278 // Encode an attribute value before an attribute with no data.
1279 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, CUPath.data());
1280 // Encode an attribute value with no data in .debug_info/types to ensure
1281 // the iteration works correctly.
1282 CUDie.addAttribute(DW_AT_declaration, DW_FORM_flag_present);
1283 // Encode an attribute value after an attribute with no data.
1284 CUDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, CULowPC);
1285 }
1286
1287 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1288 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1289 EXPECT_TRUE((bool)Obj);
1290 DWARFContextInMemory DwarfContext(*Obj.get());
1291
1292 // Verify the number of compile units is correct.
1293 uint32_t NumCUs = DwarfContext.getNumCompileUnits();
1294 EXPECT_EQ(NumCUs, 1u);
1295 DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
1296
1297 // Get the compile unit DIE is valid.
1298 auto CUDie = U->getUnitDIE(false);
1299 EXPECT_TRUE(CUDie.isValid());
1300
1301 auto R = CUDie.attributes();
1302 auto I = R.begin();
1303 auto E = R.end();
1304
1305 ASSERT_NE(E, I);
1306 EXPECT_EQ(I->Attr, DW_AT_name);
1307 auto ActualCUPath = I->Value.getAsCString();
1308 EXPECT_EQ(CUPath, *ActualCUPath);
1309
1310 ASSERT_NE(E, ++I);
1311 EXPECT_EQ(I->Attr, DW_AT_declaration);
Greg Clayton71503432017-01-13 00:23:59 +00001312 EXPECT_EQ(1ull, *I->Value.getAsUnsignedConstant());
Greg Clayton0e62ee72017-01-13 00:13:42 +00001313
1314 ASSERT_NE(E, ++I);
1315 EXPECT_EQ(I->Attr, DW_AT_low_pc);
Greg Clayton71503432017-01-13 00:23:59 +00001316 EXPECT_EQ(CULowPC, *I->Value.getAsAddress());
Greg Clayton0e62ee72017-01-13 00:13:42 +00001317
1318 EXPECT_EQ(E, ++I);
1319}
1320
Greg Clayton3462a422016-12-08 01:03:48 +00001321} // end anonymous namespace