]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - unittests/DebugInfo/DWARF/DWARFDebugInfoTest.cpp
Vendor import of llvm trunk r291274:
[FreeBSD/FreeBSD.git] / unittests / DebugInfo / DWARF / DWARFDebugInfoTest.cpp
1 //===- llvm/unittest/DebugInfo/DWARFFormValueTest.cpp ---------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "DwarfGenerator.h"
11 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
12 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
13 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
14 #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
22 using namespace llvm;
23 using namespace dwarf;
24
25 namespace {
26
27 void initLLVMIfNeeded() {
28   static bool gInitialized = false;
29   if (!gInitialized) {
30     gInitialized = true;
31     InitializeAllTargets();
32     InitializeAllTargetMCs();
33     InitializeAllAsmPrinters();
34     InitializeAllAsmParsers();
35   }
36 }
37
38 Triple 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.
52 template <typename T>
53 static bool HandleExpectedError(T &Expected) {
54   std::string ErrorMsg;
55   handleAllErrors(Expected.takeError(), [&](const llvm::ErrorInfoBase &EI) {
56     ErrorMsg = EI.message();
57   });
58   if (!ErrorMsg.empty()) {
59     ::testing::AssertionFailure() << "error: " << ErrorMsg;
60     return true;
61   }
62   return false;
63 }
64
65 template <uint16_t Version, class AddrType, class RefAddrType>
66 void 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);
218   auto DieDG = U->getUnitDIE(false);
219   EXPECT_TRUE(DieDG.isValid());
220
221   //----------------------------------------------------------------------
222   // Test address forms
223   //----------------------------------------------------------------------
224   EXPECT_EQ(DieDG.getAttributeValueAsAddress(Attr_DW_FORM_addr, 0),
225             AddrValue);
226
227   //----------------------------------------------------------------------
228   // Test block forms
229   //----------------------------------------------------------------------
230   Optional<DWARFFormValue> FormValue;
231   ArrayRef<uint8_t> ExtractedBlockData;
232   Optional<ArrayRef<uint8_t>> BlockDataOpt;
233
234   FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block);
235   EXPECT_TRUE((bool)FormValue);
236   BlockDataOpt = FormValue->getAsBlock();
237   EXPECT_TRUE(BlockDataOpt.hasValue());
238   ExtractedBlockData = BlockDataOpt.getValue();
239   EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
240   EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
241
242   FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block1);
243   EXPECT_TRUE((bool)FormValue);
244   BlockDataOpt = FormValue->getAsBlock();
245   EXPECT_TRUE(BlockDataOpt.hasValue());
246   ExtractedBlockData = BlockDataOpt.getValue();
247   EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
248   EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
249
250   FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block2);
251   EXPECT_TRUE((bool)FormValue);
252   BlockDataOpt = FormValue->getAsBlock();
253   EXPECT_TRUE(BlockDataOpt.hasValue());
254   ExtractedBlockData = BlockDataOpt.getValue();
255   EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
256   EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
257
258   FormValue = DieDG.getAttributeValue(Attr_DW_FORM_block4);
259   EXPECT_TRUE((bool)FormValue);
260   BlockDataOpt = FormValue->getAsBlock();
261   EXPECT_TRUE(BlockDataOpt.hasValue());
262   ExtractedBlockData = BlockDataOpt.getValue();
263   EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
264   EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
265
266   //----------------------------------------------------------------------
267   // Test data forms
268   //----------------------------------------------------------------------
269   EXPECT_EQ(
270       DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data1, 0),
271       Data1);
272   EXPECT_EQ(
273       DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data2, 0),
274       Data2);
275   EXPECT_EQ(
276       DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data4, 0),
277       Data4);
278   EXPECT_EQ(
279       DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_data8, 0),
280       Data8);
281
282   //----------------------------------------------------------------------
283   // Test string forms
284   //----------------------------------------------------------------------
285   const char *ExtractedStringValue =
286       DieDG.getAttributeValueAsString(Attr_DW_FORM_string, nullptr);
287   EXPECT_TRUE(ExtractedStringValue != nullptr);
288   EXPECT_TRUE(strcmp(StringValue, ExtractedStringValue) == 0);
289
290   const char *ExtractedStrpValue =
291       DieDG.getAttributeValueAsString(Attr_DW_FORM_strp, nullptr);
292   EXPECT_TRUE(ExtractedStrpValue != nullptr);
293   EXPECT_TRUE(strcmp(StrpValue, ExtractedStrpValue) == 0);
294
295   //----------------------------------------------------------------------
296   // Test reference forms
297   //----------------------------------------------------------------------
298   EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_addr, 0),
299             RefAddr);
300   EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref1, 0),
301             Data1);
302   EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref2, 0),
303             Data2);
304   EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref4, 0),
305             Data4);
306   EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref8, 0),
307             Data8);
308   EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_sig8, 0),
309             Data8_2);
310   EXPECT_EQ(DieDG.getAttributeValueAsReference(Attr_DW_FORM_ref_udata, 0),
311             UData[0]);
312
313   //----------------------------------------------------------------------
314   // Test flag forms
315   //----------------------------------------------------------------------
316   EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(
317                 Attr_DW_FORM_flag_true, 0ULL),
318             1ULL);
319   EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(
320                 Attr_DW_FORM_flag_false, 1ULL),
321             0ULL);
322   EXPECT_EQ(DieDG.getAttributeValueAsUnsignedConstant(
323                 Attr_DW_FORM_flag_present, 0ULL),
324             1ULL);
325
326   // TODO: test Attr_DW_FORM_implicit_const extraction
327
328   //----------------------------------------------------------------------
329   // Test SLEB128 based forms
330   //----------------------------------------------------------------------
331   EXPECT_EQ(DieDG.getAttributeValueAsSignedConstant(Attr_DW_FORM_sdata, 0),
332             SData);
333
334   //----------------------------------------------------------------------
335   // Test ULEB128 based forms
336   //----------------------------------------------------------------------
337   EXPECT_EQ(
338       DieDG.getAttributeValueAsUnsignedConstant(Attr_DW_FORM_udata, 0),
339       UData[0]);
340
341   //----------------------------------------------------------------------
342   // Test DWARF32/DWARF64 forms
343   //----------------------------------------------------------------------
344   EXPECT_EQ(
345       DieDG.getAttributeValueAsReference(Attr_DW_FORM_GNU_ref_alt, 0),
346       Dwarf32Values[0]);
347   EXPECT_EQ(
348       DieDG.getAttributeValueAsSectionOffset(Attr_DW_FORM_sec_offset, 0),
349       Dwarf32Values[1]);
350
351   //----------------------------------------------------------------------
352   // Add an address at the end to make sure we can decode this value
353   //----------------------------------------------------------------------
354   EXPECT_EQ(DieDG.getAttributeValueAsAddress(Attr_Last, 0), AddrValue);
355 }
356
357 TEST(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
366 TEST(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
375 TEST(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
384 TEST(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
393 TEST(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
402 TEST(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
411 template <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.
456   auto DieDG = U->getUnitDIE(false);
457   EXPECT_TRUE(DieDG.isValid());
458   // DieDG.dump(llvm::outs(), U, UINT32_MAX);
459
460   // Verify the first child of the compile unit DIE is our subprogram.
461   auto SubprogramDieDG = DieDG.getFirstChild();
462   EXPECT_TRUE(SubprogramDieDG.isValid());
463   EXPECT_EQ(SubprogramDieDG.getTag(), DW_TAG_subprogram);
464
465   // Verify the first child of the subprogram is our formal parameter.
466   auto ArgcDieDG = SubprogramDieDG.getFirstChild();
467   EXPECT_TRUE(ArgcDieDG.isValid());
468   EXPECT_EQ(ArgcDieDG.getTag(), DW_TAG_formal_parameter);
469
470   // Verify our formal parameter has a NULL tag sibling.
471   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());
477   }
478
479   // Verify the sibling of our subprogram is our integer base type.
480   auto IntDieDG = SubprogramDieDG.getSibling();
481   EXPECT_TRUE(IntDieDG.isValid());
482   EXPECT_EQ(IntDieDG.getTag(), DW_TAG_base_type);
483
484   // Verify the sibling of our subprogram is our integer base is a NULL tag.
485   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());
491   }
492 }
493
494 TEST(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
501 TEST(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
508 TEST(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
515 TEST(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
522 TEST(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
529 TEST(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
536 template <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.
630   auto Unit1DieDG = U1->getUnitDIE(false);
631   EXPECT_TRUE(Unit1DieDG.isValid());
632   // Unit1DieDG.dump(llvm::outs(), UINT32_MAX);
633
634   auto Unit2DieDG = U2->getUnitDIE(false);
635   EXPECT_TRUE(Unit2DieDG.isValid());
636   // Unit2DieDG.dump(llvm::outs(), UINT32_MAX);
637
638   // Verify the first child of the compile unit 1 DIE is our int base type.
639   auto CU1TypeDieDG = Unit1DieDG.getFirstChild();
640   EXPECT_TRUE(CU1TypeDieDG.isValid());
641   EXPECT_EQ(CU1TypeDieDG.getTag(), DW_TAG_base_type);
642   EXPECT_EQ(
643       CU1TypeDieDG.getAttributeValueAsUnsignedConstant(DW_AT_encoding, 0),
644       DW_ATE_signed);
645
646   // Verify the first child of the compile unit 2 DIE is our float base type.
647   auto CU2TypeDieDG = Unit2DieDG.getFirstChild();
648   EXPECT_TRUE(CU2TypeDieDG.isValid());
649   EXPECT_EQ(CU2TypeDieDG.getTag(), DW_TAG_base_type);
650   EXPECT_EQ(
651       CU2TypeDieDG.getAttributeValueAsUnsignedConstant(DW_AT_encoding, 0),
652       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.
656   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());
661   // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our
662   // base type DIE in CU1.
663   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());
668
669   // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our
670   // base type DIE in CU1.
671   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());
676
677   // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our
678   // base type DIE in CU1.
679   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());
684
685   // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our
686   // base type DIE in CU1.
687   auto CU1RefAddrDieDG = CU1Ref8DieDG.getSibling();
688   EXPECT_TRUE(CU1RefAddrDieDG.isValid());
689   EXPECT_EQ(CU1RefAddrDieDG.getTag(), DW_TAG_variable);
690   EXPECT_EQ(
691       CU1RefAddrDieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
692       CU1TypeDieDG.getOffset());
693
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.
696   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,
700                                                                 -1ULL),
701             CU2TypeDieDG.getOffset());
702
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.
705   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());
710   // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our
711   // base type DIE in CU2.
712   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());
717
718   // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our
719   // base type DIE in CU2.
720   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());
725
726   // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our
727   // base type DIE in CU2.
728   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());
733
734   // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our
735   // base type DIE in CU2.
736   auto CU2RefAddrDieDG = CU2Ref8DieDG.getSibling();
737   EXPECT_TRUE(CU2RefAddrDieDG.isValid());
738   EXPECT_EQ(CU2RefAddrDieDG.getTag(), DW_TAG_variable);
739   EXPECT_EQ(
740       CU2RefAddrDieDG.getAttributeValueAsReference(DW_AT_type, -1ULL),
741       CU2TypeDieDG.getOffset());
742
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.
745   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,
749                                                                 -1ULL),
750             CU1TypeDieDG.getOffset());
751 }
752
753 TEST(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
760 TEST(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
767 TEST(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
774 TEST(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
781 TEST(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
788 TEST(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 template <uint16_t Version, class AddrType> void TestAddresses() {
796   // Test the DWARF APIs related to accessing the DW_AT_low_pc and
797   // DW_AT_high_pc.
798   const uint8_t AddrSize = sizeof(AddrType);
799   const bool SupportsHighPCAsOffset = Version >= 4;
800   initLLVMIfNeeded();
801   Triple Triple = getHostTripleForAddrSize(AddrSize);
802   auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
803   if (HandleExpectedError(ExpectedDG))
804     return;
805   dwarfgen::Generator *DG = ExpectedDG.get().get();
806   dwarfgen::CompileUnit &CU = DG->addCompileUnit();
807   dwarfgen::DIE CUDie = CU.getUnitDIE();
808   
809   CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
810   CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
811   
812   // Create a subprogram DIE with no low or high PC.
813   dwarfgen::DIE SubprogramNoPC = CUDie.addChild(DW_TAG_subprogram);
814   SubprogramNoPC.addAttribute(DW_AT_name, DW_FORM_strp, "no_pc");
815
816   // Create a subprogram DIE with a low PC only.
817   dwarfgen::DIE SubprogramLowPC = CUDie.addChild(DW_TAG_subprogram);
818   SubprogramLowPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_pc");
819   const uint64_t ActualLowPC = 0x1000;
820   const uint64_t ActualHighPC = 0x2000;
821   const uint64_t ActualHighPCOffset = ActualHighPC - ActualLowPC;
822   SubprogramLowPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC);
823
824   // Create a subprogram DIE with a low and high PC.
825   dwarfgen::DIE SubprogramLowHighPC = CUDie.addChild(DW_TAG_subprogram);
826   SubprogramLowHighPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_high_pc");
827   SubprogramLowHighPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC);
828   // Encode the high PC as an offset from the low PC if supported.
829   if (SupportsHighPCAsOffset)
830     SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_data4,
831                                      ActualHighPCOffset);
832   else
833     SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_addr, ActualHighPC);
834   
835   StringRef FileBytes = DG->generate();
836   MemoryBufferRef FileBuffer(FileBytes, "dwarf");
837   auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
838   EXPECT_TRUE((bool)Obj);
839   DWARFContextInMemory DwarfContext(*Obj.get());
840   
841   // Verify the number of compile units is correct.
842   uint32_t NumCUs = DwarfContext.getNumCompileUnits();
843   EXPECT_EQ(NumCUs, 1u);
844   DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
845   
846   // Get the compile unit DIE is valid.
847   auto DieDG = U->getUnitDIE(false);
848   EXPECT_TRUE(DieDG.isValid());
849   // DieDG.dump(llvm::outs(), U, UINT32_MAX);
850   
851   uint64_t LowPC, HighPC;
852   Optional<uint64_t> OptU64;
853   // Verify the that our subprogram with no PC value fails appropriately when
854   // asked for any PC values.
855   auto SubprogramDieNoPC = DieDG.getFirstChild();
856   EXPECT_TRUE(SubprogramDieNoPC.isValid());
857   EXPECT_EQ(SubprogramDieNoPC.getTag(), DW_TAG_subprogram);
858   OptU64 = SubprogramDieNoPC.getAttributeValueAsAddress(DW_AT_low_pc);
859   EXPECT_FALSE((bool)OptU64);
860   OptU64 = SubprogramDieNoPC.getAttributeValueAsAddress(DW_AT_high_pc);
861   EXPECT_FALSE((bool)OptU64);
862   EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC));
863   OptU64 = SubprogramDieNoPC.getAttributeValueAsAddress(DW_AT_high_pc);
864   EXPECT_FALSE((bool)OptU64);
865   OptU64 = SubprogramDieNoPC.getAttributeValueAsUnsignedConstant(DW_AT_high_pc);
866   EXPECT_FALSE((bool)OptU64);
867   OptU64 = SubprogramDieNoPC.getHighPC(ActualLowPC);
868   EXPECT_FALSE((bool)OptU64);
869   EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC));
870   
871   
872   // Verify the that our subprogram with only a low PC value succeeds when
873   // we ask for the Low PC, but fails appropriately when asked for the high PC
874   // or both low and high PC values.
875   auto SubprogramDieLowPC = SubprogramDieNoPC.getSibling();
876   EXPECT_TRUE(SubprogramDieLowPC.isValid());
877   EXPECT_EQ(SubprogramDieLowPC.getTag(), DW_TAG_subprogram);
878   OptU64 = SubprogramDieLowPC.getAttributeValueAsAddress(DW_AT_low_pc);
879   EXPECT_TRUE((bool)OptU64);
880   EXPECT_EQ(OptU64.getValue(), ActualLowPC);
881   OptU64 = SubprogramDieLowPC.getAttributeValueAsAddress(DW_AT_high_pc);
882   EXPECT_FALSE((bool)OptU64);
883   OptU64 = SubprogramDieLowPC.getAttributeValueAsUnsignedConstant(DW_AT_high_pc);
884   EXPECT_FALSE((bool)OptU64);
885   OptU64 = SubprogramDieLowPC.getHighPC(ActualLowPC);
886   EXPECT_FALSE((bool)OptU64);
887   EXPECT_FALSE(SubprogramDieLowPC.getLowAndHighPC(LowPC, HighPC));
888
889   
890   // Verify the that our subprogram with only a low PC value succeeds when
891   // we ask for the Low PC, but fails appropriately when asked for the high PC
892   // or both low and high PC values.
893   auto SubprogramDieLowHighPC = SubprogramDieLowPC.getSibling();
894   EXPECT_TRUE(SubprogramDieLowHighPC.isValid());
895   EXPECT_EQ(SubprogramDieLowHighPC.getTag(), DW_TAG_subprogram);
896   OptU64 = SubprogramDieLowHighPC.getAttributeValueAsAddress(DW_AT_low_pc);
897   EXPECT_TRUE((bool)OptU64);
898   EXPECT_EQ(OptU64.getValue(), ActualLowPC);
899   // Get the high PC as an address. This should succeed if the high PC was
900   // encoded as an address and fail if the high PC was encoded as an offset.
901   OptU64 = SubprogramDieLowHighPC.getAttributeValueAsAddress(DW_AT_high_pc);
902   if (SupportsHighPCAsOffset) {
903     EXPECT_FALSE((bool)OptU64);
904   } else {
905     EXPECT_TRUE((bool)OptU64);
906     EXPECT_EQ(OptU64.getValue(), ActualHighPC);
907   }
908   // Get the high PC as an unsigned constant. This should succeed if the high PC
909   // was encoded as an offset and fail if the high PC was encoded as an address.
910   OptU64 = SubprogramDieLowHighPC.getAttributeValueAsUnsignedConstant(
911       DW_AT_high_pc);
912   if (SupportsHighPCAsOffset) {
913     EXPECT_TRUE((bool)OptU64);
914     EXPECT_EQ(OptU64.getValue(), ActualHighPCOffset);
915   } else {
916     EXPECT_FALSE((bool)OptU64);
917   }
918
919   OptU64 = SubprogramDieLowHighPC.getHighPC(ActualLowPC);
920   EXPECT_TRUE((bool)OptU64);
921   EXPECT_EQ(OptU64.getValue(), ActualHighPC);
922
923   EXPECT_TRUE(SubprogramDieLowHighPC.getLowAndHighPC(LowPC, HighPC));
924   EXPECT_EQ(LowPC, ActualLowPC);
925   EXPECT_EQ(HighPC, ActualHighPC);
926 }
927
928 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Addresses) {
929   // Test that we can decode address values in DWARF32, version 2, with 4 byte
930   // addresses.
931   typedef uint32_t AddrType;
932   TestAddresses<2, AddrType>();
933 }
934
935 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Addresses) {
936   // Test that we can decode address values in DWARF32, version 2, with 8 byte
937   // addresses.
938   typedef uint64_t AddrType;
939   TestAddresses<2, AddrType>();
940 }
941
942 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Addresses) {
943   // Test that we can decode address values in DWARF32, version 3, with 4 byte
944   // addresses.
945   typedef uint32_t AddrType;
946   TestAddresses<3, AddrType>();
947 }
948
949 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Addresses) {
950   // Test that we can decode address values in DWARF32, version 3, with 8 byte
951   // addresses.
952   typedef uint64_t AddrType;
953   TestAddresses<3, AddrType>();
954 }
955
956 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Addresses) {
957   // Test that we can decode address values in DWARF32, version 4, with 4 byte
958   // addresses.
959   typedef uint32_t AddrType;
960   TestAddresses<4, AddrType>();
961 }
962
963 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Addresses) {
964   // Test that we can decode address values in DWARF32, version 4, with 8 byte
965   // addresses.
966   typedef uint64_t AddrType;
967   TestAddresses<4, AddrType>();
968 }
969
970 TEST(DWARFDebugInfo, TestRelations) {
971   // Test the DWARF APIs related to accessing the DW_AT_low_pc and
972   // DW_AT_high_pc.
973   uint16_t Version = 4;
974   
975   const uint8_t AddrSize = sizeof(void *);
976   initLLVMIfNeeded();
977   Triple Triple = getHostTripleForAddrSize(AddrSize);
978   auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
979   if (HandleExpectedError(ExpectedDG))
980     return;
981   dwarfgen::Generator *DG = ExpectedDG.get().get();
982   dwarfgen::CompileUnit &CU = DG->addCompileUnit();
983   
984   enum class Tag: uint16_t  {
985     A = dwarf::DW_TAG_lo_user,
986     B,
987     C,
988     C1,
989     C2,
990     D,
991     D1
992   };
993
994   // Scope to allow us to re-use the same DIE names
995   {
996     // Create DWARF tree that looks like:
997     //
998     // CU
999     //   A
1000     //     B
1001     //     C
1002     //       C1
1003     //       C2
1004     //     D
1005     //       D1
1006     dwarfgen::DIE CUDie = CU.getUnitDIE();
1007     dwarfgen::DIE A = CUDie.addChild((dwarf::Tag)Tag::A);
1008     A.addChild((dwarf::Tag)Tag::B);
1009     dwarfgen::DIE C = A.addChild((dwarf::Tag)Tag::C);
1010     dwarfgen::DIE D = A.addChild((dwarf::Tag)Tag::D);
1011     C.addChild((dwarf::Tag)Tag::C1);
1012     C.addChild((dwarf::Tag)Tag::C2);
1013     D.addChild((dwarf::Tag)Tag::D1);
1014   }
1015
1016   MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1017   auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1018   EXPECT_TRUE((bool)Obj);
1019   DWARFContextInMemory DwarfContext(*Obj.get());
1020   
1021   // Verify the number of compile units is correct.
1022   uint32_t NumCUs = DwarfContext.getNumCompileUnits();
1023   EXPECT_EQ(NumCUs, 1u);
1024   DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
1025   
1026   // Get the compile unit DIE is valid.
1027   auto CUDie = U->getUnitDIE(false);
1028   EXPECT_TRUE(CUDie.isValid());
1029   // CUDie.dump(llvm::outs(), UINT32_MAX);
1030   
1031   // The compile unit doesn't have a parent or a sibling.
1032   auto ParentDie = CUDie.getParent();
1033   EXPECT_FALSE(ParentDie.isValid());
1034   auto SiblingDie = CUDie.getSibling();
1035   EXPECT_FALSE(SiblingDie.isValid());
1036   
1037   // Get the children of the compile unit
1038   auto A = CUDie.getFirstChild();
1039   auto B = A.getFirstChild();
1040   auto C = B.getSibling();
1041   auto D = C.getSibling();
1042   auto Null = D.getSibling();
1043   
1044   // Verify NULL Die is NULL and has no children or siblings
1045   EXPECT_TRUE(Null.isNULL());
1046   EXPECT_FALSE(Null.getSibling().isValid());
1047   EXPECT_FALSE(Null.getFirstChild().isValid());
1048   
1049   // Verify all children of the compile unit DIE are correct.
1050   EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A);
1051   EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B);
1052   EXPECT_EQ(C.getTag(), (dwarf::Tag)Tag::C);
1053   EXPECT_EQ(D.getTag(), (dwarf::Tag)Tag::D);
1054
1055   // Verify who has children
1056   EXPECT_TRUE(A.hasChildren());
1057   EXPECT_FALSE(B.hasChildren());
1058   EXPECT_TRUE(C.hasChildren());
1059   EXPECT_TRUE(D.hasChildren());
1060
1061   // Make sure the parent of all the children of the compile unit are the
1062   // compile unit.
1063   EXPECT_EQ(A.getParent(), CUDie);
1064   
1065   // Make sure the parent of all the children of A are the A.
1066   // B is the first child in A, so we need to verify we can get the previous
1067   // DIE as the parent.
1068   EXPECT_EQ(B.getParent(), A);
1069   // C is the second child in A, so we need to make sure we can backup across
1070   // other DIE (B) at the same level to get the correct parent.
1071   EXPECT_EQ(C.getParent(), A);
1072   // D is the third child of A. We need to verify we can backup across other DIE
1073   // (B and C) including DIE that have children (D) to get the correct parent.
1074   EXPECT_EQ(D.getParent(), A);
1075
1076   // Verify that a DIE with no children returns an invalid DWARFDie.
1077   EXPECT_FALSE(B.getFirstChild().isValid());
1078
1079   // Verify the children of the B DIE
1080   auto C1 = C.getFirstChild();
1081   auto C2 = C1.getSibling();
1082   EXPECT_TRUE(C2.getSibling().isNULL());
1083   
1084   // Verify all children of the B DIE correctly valid or invalid.
1085   EXPECT_EQ(C1.getTag(), (dwarf::Tag)Tag::C1);
1086   EXPECT_EQ(C2.getTag(), (dwarf::Tag)Tag::C2);
1087
1088   // Make sure the parent of all the children of the B are the B.
1089   EXPECT_EQ(C1.getParent(), C);
1090   EXPECT_EQ(C2.getParent(), C);
1091 }
1092
1093 TEST(DWARFDebugInfo, TestDWARFDie) {
1094
1095   // Make sure a default constructed DWARFDie doesn't have any parent, sibling
1096   // or child;
1097   DWARFDie DefaultDie;
1098   EXPECT_FALSE(DefaultDie.getParent().isValid());
1099   EXPECT_FALSE(DefaultDie.getFirstChild().isValid());
1100   EXPECT_FALSE(DefaultDie.getSibling().isValid());
1101 }
1102
1103 TEST(DWARFDebugInfo, TestChildIterators) {
1104   // Test the DWARF APIs related to iterating across the children of a DIE using
1105   // the DWARFDie::iterator class.
1106   uint16_t Version = 4;
1107   
1108   const uint8_t AddrSize = sizeof(void *);
1109   initLLVMIfNeeded();
1110   Triple Triple = getHostTripleForAddrSize(AddrSize);
1111   auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1112   if (HandleExpectedError(ExpectedDG))
1113     return;
1114   dwarfgen::Generator *DG = ExpectedDG.get().get();
1115   dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1116   
1117   enum class Tag: uint16_t  {
1118     A = dwarf::DW_TAG_lo_user,
1119     B,
1120   };
1121   
1122   // Scope to allow us to re-use the same DIE names
1123   {
1124     // Create DWARF tree that looks like:
1125     //
1126     // CU
1127     //   A
1128     //   B
1129     auto CUDie = CU.getUnitDIE();
1130     CUDie.addChild((dwarf::Tag)Tag::A);
1131     CUDie.addChild((dwarf::Tag)Tag::B);
1132   }
1133   
1134   MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1135   auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1136   EXPECT_TRUE((bool)Obj);
1137   DWARFContextInMemory DwarfContext(*Obj.get());
1138   
1139   // Verify the number of compile units is correct.
1140   uint32_t NumCUs = DwarfContext.getNumCompileUnits();
1141   EXPECT_EQ(NumCUs, 1u);
1142   DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
1143   
1144   // Get the compile unit DIE is valid.
1145   auto CUDie = U->getUnitDIE(false);
1146   EXPECT_TRUE(CUDie.isValid());
1147   // CUDie.dump(llvm::outs(), UINT32_MAX);
1148   uint32_t Index;
1149   DWARFDie A;
1150   DWARFDie B;
1151   
1152   // Verify the compile unit DIE's children.
1153   Index = 0;
1154   for (auto Die : CUDie.children()) {
1155     switch (Index++) {
1156       case 0: A = Die; break;
1157       case 1: B = Die; break;
1158     }
1159   }
1160   
1161   EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A);
1162   EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B);
1163
1164   // Verify that A has no children by verifying that the begin and end contain
1165   // invalid DIEs and also that the iterators are equal.
1166   EXPECT_EQ(A.begin(), A.end());
1167 }
1168
1169 TEST(DWARFDebugInfo, TestChildIteratorsOnInvalidDie) {
1170   // Verify that an invalid DIE has no children.
1171   DWARFDie Invalid;
1172   auto begin = Invalid.begin();
1173   auto end = Invalid.end();
1174   EXPECT_FALSE(begin->isValid());
1175   EXPECT_FALSE(end->isValid());
1176   EXPECT_EQ(begin, end);
1177 }
1178
1179   
1180 TEST(DWARFDebugInfo, TestEmptyChildren) {
1181   // Test a DIE that says it has children in the abbreviation, but actually
1182   // doesn't have any attributes, will not return anything during iteration.
1183   // We do this by making sure the begin and end iterators are equal.
1184   uint16_t Version = 4;
1185   
1186   const uint8_t AddrSize = sizeof(void *);
1187   initLLVMIfNeeded();
1188   Triple Triple = getHostTripleForAddrSize(AddrSize);
1189   auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1190   if (HandleExpectedError(ExpectedDG))
1191     return;
1192   dwarfgen::Generator *DG = ExpectedDG.get().get();
1193   dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1194   
1195   // Scope to allow us to re-use the same DIE names
1196   {
1197     // Create a compile unit DIE that has an abbreviation that says it has
1198     // children, but doesn't have any actual attributes. This helps us test
1199     // a DIE that has only one child: a NULL DIE.
1200     auto CUDie = CU.getUnitDIE();
1201     CUDie.setForceChildren();
1202   }
1203   
1204   MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1205   auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1206   EXPECT_TRUE((bool)Obj);
1207   DWARFContextInMemory DwarfContext(*Obj.get());
1208   
1209   // Verify the number of compile units is correct.
1210   uint32_t NumCUs = DwarfContext.getNumCompileUnits();
1211   EXPECT_EQ(NumCUs, 1u);
1212   DWARFCompileUnit *U = DwarfContext.getCompileUnitAtIndex(0);
1213   
1214   // Get the compile unit DIE is valid.
1215   auto CUDie = U->getUnitDIE(false);
1216   EXPECT_TRUE(CUDie.isValid());
1217   CUDie.dump(llvm::outs(), UINT32_MAX);
1218   
1219   // Verify that the CU Die that says it has children, but doesn't, actually
1220   // has begin and end iterators that are equal. We want to make sure we don't
1221   // see the Null DIEs during iteration.
1222   EXPECT_EQ(CUDie.begin(), CUDie.end());
1223 }
1224
1225 } // end anonymous namespace