[MIParser] Set RegClassOrRegBank during instruction parsing
[llvm-complete.git] / unittests / DebugInfo / DWARF / DWARFDebugInfoTest.cpp
blob9b017b31560217903582e77531dffc3557d8bf3c
1 //===- llvm/unittest/DebugInfo/DWARFDebugInfoTest.cpp ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "DwarfGenerator.h"
10 #include "DwarfUtils.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/BinaryFormat/Dwarf.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
19 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
20 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
21 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
22 #include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCSectionELF.h"
25 #include "llvm/MC/MCStreamer.h"
26 #include "llvm/Object/ObjectFile.h"
27 #include "llvm/ObjectYAML/DWARFEmitter.h"
28 #include "llvm/Support/Error.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/TargetRegistry.h"
31 #include "llvm/Support/TargetSelect.h"
32 #include "llvm/Testing/Support/Error.h"
33 #include "gtest/gtest.h"
34 #include <string>
36 using namespace llvm;
37 using namespace dwarf;
38 using namespace utils;
40 namespace {
42 template <uint16_t Version, class AddrType, class RefAddrType>
43 void TestAllForms() {
44 Triple Triple = getDefaultTargetTripleForAddrSize(sizeof(AddrType));
45 if (!isConfigurationSupported(Triple))
46 return;
48 // Test that we can decode all DW_FORM values correctly.
49 const AddrType AddrValue = (AddrType)0x0123456789abcdefULL;
50 const uint8_t BlockData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
51 const uint32_t BlockSize = sizeof(BlockData);
52 const RefAddrType RefAddr = 0x12345678;
53 const uint8_t Data1 = 0x01U;
54 const uint16_t Data2 = 0x2345U;
55 const uint32_t Data4 = 0x6789abcdU;
56 const uint64_t Data8 = 0x0011223344556677ULL;
57 const uint64_t Data8_2 = 0xAABBCCDDEEFF0011ULL;
58 const uint8_t Data16[16] = {1, 2, 3, 4, 5, 6, 7, 8,
59 9, 10, 11, 12, 13, 14, 15, 16};
60 const int64_t SData = INT64_MIN;
61 const int64_t ICSData = INT64_MAX; // DW_FORM_implicit_const SData
62 const uint64_t UData[] = {UINT64_MAX - 1, UINT64_MAX - 2, UINT64_MAX - 3,
63 UINT64_MAX - 4, UINT64_MAX - 5, UINT64_MAX - 6,
64 UINT64_MAX - 7, UINT64_MAX - 8, UINT64_MAX - 9};
65 #define UDATA_1 18446744073709551614ULL
66 const uint32_t Dwarf32Values[] = {1, 2, 3, 4, 5, 6, 7, 8};
67 const char *StringValue = "Hello";
68 const char *StrpValue = "World";
69 const char *StrxValue = "Indexed";
70 const char *Strx1Value = "Indexed1";
71 const char *Strx2Value = "Indexed2";
72 const char *Strx3Value = "Indexed3";
73 const char *Strx4Value = "Indexed4";
75 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
76 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
77 dwarfgen::Generator *DG = ExpectedDG.get().get();
78 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
79 dwarfgen::DIE CUDie = CU.getUnitDIE();
81 if (Version >= 5)
82 CUDie.addStrOffsetsBaseAttribute();
84 uint16_t Attr = DW_AT_lo_user;
86 //----------------------------------------------------------------------
87 // Test address forms
88 //----------------------------------------------------------------------
89 const auto Attr_DW_FORM_addr = static_cast<dwarf::Attribute>(Attr++);
90 CUDie.addAttribute(Attr_DW_FORM_addr, DW_FORM_addr, AddrValue);
92 //----------------------------------------------------------------------
93 // Test block forms
94 //----------------------------------------------------------------------
95 const auto Attr_DW_FORM_block = static_cast<dwarf::Attribute>(Attr++);
96 CUDie.addAttribute(Attr_DW_FORM_block, DW_FORM_block, BlockData, BlockSize);
98 const auto Attr_DW_FORM_block1 = static_cast<dwarf::Attribute>(Attr++);
99 CUDie.addAttribute(Attr_DW_FORM_block1, DW_FORM_block1, BlockData, BlockSize);
101 const auto Attr_DW_FORM_block2 = static_cast<dwarf::Attribute>(Attr++);
102 CUDie.addAttribute(Attr_DW_FORM_block2, DW_FORM_block2, BlockData, BlockSize);
104 const auto Attr_DW_FORM_block4 = static_cast<dwarf::Attribute>(Attr++);
105 CUDie.addAttribute(Attr_DW_FORM_block4, DW_FORM_block4, BlockData, BlockSize);
107 // We handle data16 as a block form.
108 const auto Attr_DW_FORM_data16 = static_cast<dwarf::Attribute>(Attr++);
109 if (Version >= 5)
110 CUDie.addAttribute(Attr_DW_FORM_data16, DW_FORM_data16, Data16, 16);
112 //----------------------------------------------------------------------
113 // Test data forms
114 //----------------------------------------------------------------------
115 const auto Attr_DW_FORM_data1 = static_cast<dwarf::Attribute>(Attr++);
116 CUDie.addAttribute(Attr_DW_FORM_data1, DW_FORM_data1, Data1);
118 const auto Attr_DW_FORM_data2 = static_cast<dwarf::Attribute>(Attr++);
119 CUDie.addAttribute(Attr_DW_FORM_data2, DW_FORM_data2, Data2);
121 const auto Attr_DW_FORM_data4 = static_cast<dwarf::Attribute>(Attr++);
122 CUDie.addAttribute(Attr_DW_FORM_data4, DW_FORM_data4, Data4);
124 const auto Attr_DW_FORM_data8 = static_cast<dwarf::Attribute>(Attr++);
125 CUDie.addAttribute(Attr_DW_FORM_data8, DW_FORM_data8, Data8);
127 //----------------------------------------------------------------------
128 // Test string forms
129 //----------------------------------------------------------------------
130 const auto Attr_DW_FORM_string = static_cast<dwarf::Attribute>(Attr++);
131 CUDie.addAttribute(Attr_DW_FORM_string, DW_FORM_string, StringValue);
133 const auto Attr_DW_FORM_strx = static_cast<dwarf::Attribute>(Attr++);
134 const auto Attr_DW_FORM_strx1 = static_cast<dwarf::Attribute>(Attr++);
135 const auto Attr_DW_FORM_strx2 = static_cast<dwarf::Attribute>(Attr++);
136 const auto Attr_DW_FORM_strx3 = static_cast<dwarf::Attribute>(Attr++);
137 const auto Attr_DW_FORM_strx4 = static_cast<dwarf::Attribute>(Attr++);
138 if (Version >= 5) {
139 CUDie.addAttribute(Attr_DW_FORM_strx, DW_FORM_strx, StrxValue);
140 CUDie.addAttribute(Attr_DW_FORM_strx1, DW_FORM_strx1, Strx1Value);
141 CUDie.addAttribute(Attr_DW_FORM_strx2, DW_FORM_strx2, Strx2Value);
142 CUDie.addAttribute(Attr_DW_FORM_strx3, DW_FORM_strx3, Strx3Value);
143 CUDie.addAttribute(Attr_DW_FORM_strx4, DW_FORM_strx4, Strx4Value);
146 const auto Attr_DW_FORM_strp = static_cast<dwarf::Attribute>(Attr++);
147 CUDie.addAttribute(Attr_DW_FORM_strp, DW_FORM_strp, StrpValue);
149 //----------------------------------------------------------------------
150 // Test reference forms
151 //----------------------------------------------------------------------
152 const auto Attr_DW_FORM_ref_addr = static_cast<dwarf::Attribute>(Attr++);
153 CUDie.addAttribute(Attr_DW_FORM_ref_addr, DW_FORM_ref_addr, RefAddr);
155 const auto Attr_DW_FORM_ref1 = static_cast<dwarf::Attribute>(Attr++);
156 CUDie.addAttribute(Attr_DW_FORM_ref1, DW_FORM_ref1, Data1);
158 const auto Attr_DW_FORM_ref2 = static_cast<dwarf::Attribute>(Attr++);
159 CUDie.addAttribute(Attr_DW_FORM_ref2, DW_FORM_ref2, Data2);
161 const auto Attr_DW_FORM_ref4 = static_cast<dwarf::Attribute>(Attr++);
162 CUDie.addAttribute(Attr_DW_FORM_ref4, DW_FORM_ref4, Data4);
164 const auto Attr_DW_FORM_ref8 = static_cast<dwarf::Attribute>(Attr++);
165 CUDie.addAttribute(Attr_DW_FORM_ref8, DW_FORM_ref8, Data8);
167 const auto Attr_DW_FORM_ref_sig8 = static_cast<dwarf::Attribute>(Attr++);
168 if (Version >= 4)
169 CUDie.addAttribute(Attr_DW_FORM_ref_sig8, DW_FORM_ref_sig8, Data8_2);
171 const auto Attr_DW_FORM_ref_udata = static_cast<dwarf::Attribute>(Attr++);
172 CUDie.addAttribute(Attr_DW_FORM_ref_udata, DW_FORM_ref_udata, UData[0]);
174 //----------------------------------------------------------------------
175 // Test flag forms
176 //----------------------------------------------------------------------
177 const auto Attr_DW_FORM_flag_true = static_cast<dwarf::Attribute>(Attr++);
178 CUDie.addAttribute(Attr_DW_FORM_flag_true, DW_FORM_flag, true);
180 const auto Attr_DW_FORM_flag_false = static_cast<dwarf::Attribute>(Attr++);
181 CUDie.addAttribute(Attr_DW_FORM_flag_false, DW_FORM_flag, false);
183 const auto Attr_DW_FORM_flag_present = static_cast<dwarf::Attribute>(Attr++);
184 if (Version >= 4)
185 CUDie.addAttribute(Attr_DW_FORM_flag_present, DW_FORM_flag_present);
187 //----------------------------------------------------------------------
188 // Test SLEB128 based forms
189 //----------------------------------------------------------------------
190 const auto Attr_DW_FORM_sdata = static_cast<dwarf::Attribute>(Attr++);
191 CUDie.addAttribute(Attr_DW_FORM_sdata, DW_FORM_sdata, SData);
193 const auto Attr_DW_FORM_implicit_const =
194 static_cast<dwarf::Attribute>(Attr++);
195 if (Version >= 5)
196 CUDie.addAttribute(Attr_DW_FORM_implicit_const, DW_FORM_implicit_const,
197 ICSData);
199 //----------------------------------------------------------------------
200 // Test ULEB128 based forms
201 //----------------------------------------------------------------------
202 const auto Attr_DW_FORM_udata = static_cast<dwarf::Attribute>(Attr++);
203 CUDie.addAttribute(Attr_DW_FORM_udata, DW_FORM_udata, UData[0]);
205 //----------------------------------------------------------------------
206 // Test DWARF32/DWARF64 forms
207 //----------------------------------------------------------------------
208 const auto Attr_DW_FORM_GNU_ref_alt = static_cast<dwarf::Attribute>(Attr++);
209 CUDie.addAttribute(Attr_DW_FORM_GNU_ref_alt, DW_FORM_GNU_ref_alt,
210 Dwarf32Values[0]);
212 const auto Attr_DW_FORM_sec_offset = static_cast<dwarf::Attribute>(Attr++);
213 if (Version >= 4)
214 CUDie.addAttribute(Attr_DW_FORM_sec_offset, DW_FORM_sec_offset,
215 Dwarf32Values[1]);
217 //----------------------------------------------------------------------
218 // Add an address at the end to make sure we can decode this value
219 //----------------------------------------------------------------------
220 const auto Attr_Last = static_cast<dwarf::Attribute>(Attr++);
221 CUDie.addAttribute(Attr_Last, DW_FORM_addr, AddrValue);
223 //----------------------------------------------------------------------
224 // Generate the DWARF
225 //----------------------------------------------------------------------
226 StringRef FileBytes = DG->generate();
227 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
228 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
229 EXPECT_TRUE((bool)Obj);
230 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
231 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
232 EXPECT_EQ(NumCUs, 1u);
233 DWARFCompileUnit *U =
234 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));
235 auto DieDG = U->getUnitDIE(false);
236 EXPECT_TRUE(DieDG.isValid());
238 //----------------------------------------------------------------------
239 // Test address forms
240 //----------------------------------------------------------------------
241 EXPECT_EQ(AddrValue, toAddress(DieDG.find(Attr_DW_FORM_addr), 0));
243 //----------------------------------------------------------------------
244 // Test block forms
245 //----------------------------------------------------------------------
246 Optional<DWARFFormValue> FormValue;
247 ArrayRef<uint8_t> ExtractedBlockData;
248 Optional<ArrayRef<uint8_t>> BlockDataOpt;
250 FormValue = DieDG.find(Attr_DW_FORM_block);
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);
258 FormValue = DieDG.find(Attr_DW_FORM_block1);
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);
266 FormValue = DieDG.find(Attr_DW_FORM_block2);
267 EXPECT_TRUE((bool)FormValue);
268 BlockDataOpt = FormValue->getAsBlock();
269 EXPECT_TRUE(BlockDataOpt.hasValue());
270 ExtractedBlockData = BlockDataOpt.getValue();
271 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
272 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
274 FormValue = DieDG.find(Attr_DW_FORM_block4);
275 EXPECT_TRUE((bool)FormValue);
276 BlockDataOpt = FormValue->getAsBlock();
277 EXPECT_TRUE(BlockDataOpt.hasValue());
278 ExtractedBlockData = BlockDataOpt.getValue();
279 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
280 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
282 // Data16 is handled like a block.
283 if (Version >= 5) {
284 FormValue = DieDG.find(Attr_DW_FORM_data16);
285 EXPECT_TRUE((bool)FormValue);
286 BlockDataOpt = FormValue->getAsBlock();
287 EXPECT_TRUE(BlockDataOpt.hasValue());
288 ExtractedBlockData = BlockDataOpt.getValue();
289 EXPECT_EQ(ExtractedBlockData.size(), 16u);
290 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), Data16, 16) == 0);
293 //----------------------------------------------------------------------
294 // Test data forms
295 //----------------------------------------------------------------------
296 EXPECT_EQ(Data1, toUnsigned(DieDG.find(Attr_DW_FORM_data1), 0));
297 EXPECT_EQ(Data2, toUnsigned(DieDG.find(Attr_DW_FORM_data2), 0));
298 EXPECT_EQ(Data4, toUnsigned(DieDG.find(Attr_DW_FORM_data4), 0));
299 EXPECT_EQ(Data8, toUnsigned(DieDG.find(Attr_DW_FORM_data8), 0));
301 //----------------------------------------------------------------------
302 // Test string forms
303 //----------------------------------------------------------------------
304 auto ExtractedStringValue = toString(DieDG.find(Attr_DW_FORM_string));
305 EXPECT_TRUE((bool)ExtractedStringValue);
306 EXPECT_STREQ(StringValue, *ExtractedStringValue);
308 if (Version >= 5) {
309 auto ExtractedStrxValue = toString(DieDG.find(Attr_DW_FORM_strx));
310 EXPECT_TRUE((bool)ExtractedStrxValue);
311 EXPECT_STREQ(StrxValue, *ExtractedStrxValue);
313 auto ExtractedStrx1Value = toString(DieDG.find(Attr_DW_FORM_strx1));
314 EXPECT_TRUE((bool)ExtractedStrx1Value);
315 EXPECT_STREQ(Strx1Value, *ExtractedStrx1Value);
317 auto ExtractedStrx2Value = toString(DieDG.find(Attr_DW_FORM_strx2));
318 EXPECT_TRUE((bool)ExtractedStrx2Value);
319 EXPECT_STREQ(Strx2Value, *ExtractedStrx2Value);
321 auto ExtractedStrx3Value = toString(DieDG.find(Attr_DW_FORM_strx3));
322 EXPECT_TRUE((bool)ExtractedStrx3Value);
323 EXPECT_STREQ(Strx3Value, *ExtractedStrx3Value);
325 auto ExtractedStrx4Value = toString(DieDG.find(Attr_DW_FORM_strx4));
326 EXPECT_TRUE((bool)ExtractedStrx4Value);
327 EXPECT_STREQ(Strx4Value, *ExtractedStrx4Value);
330 auto ExtractedStrpValue = toString(DieDG.find(Attr_DW_FORM_strp));
331 EXPECT_TRUE((bool)ExtractedStrpValue);
332 EXPECT_STREQ(StrpValue, *ExtractedStrpValue);
334 //----------------------------------------------------------------------
335 // Test reference forms
336 //----------------------------------------------------------------------
337 EXPECT_EQ(RefAddr, toReference(DieDG.find(Attr_DW_FORM_ref_addr), 0));
338 EXPECT_EQ(Data1, toReference(DieDG.find(Attr_DW_FORM_ref1), 0));
339 EXPECT_EQ(Data2, toReference(DieDG.find(Attr_DW_FORM_ref2), 0));
340 EXPECT_EQ(Data4, toReference(DieDG.find(Attr_DW_FORM_ref4), 0));
341 EXPECT_EQ(Data8, toReference(DieDG.find(Attr_DW_FORM_ref8), 0));
342 if (Version >= 4) {
343 EXPECT_EQ(Data8_2, toReference(DieDG.find(Attr_DW_FORM_ref_sig8), 0));
345 EXPECT_EQ(UData[0], toReference(DieDG.find(Attr_DW_FORM_ref_udata), 0));
347 //----------------------------------------------------------------------
348 // Test flag forms
349 //----------------------------------------------------------------------
350 EXPECT_EQ(1ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_true), 0));
351 EXPECT_EQ(0ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_false), 1));
352 if (Version >= 4) {
353 EXPECT_EQ(1ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_present), 0));
356 //----------------------------------------------------------------------
357 // Test SLEB128 based forms
358 //----------------------------------------------------------------------
359 EXPECT_EQ(SData, toSigned(DieDG.find(Attr_DW_FORM_sdata), 0));
360 if (Version >= 5) {
361 EXPECT_EQ(ICSData, toSigned(DieDG.find(Attr_DW_FORM_implicit_const), 0));
364 //----------------------------------------------------------------------
365 // Test ULEB128 based forms
366 //----------------------------------------------------------------------
367 EXPECT_EQ(UData[0], toUnsigned(DieDG.find(Attr_DW_FORM_udata), 0));
369 //----------------------------------------------------------------------
370 // Test DWARF32/DWARF64 forms
371 //----------------------------------------------------------------------
372 EXPECT_EQ(Dwarf32Values[0],
373 toReference(DieDG.find(Attr_DW_FORM_GNU_ref_alt), 0));
374 if (Version >= 4) {
375 EXPECT_EQ(Dwarf32Values[1],
376 toSectionOffset(DieDG.find(Attr_DW_FORM_sec_offset), 0));
379 //----------------------------------------------------------------------
380 // Add an address at the end to make sure we can decode this value
381 //----------------------------------------------------------------------
382 EXPECT_EQ(AddrValue, toAddress(DieDG.find(Attr_Last), 0));
385 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4AllForms) {
386 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
387 // addresses.
388 typedef uint32_t AddrType;
389 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2.
390 typedef AddrType RefAddrType;
391 TestAllForms<2, AddrType, RefAddrType>();
394 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8AllForms) {
395 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
396 // addresses.
397 typedef uint64_t AddrType;
398 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2.
399 typedef AddrType RefAddrType;
400 TestAllForms<2, AddrType, RefAddrType>();
403 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4AllForms) {
404 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
405 // addresses.
406 typedef uint32_t AddrType;
407 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later.
408 typedef uint32_t RefAddrType;
409 TestAllForms<3, AddrType, RefAddrType>();
412 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8AllForms) {
413 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
414 // addresses.
415 typedef uint64_t AddrType;
416 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
417 typedef uint32_t RefAddrType;
418 TestAllForms<3, AddrType, RefAddrType>();
421 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4AllForms) {
422 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
423 // addresses.
424 typedef uint32_t AddrType;
425 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
426 typedef uint32_t RefAddrType;
427 TestAllForms<4, AddrType, RefAddrType>();
430 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8AllForms) {
431 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
432 // addresses.
433 typedef uint64_t AddrType;
434 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
435 typedef uint32_t RefAddrType;
436 TestAllForms<4, AddrType, RefAddrType>();
439 TEST(DWARFDebugInfo, TestDWARF32Version5Addr4AllForms) {
440 // Test that we can decode all forms for DWARF32, version 5, with 4 byte
441 // addresses.
442 typedef uint32_t AddrType;
443 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
444 typedef uint32_t RefAddrType;
445 TestAllForms<5, AddrType, RefAddrType>();
448 TEST(DWARFDebugInfo, TestDWARF32Version5Addr8AllForms) {
449 // Test that we can decode all forms for DWARF32, version 5, with 8 byte
450 // addresses.
451 typedef uint64_t AddrType;
452 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
453 typedef uint32_t RefAddrType;
454 TestAllForms<5, AddrType, RefAddrType>();
457 template <uint16_t Version, class AddrType> void TestChildren() {
458 Triple Triple = getDefaultTargetTripleForAddrSize(sizeof(AddrType));
459 if (!isConfigurationSupported(Triple))
460 return;
462 // Test that we can decode DW_FORM_ref_addr values correctly in DWARF 2 with
463 // 4 byte addresses. DW_FORM_ref_addr values should be 4 bytes when using
464 // 8 byte addresses.
466 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
467 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
468 dwarfgen::Generator *DG = ExpectedDG.get().get();
469 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
470 dwarfgen::DIE CUDie = CU.getUnitDIE();
472 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
473 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
475 dwarfgen::DIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram);
476 SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main");
477 SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U);
478 SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U);
480 dwarfgen::DIE IntDie = CUDie.addChild(DW_TAG_base_type);
481 IntDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");
482 IntDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
483 IntDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
485 dwarfgen::DIE ArgcDie = SubprogramDie.addChild(DW_TAG_formal_parameter);
486 ArgcDie.addAttribute(DW_AT_name, DW_FORM_strp, "argc");
487 // ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref4, IntDie);
488 ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, IntDie);
490 StringRef FileBytes = DG->generate();
491 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
492 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
493 EXPECT_TRUE((bool)Obj);
494 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
496 // Verify the number of compile units is correct.
497 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
498 EXPECT_EQ(NumCUs, 1u);
499 DWARFCompileUnit *U =
500 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));
502 // Get the compile unit DIE is valid.
503 auto DieDG = U->getUnitDIE(false);
504 EXPECT_TRUE(DieDG.isValid());
506 // Verify the first child of the compile unit DIE is our subprogram.
507 auto SubprogramDieDG = DieDG.getFirstChild();
508 EXPECT_TRUE(SubprogramDieDG.isValid());
509 EXPECT_EQ(SubprogramDieDG.getTag(), DW_TAG_subprogram);
511 // Verify the first child of the subprogram is our formal parameter.
512 auto ArgcDieDG = SubprogramDieDG.getFirstChild();
513 EXPECT_TRUE(ArgcDieDG.isValid());
514 EXPECT_EQ(ArgcDieDG.getTag(), DW_TAG_formal_parameter);
516 // Verify our formal parameter has a NULL tag sibling.
517 auto NullDieDG = ArgcDieDG.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());
525 // Verify the sibling of our subprogram is our integer base type.
526 auto IntDieDG = SubprogramDieDG.getSibling();
527 EXPECT_TRUE(IntDieDG.isValid());
528 EXPECT_EQ(IntDieDG.getTag(), DW_TAG_base_type);
530 // Verify the sibling of our subprogram is our integer base is a NULL tag.
531 NullDieDG = IntDieDG.getSibling();
532 EXPECT_TRUE(NullDieDG.isValid());
533 if (NullDieDG) {
534 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null);
535 EXPECT_TRUE(!NullDieDG.getSibling().isValid());
536 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid());
539 // Verify the previous sibling of our subprogram is our integer base type.
540 IntDieDG = NullDieDG.getPreviousSibling();
541 EXPECT_TRUE(IntDieDG.isValid());
542 EXPECT_EQ(IntDieDG.getTag(), DW_TAG_base_type);
545 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Children) {
546 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
547 // addresses.
548 typedef uint32_t AddrType;
549 TestChildren<2, AddrType>();
552 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Children) {
553 // Test that we can decode all forms for DWARF32, version 2, with 8 byte
554 // addresses.
555 typedef uint64_t AddrType;
556 TestChildren<2, AddrType>();
559 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Children) {
560 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
561 // addresses.
562 typedef uint32_t AddrType;
563 TestChildren<3, AddrType>();
566 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Children) {
567 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
568 // addresses.
569 typedef uint64_t AddrType;
570 TestChildren<3, AddrType>();
573 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Children) {
574 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
575 // addresses.
576 typedef uint32_t AddrType;
577 TestChildren<4, AddrType>();
580 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Children) {
581 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
582 // addresses.
583 typedef uint64_t AddrType;
584 TestChildren<4, AddrType>();
587 template <uint16_t Version, class AddrType> void TestReferences() {
588 Triple Triple = getDefaultTargetTripleForAddrSize(sizeof(AddrType));
589 if (!isConfigurationSupported(Triple))
590 return;
592 // Test that we can decode DW_FORM_refXXX values correctly in DWARF.
593 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
594 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
595 dwarfgen::Generator *DG = ExpectedDG.get().get();
596 dwarfgen::CompileUnit &CU1 = DG->addCompileUnit();
597 dwarfgen::CompileUnit &CU2 = DG->addCompileUnit();
599 dwarfgen::DIE CU1Die = CU1.getUnitDIE();
600 CU1Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
601 CU1Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
603 dwarfgen::DIE CU1TypeDie = CU1Die.addChild(DW_TAG_base_type);
604 CU1TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");
605 CU1TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
606 CU1TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
608 dwarfgen::DIE CU1Ref1Die = CU1Die.addChild(DW_TAG_variable);
609 CU1Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref1");
610 CU1Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU1TypeDie);
612 dwarfgen::DIE CU1Ref2Die = CU1Die.addChild(DW_TAG_variable);
613 CU1Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref2");
614 CU1Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU1TypeDie);
616 dwarfgen::DIE CU1Ref4Die = CU1Die.addChild(DW_TAG_variable);
617 CU1Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref4");
618 CU1Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU1TypeDie);
620 dwarfgen::DIE CU1Ref8Die = CU1Die.addChild(DW_TAG_variable);
621 CU1Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref8");
622 CU1Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU1TypeDie);
624 dwarfgen::DIE CU1RefAddrDie = CU1Die.addChild(DW_TAG_variable);
625 CU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1RefAddr");
626 CU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie);
628 dwarfgen::DIE CU2Die = CU2.getUnitDIE();
629 CU2Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/foo.c");
630 CU2Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
632 dwarfgen::DIE CU2TypeDie = CU2Die.addChild(DW_TAG_base_type);
633 CU2TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "float");
634 CU2TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_float);
635 CU2TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
637 dwarfgen::DIE CU2Ref1Die = CU2Die.addChild(DW_TAG_variable);
638 CU2Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref1");
639 CU2Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU2TypeDie);
641 dwarfgen::DIE CU2Ref2Die = CU2Die.addChild(DW_TAG_variable);
642 CU2Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref2");
643 CU2Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU2TypeDie);
645 dwarfgen::DIE CU2Ref4Die = CU2Die.addChild(DW_TAG_variable);
646 CU2Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref4");
647 CU2Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU2TypeDie);
649 dwarfgen::DIE CU2Ref8Die = CU2Die.addChild(DW_TAG_variable);
650 CU2Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref8");
651 CU2Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU2TypeDie);
653 dwarfgen::DIE CU2RefAddrDie = CU2Die.addChild(DW_TAG_variable);
654 CU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2RefAddr");
655 CU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie);
657 // Refer to a type in CU1 from CU2
658 dwarfgen::DIE CU2ToCU1RefAddrDie = CU2Die.addChild(DW_TAG_variable);
659 CU2ToCU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2ToCU1RefAddr");
660 CU2ToCU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie);
662 // Refer to a type in CU2 from CU1
663 dwarfgen::DIE CU1ToCU2RefAddrDie = CU1Die.addChild(DW_TAG_variable);
664 CU1ToCU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1ToCU2RefAddr");
665 CU1ToCU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie);
667 StringRef FileBytes = DG->generate();
668 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
669 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
670 EXPECT_TRUE((bool)Obj);
671 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
673 // Verify the number of compile units is correct.
674 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
675 EXPECT_EQ(NumCUs, 2u);
676 DWARFCompileUnit *U1 =
677 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));
678 DWARFCompileUnit *U2 =
679 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(1));
681 // Get the compile unit DIE is valid.
682 auto Unit1DieDG = U1->getUnitDIE(false);
683 EXPECT_TRUE(Unit1DieDG.isValid());
685 auto Unit2DieDG = U2->getUnitDIE(false);
686 EXPECT_TRUE(Unit2DieDG.isValid());
688 // Verify the first child of the compile unit 1 DIE is our int base type.
689 auto CU1TypeDieDG = Unit1DieDG.getFirstChild();
690 EXPECT_TRUE(CU1TypeDieDG.isValid());
691 EXPECT_EQ(CU1TypeDieDG.getTag(), DW_TAG_base_type);
692 EXPECT_EQ(DW_ATE_signed, toUnsigned(CU1TypeDieDG.find(DW_AT_encoding), 0));
694 // Verify the first child of the compile unit 2 DIE is our float base type.
695 auto CU2TypeDieDG = Unit2DieDG.getFirstChild();
696 EXPECT_TRUE(CU2TypeDieDG.isValid());
697 EXPECT_EQ(CU2TypeDieDG.getTag(), DW_TAG_base_type);
698 EXPECT_EQ(DW_ATE_float, toUnsigned(CU2TypeDieDG.find(DW_AT_encoding), 0));
700 // Verify the sibling of the base type DIE is our Ref1 DIE and that its
701 // DW_AT_type points to our base type DIE.
702 auto CU1Ref1DieDG = CU1TypeDieDG.getSibling();
703 EXPECT_TRUE(CU1Ref1DieDG.isValid());
704 EXPECT_EQ(CU1Ref1DieDG.getTag(), DW_TAG_variable);
705 EXPECT_EQ(CU1TypeDieDG.getOffset(),
706 toReference(CU1Ref1DieDG.find(DW_AT_type), -1ULL));
707 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our
708 // base type DIE in CU1.
709 auto CU1Ref2DieDG = CU1Ref1DieDG.getSibling();
710 EXPECT_TRUE(CU1Ref2DieDG.isValid());
711 EXPECT_EQ(CU1Ref2DieDG.getTag(), DW_TAG_variable);
712 EXPECT_EQ(CU1TypeDieDG.getOffset(),
713 toReference(CU1Ref2DieDG.find(DW_AT_type), -1ULL));
715 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our
716 // base type DIE in CU1.
717 auto CU1Ref4DieDG = CU1Ref2DieDG.getSibling();
718 EXPECT_TRUE(CU1Ref4DieDG.isValid());
719 EXPECT_EQ(CU1Ref4DieDG.getTag(), DW_TAG_variable);
720 EXPECT_EQ(CU1TypeDieDG.getOffset(),
721 toReference(CU1Ref4DieDG.find(DW_AT_type), -1ULL));
723 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our
724 // base type DIE in CU1.
725 auto CU1Ref8DieDG = CU1Ref4DieDG.getSibling();
726 EXPECT_TRUE(CU1Ref8DieDG.isValid());
727 EXPECT_EQ(CU1Ref8DieDG.getTag(), DW_TAG_variable);
728 EXPECT_EQ(CU1TypeDieDG.getOffset(),
729 toReference(CU1Ref8DieDG.find(DW_AT_type), -1ULL));
731 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our
732 // base type DIE in CU1.
733 auto CU1RefAddrDieDG = CU1Ref8DieDG.getSibling();
734 EXPECT_TRUE(CU1RefAddrDieDG.isValid());
735 EXPECT_EQ(CU1RefAddrDieDG.getTag(), DW_TAG_variable);
736 EXPECT_EQ(CU1TypeDieDG.getOffset(),
737 toReference(CU1RefAddrDieDG.find(DW_AT_type), -1ULL));
739 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its
740 // DW_AT_type points to our base type DIE.
741 auto CU1ToCU2RefAddrDieDG = CU1RefAddrDieDG.getSibling();
742 EXPECT_TRUE(CU1ToCU2RefAddrDieDG.isValid());
743 EXPECT_EQ(CU1ToCU2RefAddrDieDG.getTag(), DW_TAG_variable);
744 EXPECT_EQ(CU2TypeDieDG.getOffset(),
745 toReference(CU1ToCU2RefAddrDieDG.find(DW_AT_type), -1ULL));
747 // Verify the sibling of the base type DIE is our Ref1 DIE and that its
748 // DW_AT_type points to our base type DIE.
749 auto CU2Ref1DieDG = CU2TypeDieDG.getSibling();
750 EXPECT_TRUE(CU2Ref1DieDG.isValid());
751 EXPECT_EQ(CU2Ref1DieDG.getTag(), DW_TAG_variable);
752 EXPECT_EQ(CU2TypeDieDG.getOffset(),
753 toReference(CU2Ref1DieDG.find(DW_AT_type), -1ULL));
754 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our
755 // base type DIE in CU2.
756 auto CU2Ref2DieDG = CU2Ref1DieDG.getSibling();
757 EXPECT_TRUE(CU2Ref2DieDG.isValid());
758 EXPECT_EQ(CU2Ref2DieDG.getTag(), DW_TAG_variable);
759 EXPECT_EQ(CU2TypeDieDG.getOffset(),
760 toReference(CU2Ref2DieDG.find(DW_AT_type), -1ULL));
762 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our
763 // base type DIE in CU2.
764 auto CU2Ref4DieDG = CU2Ref2DieDG.getSibling();
765 EXPECT_TRUE(CU2Ref4DieDG.isValid());
766 EXPECT_EQ(CU2Ref4DieDG.getTag(), DW_TAG_variable);
767 EXPECT_EQ(CU2TypeDieDG.getOffset(),
768 toReference(CU2Ref4DieDG.find(DW_AT_type), -1ULL));
770 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our
771 // base type DIE in CU2.
772 auto CU2Ref8DieDG = CU2Ref4DieDG.getSibling();
773 EXPECT_TRUE(CU2Ref8DieDG.isValid());
774 EXPECT_EQ(CU2Ref8DieDG.getTag(), DW_TAG_variable);
775 EXPECT_EQ(CU2TypeDieDG.getOffset(),
776 toReference(CU2Ref8DieDG.find(DW_AT_type), -1ULL));
778 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our
779 // base type DIE in CU2.
780 auto CU2RefAddrDieDG = CU2Ref8DieDG.getSibling();
781 EXPECT_TRUE(CU2RefAddrDieDG.isValid());
782 EXPECT_EQ(CU2RefAddrDieDG.getTag(), DW_TAG_variable);
783 EXPECT_EQ(CU2TypeDieDG.getOffset(),
784 toReference(CU2RefAddrDieDG.find(DW_AT_type), -1ULL));
786 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its
787 // DW_AT_type points to our base type DIE.
788 auto CU2ToCU1RefAddrDieDG = CU2RefAddrDieDG.getSibling();
789 EXPECT_TRUE(CU2ToCU1RefAddrDieDG.isValid());
790 EXPECT_EQ(CU2ToCU1RefAddrDieDG.getTag(), DW_TAG_variable);
791 EXPECT_EQ(CU1TypeDieDG.getOffset(),
792 toReference(CU2ToCU1RefAddrDieDG.find(DW_AT_type), -1ULL));
795 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4References) {
796 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
797 // addresses.
798 typedef uint32_t AddrType;
799 TestReferences<2, AddrType>();
802 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8References) {
803 // Test that we can decode all forms for DWARF32, version 2, with 8 byte
804 // addresses.
805 typedef uint64_t AddrType;
806 TestReferences<2, AddrType>();
809 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4References) {
810 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
811 // addresses.
812 typedef uint32_t AddrType;
813 TestReferences<3, AddrType>();
816 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8References) {
817 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
818 // addresses.
819 typedef uint64_t AddrType;
820 TestReferences<3, AddrType>();
823 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4References) {
824 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
825 // addresses.
826 typedef uint32_t AddrType;
827 TestReferences<4, AddrType>();
830 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8References) {
831 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
832 // addresses.
833 typedef uint64_t AddrType;
834 TestReferences<4, AddrType>();
837 template <uint16_t Version, class AddrType> void TestAddresses() {
838 Triple Triple = getDefaultTargetTripleForAddrSize(sizeof(AddrType));
839 if (!isConfigurationSupported(Triple))
840 return;
842 // Test the DWARF APIs related to accessing the DW_AT_low_pc and
843 // DW_AT_high_pc.
844 const bool SupportsHighPCAsOffset = Version >= 4;
845 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
846 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
847 dwarfgen::Generator *DG = ExpectedDG.get().get();
848 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
849 dwarfgen::DIE CUDie = CU.getUnitDIE();
851 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
852 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
854 // Create a subprogram DIE with no low or high PC.
855 dwarfgen::DIE SubprogramNoPC = CUDie.addChild(DW_TAG_subprogram);
856 SubprogramNoPC.addAttribute(DW_AT_name, DW_FORM_strp, "no_pc");
858 // Create a subprogram DIE with a low PC only.
859 dwarfgen::DIE SubprogramLowPC = CUDie.addChild(DW_TAG_subprogram);
860 SubprogramLowPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_pc");
861 const uint64_t ActualLowPC = 0x1000;
862 const uint64_t ActualHighPC = 0x2000;
863 const uint64_t ActualHighPCOffset = ActualHighPC - ActualLowPC;
864 SubprogramLowPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC);
866 // Create a subprogram DIE with a low and high PC.
867 dwarfgen::DIE SubprogramLowHighPC = CUDie.addChild(DW_TAG_subprogram);
868 SubprogramLowHighPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_high_pc");
869 SubprogramLowHighPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC);
870 // Encode the high PC as an offset from the low PC if supported.
871 if (SupportsHighPCAsOffset)
872 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_data4,
873 ActualHighPCOffset);
874 else
875 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_addr, ActualHighPC);
877 StringRef FileBytes = DG->generate();
878 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
879 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
880 EXPECT_TRUE((bool)Obj);
881 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
883 // Verify the number of compile units is correct.
884 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
885 EXPECT_EQ(NumCUs, 1u);
886 DWARFCompileUnit *U =
887 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));
889 // Get the compile unit DIE is valid.
890 auto DieDG = U->getUnitDIE(false);
891 EXPECT_TRUE(DieDG.isValid());
893 uint64_t LowPC, HighPC, SectionIndex;
894 Optional<uint64_t> OptU64;
895 // Verify the that our subprogram with no PC value fails appropriately when
896 // asked for any PC values.
897 auto SubprogramDieNoPC = DieDG.getFirstChild();
898 EXPECT_TRUE(SubprogramDieNoPC.isValid());
899 EXPECT_EQ(SubprogramDieNoPC.getTag(), DW_TAG_subprogram);
900 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_low_pc));
901 EXPECT_FALSE((bool)OptU64);
902 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_high_pc));
903 EXPECT_FALSE((bool)OptU64);
904 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC, SectionIndex));
905 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_high_pc));
906 EXPECT_FALSE((bool)OptU64);
907 OptU64 = toUnsigned(SubprogramDieNoPC.find(DW_AT_high_pc));
908 EXPECT_FALSE((bool)OptU64);
909 OptU64 = SubprogramDieNoPC.getHighPC(ActualLowPC);
910 EXPECT_FALSE((bool)OptU64);
911 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC, SectionIndex));
913 // Verify the that our subprogram with only a low PC value succeeds when
914 // we ask for the Low PC, but fails appropriately when asked for the high PC
915 // or both low and high PC values.
916 auto SubprogramDieLowPC = SubprogramDieNoPC.getSibling();
917 EXPECT_TRUE(SubprogramDieLowPC.isValid());
918 EXPECT_EQ(SubprogramDieLowPC.getTag(), DW_TAG_subprogram);
919 OptU64 = toAddress(SubprogramDieLowPC.find(DW_AT_low_pc));
920 EXPECT_TRUE((bool)OptU64);
921 EXPECT_EQ(OptU64.getValue(), ActualLowPC);
922 OptU64 = toAddress(SubprogramDieLowPC.find(DW_AT_high_pc));
923 EXPECT_FALSE((bool)OptU64);
924 OptU64 = toUnsigned(SubprogramDieLowPC.find(DW_AT_high_pc));
925 EXPECT_FALSE((bool)OptU64);
926 OptU64 = SubprogramDieLowPC.getHighPC(ActualLowPC);
927 EXPECT_FALSE((bool)OptU64);
928 EXPECT_FALSE(SubprogramDieLowPC.getLowAndHighPC(LowPC, HighPC, SectionIndex));
930 // Verify the that our subprogram with only a low PC value succeeds when
931 // we ask for the Low PC, but fails appropriately when asked for the high PC
932 // or both low and high PC values.
933 auto SubprogramDieLowHighPC = SubprogramDieLowPC.getSibling();
934 EXPECT_TRUE(SubprogramDieLowHighPC.isValid());
935 EXPECT_EQ(SubprogramDieLowHighPC.getTag(), DW_TAG_subprogram);
936 OptU64 = toAddress(SubprogramDieLowHighPC.find(DW_AT_low_pc));
937 EXPECT_TRUE((bool)OptU64);
938 EXPECT_EQ(OptU64.getValue(), ActualLowPC);
939 // Get the high PC as an address. This should succeed if the high PC was
940 // encoded as an address and fail if the high PC was encoded as an offset.
941 OptU64 = toAddress(SubprogramDieLowHighPC.find(DW_AT_high_pc));
942 if (SupportsHighPCAsOffset) {
943 EXPECT_FALSE((bool)OptU64);
944 } else {
945 EXPECT_TRUE((bool)OptU64);
946 EXPECT_EQ(OptU64.getValue(), ActualHighPC);
948 // Get the high PC as an unsigned constant. This should succeed if the high PC
949 // was encoded as an offset and fail if the high PC was encoded as an address.
950 OptU64 = toUnsigned(SubprogramDieLowHighPC.find(DW_AT_high_pc));
951 if (SupportsHighPCAsOffset) {
952 EXPECT_TRUE((bool)OptU64);
953 EXPECT_EQ(OptU64.getValue(), ActualHighPCOffset);
954 } else {
955 EXPECT_FALSE((bool)OptU64);
958 OptU64 = SubprogramDieLowHighPC.getHighPC(ActualLowPC);
959 EXPECT_TRUE((bool)OptU64);
960 EXPECT_EQ(OptU64.getValue(), ActualHighPC);
962 EXPECT_TRUE(SubprogramDieLowHighPC.getLowAndHighPC(LowPC, HighPC, SectionIndex));
963 EXPECT_EQ(LowPC, ActualLowPC);
964 EXPECT_EQ(HighPC, ActualHighPC);
967 TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Addresses) {
968 // Test that we can decode address values in DWARF32, version 2, with 4 byte
969 // addresses.
970 typedef uint32_t AddrType;
971 TestAddresses<2, AddrType>();
974 TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Addresses) {
975 // Test that we can decode address values in DWARF32, version 2, with 8 byte
976 // addresses.
977 typedef uint64_t AddrType;
978 TestAddresses<2, AddrType>();
981 TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Addresses) {
982 // Test that we can decode address values in DWARF32, version 3, with 4 byte
983 // addresses.
984 typedef uint32_t AddrType;
985 TestAddresses<3, AddrType>();
988 TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Addresses) {
989 // Test that we can decode address values in DWARF32, version 3, with 8 byte
990 // addresses.
991 typedef uint64_t AddrType;
992 TestAddresses<3, AddrType>();
995 TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Addresses) {
996 // Test that we can decode address values in DWARF32, version 4, with 4 byte
997 // addresses.
998 typedef uint32_t AddrType;
999 TestAddresses<4, AddrType>();
1002 TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Addresses) {
1003 // Test that we can decode address values in DWARF32, version 4, with 8 byte
1004 // addresses.
1005 typedef uint64_t AddrType;
1006 TestAddresses<4, AddrType>();
1009 TEST(DWARFDebugInfo, TestStringOffsets) {
1010 Triple Triple = getNormalizedDefaultTargetTriple();
1011 if (!isConfigurationSupported(Triple))
1012 return;
1014 const char *String1 = "Hello";
1015 const char *String2 = "World";
1017 auto ExpectedDG = dwarfgen::Generator::create(Triple, 5);
1018 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
1019 dwarfgen::Generator *DG = ExpectedDG.get().get();
1020 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1021 dwarfgen::DIE CUDie = CU.getUnitDIE();
1023 CUDie.addStrOffsetsBaseAttribute();
1025 uint16_t Attr = DW_AT_lo_user;
1027 // Create our strings. First we create a non-indexed reference to String1,
1028 // followed by an indexed String2. Finally, we add an indexed reference to
1029 // String1.
1030 const auto Attr1 = static_cast<dwarf::Attribute>(Attr++);
1031 CUDie.addAttribute(Attr1, DW_FORM_strp, String1);
1033 const auto Attr2 = static_cast<dwarf::Attribute>(Attr++);
1034 CUDie.addAttribute(Attr2, DW_FORM_strx, String2);
1036 const auto Attr3 = static_cast<dwarf::Attribute>(Attr++);
1037 CUDie.addAttribute(Attr3, DW_FORM_strx, String1);
1039 // Generate the DWARF
1040 StringRef FileBytes = DG->generate();
1041 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
1042 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1043 ASSERT_TRUE((bool)Obj);
1044 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
1045 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
1046 ASSERT_EQ(NumCUs, 1u);
1047 DWARFUnit *U = DwarfContext->getUnitAtIndex(0);
1048 auto DieDG = U->getUnitDIE(false);
1049 ASSERT_TRUE(DieDG.isValid());
1051 // Now make sure the string offsets came out properly. Attr2 should have index
1052 // 0 (because it was the first indexed string) even though the string itself
1053 // was added eariler.
1054 auto Extracted1 = toString(DieDG.find(Attr1));
1055 ASSERT_TRUE((bool)Extracted1);
1056 EXPECT_STREQ(String1, *Extracted1);
1058 Optional<DWARFFormValue> Form2 = DieDG.find(Attr2);
1059 ASSERT_TRUE((bool)Form2);
1060 EXPECT_EQ(0u, Form2->getRawUValue());
1061 auto Extracted2 = toString(Form2);
1062 ASSERT_TRUE((bool)Extracted2);
1063 EXPECT_STREQ(String2, *Extracted2);
1065 Optional<DWARFFormValue> Form3 = DieDG.find(Attr3);
1066 ASSERT_TRUE((bool)Form3);
1067 EXPECT_EQ(1u, Form3->getRawUValue());
1068 auto Extracted3 = toString(Form3);
1069 ASSERT_TRUE((bool)Extracted3);
1070 EXPECT_STREQ(String1, *Extracted3);
1073 TEST(DWARFDebugInfo, TestEmptyStringOffsets) {
1074 Triple Triple = getNormalizedDefaultTargetTriple();
1075 if (!isConfigurationSupported(Triple))
1076 return;
1078 const char *String1 = "Hello";
1080 auto ExpectedDG = dwarfgen::Generator::create(Triple, 5);
1081 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
1082 dwarfgen::Generator *DG = ExpectedDG.get().get();
1083 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1084 dwarfgen::DIE CUDie = CU.getUnitDIE();
1086 uint16_t Attr = DW_AT_lo_user;
1088 // We shall insert only one string. It will be referenced directly.
1089 const auto Attr1 = static_cast<dwarf::Attribute>(Attr++);
1090 CUDie.addAttribute(Attr1, DW_FORM_strp, String1);
1092 // Generate the DWARF
1093 StringRef FileBytes = DG->generate();
1094 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
1095 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1096 ASSERT_TRUE((bool)Obj);
1097 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
1098 EXPECT_TRUE(
1099 DwarfContext->getDWARFObj().getStrOffsetsSection().Data.empty());
1102 TEST(DWARFDebugInfo, TestRelations) {
1103 Triple Triple = getNormalizedDefaultTargetTriple();
1104 if (!isConfigurationSupported(Triple))
1105 return;
1107 // Test the DWARF APIs related to accessing the DW_AT_low_pc and
1108 // DW_AT_high_pc.
1109 uint16_t Version = 4;
1110 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1111 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
1112 dwarfgen::Generator *DG = ExpectedDG.get().get();
1113 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1115 enum class Tag: uint16_t {
1116 A = dwarf::DW_TAG_lo_user,
1125 // Scope to allow us to re-use the same DIE names
1127 // Create DWARF tree that looks like:
1129 // CU
1130 // A
1131 // B
1132 // C
1133 // C1
1134 // C2
1135 // D
1136 // D1
1137 dwarfgen::DIE CUDie = CU.getUnitDIE();
1138 dwarfgen::DIE A = CUDie.addChild((dwarf::Tag)Tag::A);
1139 A.addChild((dwarf::Tag)Tag::B);
1140 dwarfgen::DIE C = A.addChild((dwarf::Tag)Tag::C);
1141 dwarfgen::DIE D = A.addChild((dwarf::Tag)Tag::D);
1142 C.addChild((dwarf::Tag)Tag::C1);
1143 C.addChild((dwarf::Tag)Tag::C2);
1144 D.addChild((dwarf::Tag)Tag::D1);
1147 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1148 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1149 EXPECT_TRUE((bool)Obj);
1150 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
1152 // Verify the number of compile units is correct.
1153 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
1154 EXPECT_EQ(NumCUs, 1u);
1155 DWARFCompileUnit *U =
1156 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));
1158 // Get the compile unit DIE is valid.
1159 auto CUDie = U->getUnitDIE(false);
1160 EXPECT_TRUE(CUDie.isValid());
1162 // The compile unit doesn't have a parent or a sibling.
1163 auto ParentDie = CUDie.getParent();
1164 EXPECT_FALSE(ParentDie.isValid());
1165 auto SiblingDie = CUDie.getSibling();
1166 EXPECT_FALSE(SiblingDie.isValid());
1168 // Get the children of the compile unit
1169 auto A = CUDie.getFirstChild();
1170 auto B = A.getFirstChild();
1171 auto C = B.getSibling();
1172 auto D = C.getSibling();
1173 auto Null = D.getSibling();
1175 // Verify NULL Die is NULL and has no children or siblings
1176 EXPECT_TRUE(Null.isNULL());
1177 EXPECT_FALSE(Null.getSibling().isValid());
1178 EXPECT_FALSE(Null.getFirstChild().isValid());
1180 // Verify all children of the compile unit DIE are correct.
1181 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A);
1182 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B);
1183 EXPECT_EQ(C.getTag(), (dwarf::Tag)Tag::C);
1184 EXPECT_EQ(D.getTag(), (dwarf::Tag)Tag::D);
1186 // Verify who has children
1187 EXPECT_TRUE(A.hasChildren());
1188 EXPECT_FALSE(B.hasChildren());
1189 EXPECT_TRUE(C.hasChildren());
1190 EXPECT_TRUE(D.hasChildren());
1192 // Make sure the parent of all the children of the compile unit are the
1193 // compile unit.
1194 EXPECT_EQ(A.getParent(), CUDie);
1196 // Make sure the parent of all the children of A are the A.
1197 // B is the first child in A, so we need to verify we can get the previous
1198 // DIE as the parent.
1199 EXPECT_EQ(B.getParent(), A);
1200 // C is the second child in A, so we need to make sure we can backup across
1201 // other DIE (B) at the same level to get the correct parent.
1202 EXPECT_EQ(C.getParent(), A);
1203 // D is the third child of A. We need to verify we can backup across other DIE
1204 // (B and C) including DIE that have children (D) to get the correct parent.
1205 EXPECT_EQ(D.getParent(), A);
1207 // Verify that a DIE with no children returns an invalid DWARFDie.
1208 EXPECT_FALSE(B.getFirstChild().isValid());
1210 // Verify the children of the B DIE
1211 auto C1 = C.getFirstChild();
1212 auto C2 = C1.getSibling();
1213 EXPECT_TRUE(C2.getSibling().isNULL());
1215 // Verify all children of the B DIE correctly valid or invalid.
1216 EXPECT_EQ(C1.getTag(), (dwarf::Tag)Tag::C1);
1217 EXPECT_EQ(C2.getTag(), (dwarf::Tag)Tag::C2);
1219 // Make sure the parent of all the children of the B are the B.
1220 EXPECT_EQ(C1.getParent(), C);
1221 EXPECT_EQ(C2.getParent(), C);
1223 // Make sure iterators work as expected.
1224 EXPECT_THAT(std::vector<DWARFDie>(A.begin(), A.end()),
1225 testing::ElementsAre(B, C, D));
1226 EXPECT_THAT(std::vector<DWARFDie>(A.rbegin(), A.rend()),
1227 testing::ElementsAre(D, C, B));
1229 // Make sure conversion from reverse iterator works as expected.
1230 EXPECT_EQ(A.rbegin().base(), A.end());
1231 EXPECT_EQ(A.rend().base(), A.begin());
1233 // Make sure iterator is bidirectional.
1235 auto Begin = A.begin();
1236 auto End = A.end();
1237 auto It = A.begin();
1239 EXPECT_EQ(It, Begin);
1240 EXPECT_EQ(*It, B);
1241 ++It;
1242 EXPECT_EQ(*It, C);
1243 ++It;
1244 EXPECT_EQ(*It, D);
1245 ++It;
1246 EXPECT_EQ(It, End);
1247 --It;
1248 EXPECT_EQ(*It, D);
1249 --It;
1250 EXPECT_EQ(*It, C);
1251 --It;
1252 EXPECT_EQ(*It, B);
1253 EXPECT_EQ(It, Begin);
1256 // Make sure reverse iterator is bidirectional.
1258 auto Begin = A.rbegin();
1259 auto End = A.rend();
1260 auto It = A.rbegin();
1262 EXPECT_EQ(It, Begin);
1263 EXPECT_EQ(*It, D);
1264 ++It;
1265 EXPECT_EQ(*It, C);
1266 ++It;
1267 EXPECT_EQ(*It, B);
1268 ++It;
1269 EXPECT_EQ(It, End);
1270 --It;
1271 EXPECT_EQ(*It, B);
1272 --It;
1273 EXPECT_EQ(*It, C);
1274 --It;
1275 EXPECT_EQ(*It, D);
1276 EXPECT_EQ(It, Begin);
1280 TEST(DWARFDebugInfo, TestDWARFDie) {
1281 // Make sure a default constructed DWARFDie doesn't have any parent, sibling
1282 // or child;
1283 DWARFDie DefaultDie;
1284 EXPECT_FALSE(DefaultDie.getParent().isValid());
1285 EXPECT_FALSE(DefaultDie.getFirstChild().isValid());
1286 EXPECT_FALSE(DefaultDie.getSibling().isValid());
1289 TEST(DWARFDebugInfo, TestChildIterators) {
1290 Triple Triple = getNormalizedDefaultTargetTriple();
1291 if (!isConfigurationSupported(Triple))
1292 return;
1294 // Test the DWARF APIs related to iterating across the children of a DIE using
1295 // the DWARFDie::iterator class.
1296 uint16_t Version = 4;
1297 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1298 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
1299 dwarfgen::Generator *DG = ExpectedDG.get().get();
1300 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1302 enum class Tag: uint16_t {
1303 A = dwarf::DW_TAG_lo_user,
1307 // Scope to allow us to re-use the same DIE names
1309 // Create DWARF tree that looks like:
1311 // CU
1312 // A
1313 // B
1314 auto CUDie = CU.getUnitDIE();
1315 CUDie.addChild((dwarf::Tag)Tag::A);
1316 CUDie.addChild((dwarf::Tag)Tag::B);
1319 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1320 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1321 EXPECT_TRUE((bool)Obj);
1322 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
1324 // Verify the number of compile units is correct.
1325 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
1326 EXPECT_EQ(NumCUs, 1u);
1327 DWARFCompileUnit *U =
1328 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));
1330 // Get the compile unit DIE is valid.
1331 auto CUDie = U->getUnitDIE(false);
1332 EXPECT_TRUE(CUDie.isValid());
1333 uint32_t Index;
1334 DWARFDie A;
1335 DWARFDie B;
1337 // Verify the compile unit DIE's children.
1338 Index = 0;
1339 for (auto Die : CUDie.children()) {
1340 switch (Index++) {
1341 case 0: A = Die; break;
1342 case 1: B = Die; break;
1346 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A);
1347 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B);
1349 // Verify that A has no children by verifying that the begin and end contain
1350 // invalid DIEs and also that the iterators are equal.
1351 EXPECT_EQ(A.begin(), A.end());
1354 TEST(DWARFDebugInfo, TestChildIteratorsOnInvalidDie) {
1355 // Verify that an invalid DIE has no children.
1356 DWARFDie Invalid;
1357 auto begin = Invalid.begin();
1358 auto end = Invalid.end();
1359 EXPECT_FALSE(begin->isValid());
1360 EXPECT_FALSE(end->isValid());
1361 EXPECT_EQ(begin, end);
1364 TEST(DWARFDebugInfo, TestEmptyChildren) {
1365 const char *yamldata = "debug_abbrev:\n"
1366 " - Code: 0x00000001\n"
1367 " Tag: DW_TAG_compile_unit\n"
1368 " Children: DW_CHILDREN_yes\n"
1369 " Attributes:\n"
1370 "debug_info:\n"
1371 " - Length:\n"
1372 " TotalLength: 0\n"
1373 " Version: 4\n"
1374 " AbbrOffset: 0\n"
1375 " AddrSize: 8\n"
1376 " Entries:\n"
1377 " - AbbrCode: 0x00000001\n"
1378 " Values:\n"
1379 " - AbbrCode: 0x00000000\n"
1380 " Values:\n";
1382 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata), true);
1383 ASSERT_TRUE((bool)ErrOrSections);
1384 std::unique_ptr<DWARFContext> DwarfContext =
1385 DWARFContext::create(*ErrOrSections, 8);
1387 // Verify the number of compile units is correct.
1388 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
1389 EXPECT_EQ(NumCUs, 1u);
1390 DWARFCompileUnit *U =
1391 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));
1393 // Get the compile unit DIE is valid.
1394 auto CUDie = U->getUnitDIE(false);
1395 EXPECT_TRUE(CUDie.isValid());
1397 // Verify that the CU Die that says it has children, but doesn't, actually
1398 // has begin and end iterators that are equal. We want to make sure we don't
1399 // see the Null DIEs during iteration.
1400 EXPECT_EQ(CUDie.begin(), CUDie.end());
1403 TEST(DWARFDebugInfo, TestAttributeIterators) {
1404 Triple Triple = getNormalizedDefaultTargetTriple();
1405 if (!isConfigurationSupported(Triple))
1406 return;
1408 // Test the DWARF APIs related to iterating across all attribute values in a
1409 // a DWARFDie.
1410 uint16_t Version = 4;
1411 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1412 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
1413 dwarfgen::Generator *DG = ExpectedDG.get().get();
1414 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1415 const uint64_t CULowPC = 0x1000;
1416 StringRef CUPath("/tmp/main.c");
1418 // Scope to allow us to re-use the same DIE names
1420 auto CUDie = CU.getUnitDIE();
1421 // Encode an attribute value before an attribute with no data.
1422 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, CUPath.data());
1423 // Encode an attribute value with no data in .debug_info/types to ensure
1424 // the iteration works correctly.
1425 CUDie.addAttribute(DW_AT_declaration, DW_FORM_flag_present);
1426 // Encode an attribute value after an attribute with no data.
1427 CUDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, CULowPC);
1430 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1431 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1432 EXPECT_TRUE((bool)Obj);
1433 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
1435 // Verify the number of compile units is correct.
1436 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
1437 EXPECT_EQ(NumCUs, 1u);
1438 DWARFCompileUnit *U =
1439 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));
1441 // Get the compile unit DIE is valid.
1442 auto CUDie = U->getUnitDIE(false);
1443 EXPECT_TRUE(CUDie.isValid());
1445 auto R = CUDie.attributes();
1446 auto I = R.begin();
1447 auto E = R.end();
1449 ASSERT_NE(E, I);
1450 EXPECT_EQ(I->Attr, DW_AT_name);
1451 auto ActualCUPath = I->Value.getAsCString();
1452 EXPECT_EQ(CUPath, *ActualCUPath);
1454 ASSERT_NE(E, ++I);
1455 EXPECT_EQ(I->Attr, DW_AT_declaration);
1456 EXPECT_EQ(1ull, *I->Value.getAsUnsignedConstant());
1458 ASSERT_NE(E, ++I);
1459 EXPECT_EQ(I->Attr, DW_AT_low_pc);
1460 EXPECT_EQ(CULowPC, *I->Value.getAsAddress());
1462 EXPECT_EQ(E, ++I);
1465 TEST(DWARFDebugInfo, TestFindRecurse) {
1466 Triple Triple = getNormalizedDefaultTargetTriple();
1467 if (!isConfigurationSupported(Triple))
1468 return;
1470 uint16_t Version = 4;
1471 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1472 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
1473 dwarfgen::Generator *DG = ExpectedDG.get().get();
1474 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1476 StringRef SpecDieName = "spec";
1477 StringRef SpecLinkageName = "spec_linkage";
1478 StringRef AbsDieName = "abs";
1479 // Scope to allow us to re-use the same DIE names
1481 auto CUDie = CU.getUnitDIE();
1482 auto FuncSpecDie = CUDie.addChild(DW_TAG_subprogram);
1483 auto FuncAbsDie = CUDie.addChild(DW_TAG_subprogram);
1484 // Put the linkage name in a second abstract origin DIE to ensure we
1485 // recurse through more than just one DIE when looking for attributes.
1486 auto FuncAbsDie2 = CUDie.addChild(DW_TAG_subprogram);
1487 auto FuncDie = CUDie.addChild(DW_TAG_subprogram);
1488 auto VarAbsDie = CUDie.addChild(DW_TAG_variable);
1489 auto VarDie = CUDie.addChild(DW_TAG_variable);
1490 FuncSpecDie.addAttribute(DW_AT_name, DW_FORM_strp, SpecDieName);
1491 FuncAbsDie2.addAttribute(DW_AT_linkage_name, DW_FORM_strp, SpecLinkageName);
1492 FuncAbsDie.addAttribute(DW_AT_specification, DW_FORM_ref4, FuncSpecDie);
1493 FuncAbsDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, FuncAbsDie2);
1494 FuncDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, FuncAbsDie);
1495 VarAbsDie.addAttribute(DW_AT_name, DW_FORM_strp, AbsDieName);
1496 VarDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, VarAbsDie);
1499 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1500 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1501 EXPECT_TRUE((bool)Obj);
1502 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
1504 // Verify the number of compile units is correct.
1505 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
1506 EXPECT_EQ(NumCUs, 1u);
1507 DWARFCompileUnit *U =
1508 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));
1510 // Get the compile unit DIE is valid.
1511 auto CUDie = U->getUnitDIE(false);
1512 EXPECT_TRUE(CUDie.isValid());
1514 auto FuncSpecDie = CUDie.getFirstChild();
1515 auto FuncAbsDie = FuncSpecDie.getSibling();
1516 auto FuncAbsDie2 = FuncAbsDie.getSibling();
1517 auto FuncDie = FuncAbsDie2.getSibling();
1518 auto VarAbsDie = FuncDie.getSibling();
1519 auto VarDie = VarAbsDie.getSibling();
1521 // Make sure we can't extract the name from the specification die when using
1522 // DWARFDie::find() since it won't check the DW_AT_specification DIE.
1523 EXPECT_FALSE(FuncDie.find(DW_AT_name));
1525 // Make sure we can extract the name from the specification die when using
1526 // DWARFDie::findRecursively() since it should recurse through the
1527 // DW_AT_specification DIE.
1528 auto NameOpt = FuncDie.findRecursively(DW_AT_name);
1529 EXPECT_TRUE(NameOpt);
1530 // Test the dwarf::toString() helper function.
1531 auto StringOpt = toString(NameOpt);
1532 EXPECT_TRUE(StringOpt);
1533 EXPECT_EQ(SpecDieName, StringOpt.getValueOr(nullptr));
1534 // Test the dwarf::toString() helper function with a default value specified.
1535 EXPECT_EQ(SpecDieName, toString(NameOpt, nullptr));
1537 auto LinkageNameOpt = FuncDie.findRecursively(DW_AT_linkage_name);
1538 EXPECT_EQ(SpecLinkageName, toString(LinkageNameOpt).getValueOr(nullptr));
1540 // Make sure we can't extract the name from the abstract origin die when using
1541 // DWARFDie::find() since it won't check the DW_AT_abstract_origin DIE.
1542 EXPECT_FALSE(VarDie.find(DW_AT_name));
1544 // Make sure we can extract the name from the abstract origin die when using
1545 // DWARFDie::findRecursively() since it should recurse through the
1546 // DW_AT_abstract_origin DIE.
1547 NameOpt = VarDie.findRecursively(DW_AT_name);
1548 EXPECT_TRUE(NameOpt);
1549 // Test the dwarf::toString() helper function.
1550 StringOpt = toString(NameOpt);
1551 EXPECT_TRUE(StringOpt);
1552 EXPECT_EQ(AbsDieName, StringOpt.getValueOr(nullptr));
1555 TEST(DWARFDebugInfo, TestDwarfToFunctions) {
1556 // Test all of the dwarf::toXXX functions that take a
1557 // Optional<DWARFFormValue> and extract the values from it.
1558 uint64_t InvalidU64 = 0xBADBADBADBADBADB;
1559 int64_t InvalidS64 = 0xBADBADBADBADBADB;
1561 // First test that we don't get valid values back when using an optional with
1562 // no value.
1563 Optional<DWARFFormValue> FormValOpt1 = DWARFFormValue();
1564 EXPECT_FALSE(toString(FormValOpt1).hasValue());
1565 EXPECT_FALSE(toUnsigned(FormValOpt1).hasValue());
1566 EXPECT_FALSE(toReference(FormValOpt1).hasValue());
1567 EXPECT_FALSE(toSigned(FormValOpt1).hasValue());
1568 EXPECT_FALSE(toAddress(FormValOpt1).hasValue());
1569 EXPECT_FALSE(toSectionOffset(FormValOpt1).hasValue());
1570 EXPECT_FALSE(toBlock(FormValOpt1).hasValue());
1571 EXPECT_EQ(nullptr, toString(FormValOpt1, nullptr));
1572 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt1, InvalidU64));
1573 EXPECT_EQ(InvalidU64, toReference(FormValOpt1, InvalidU64));
1574 EXPECT_EQ(InvalidU64, toAddress(FormValOpt1, InvalidU64));
1575 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt1, InvalidU64));
1576 EXPECT_EQ(InvalidS64, toSigned(FormValOpt1, InvalidS64));
1578 // Test successful and unsuccessful address decoding.
1579 uint64_t Address = 0x100000000ULL;
1580 Optional<DWARFFormValue> FormValOpt2 =
1581 DWARFFormValue::createFromUValue(DW_FORM_addr, Address);
1583 EXPECT_FALSE(toString(FormValOpt2).hasValue());
1584 EXPECT_FALSE(toUnsigned(FormValOpt2).hasValue());
1585 EXPECT_FALSE(toReference(FormValOpt2).hasValue());
1586 EXPECT_FALSE(toSigned(FormValOpt2).hasValue());
1587 EXPECT_TRUE(toAddress(FormValOpt2).hasValue());
1588 EXPECT_FALSE(toSectionOffset(FormValOpt2).hasValue());
1589 EXPECT_FALSE(toBlock(FormValOpt2).hasValue());
1590 EXPECT_EQ(nullptr, toString(FormValOpt2, nullptr));
1591 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt2, InvalidU64));
1592 EXPECT_EQ(InvalidU64, toReference(FormValOpt2, InvalidU64));
1593 EXPECT_EQ(Address, toAddress(FormValOpt2, InvalidU64));
1594 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt2, InvalidU64));
1595 EXPECT_EQ(InvalidS64, toSigned(FormValOpt2, InvalidU64));
1597 // Test successful and unsuccessful unsigned constant decoding.
1598 uint64_t UData8 = 0x1020304050607080ULL;
1599 Optional<DWARFFormValue> FormValOpt3 =
1600 DWARFFormValue::createFromUValue(DW_FORM_udata, UData8);
1602 EXPECT_FALSE(toString(FormValOpt3).hasValue());
1603 EXPECT_TRUE(toUnsigned(FormValOpt3).hasValue());
1604 EXPECT_FALSE(toReference(FormValOpt3).hasValue());
1605 EXPECT_TRUE(toSigned(FormValOpt3).hasValue());
1606 EXPECT_FALSE(toAddress(FormValOpt3).hasValue());
1607 EXPECT_FALSE(toSectionOffset(FormValOpt3).hasValue());
1608 EXPECT_FALSE(toBlock(FormValOpt3).hasValue());
1609 EXPECT_EQ(nullptr, toString(FormValOpt3, nullptr));
1610 EXPECT_EQ(UData8, toUnsigned(FormValOpt3, InvalidU64));
1611 EXPECT_EQ(InvalidU64, toReference(FormValOpt3, InvalidU64));
1612 EXPECT_EQ(InvalidU64, toAddress(FormValOpt3, InvalidU64));
1613 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt3, InvalidU64));
1614 EXPECT_EQ((int64_t)UData8, toSigned(FormValOpt3, InvalidU64));
1616 // Test successful and unsuccessful reference decoding.
1617 uint32_t RefData = 0x11223344U;
1618 Optional<DWARFFormValue> FormValOpt4 =
1619 DWARFFormValue::createFromUValue(DW_FORM_ref_addr, RefData);
1621 EXPECT_FALSE(toString(FormValOpt4).hasValue());
1622 EXPECT_FALSE(toUnsigned(FormValOpt4).hasValue());
1623 EXPECT_TRUE(toReference(FormValOpt4).hasValue());
1624 EXPECT_FALSE(toSigned(FormValOpt4).hasValue());
1625 EXPECT_FALSE(toAddress(FormValOpt4).hasValue());
1626 EXPECT_FALSE(toSectionOffset(FormValOpt4).hasValue());
1627 EXPECT_FALSE(toBlock(FormValOpt4).hasValue());
1628 EXPECT_EQ(nullptr, toString(FormValOpt4, nullptr));
1629 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt4, InvalidU64));
1630 EXPECT_EQ(RefData, toReference(FormValOpt4, InvalidU64));
1631 EXPECT_EQ(InvalidU64, toAddress(FormValOpt4, InvalidU64));
1632 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt4, InvalidU64));
1633 EXPECT_EQ(InvalidS64, toSigned(FormValOpt4, InvalidU64));
1635 // Test successful and unsuccessful signed constant decoding.
1636 int64_t SData8 = 0x1020304050607080ULL;
1637 Optional<DWARFFormValue> FormValOpt5 =
1638 DWARFFormValue::createFromSValue(DW_FORM_udata, SData8);
1640 EXPECT_FALSE(toString(FormValOpt5).hasValue());
1641 EXPECT_TRUE(toUnsigned(FormValOpt5).hasValue());
1642 EXPECT_FALSE(toReference(FormValOpt5).hasValue());
1643 EXPECT_TRUE(toSigned(FormValOpt5).hasValue());
1644 EXPECT_FALSE(toAddress(FormValOpt5).hasValue());
1645 EXPECT_FALSE(toSectionOffset(FormValOpt5).hasValue());
1646 EXPECT_FALSE(toBlock(FormValOpt5).hasValue());
1647 EXPECT_EQ(nullptr, toString(FormValOpt5, nullptr));
1648 EXPECT_EQ((uint64_t)SData8, toUnsigned(FormValOpt5, InvalidU64));
1649 EXPECT_EQ(InvalidU64, toReference(FormValOpt5, InvalidU64));
1650 EXPECT_EQ(InvalidU64, toAddress(FormValOpt5, InvalidU64));
1651 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt5, InvalidU64));
1652 EXPECT_EQ(SData8, toSigned(FormValOpt5, InvalidU64));
1654 // Test successful and unsuccessful block decoding.
1655 uint8_t Data[] = { 2, 3, 4 };
1656 ArrayRef<uint8_t> Array(Data);
1657 Optional<DWARFFormValue> FormValOpt6 =
1658 DWARFFormValue::createFromBlockValue(DW_FORM_block1, Array);
1660 EXPECT_FALSE(toString(FormValOpt6).hasValue());
1661 EXPECT_FALSE(toUnsigned(FormValOpt6).hasValue());
1662 EXPECT_FALSE(toReference(FormValOpt6).hasValue());
1663 EXPECT_FALSE(toSigned(FormValOpt6).hasValue());
1664 EXPECT_FALSE(toAddress(FormValOpt6).hasValue());
1665 EXPECT_FALSE(toSectionOffset(FormValOpt6).hasValue());
1666 auto BlockOpt = toBlock(FormValOpt6);
1667 EXPECT_TRUE(BlockOpt.hasValue());
1668 EXPECT_EQ(*BlockOpt, Array);
1669 EXPECT_EQ(nullptr, toString(FormValOpt6, nullptr));
1670 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt6, InvalidU64));
1671 EXPECT_EQ(InvalidU64, toReference(FormValOpt6, InvalidU64));
1672 EXPECT_EQ(InvalidU64, toAddress(FormValOpt6, InvalidU64));
1673 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt6, InvalidU64));
1674 EXPECT_EQ(InvalidS64, toSigned(FormValOpt6, InvalidU64));
1676 // Test
1679 TEST(DWARFDebugInfo, TestFindAttrs) {
1680 Triple Triple = getNormalizedDefaultTargetTriple();
1681 if (!isConfigurationSupported(Triple))
1682 return;
1684 // Test the DWARFDie::find() and DWARFDie::findRecursively() that take an
1685 // ArrayRef<dwarf::Attribute> value to make sure they work correctly.
1686 uint16_t Version = 4;
1687 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1688 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
1689 dwarfgen::Generator *DG = ExpectedDG.get().get();
1690 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1692 StringRef DieMangled("_Z3fooi");
1693 // Scope to allow us to re-use the same DIE names
1695 auto CUDie = CU.getUnitDIE();
1696 auto FuncSpecDie = CUDie.addChild(DW_TAG_subprogram);
1697 auto FuncDie = CUDie.addChild(DW_TAG_subprogram);
1698 FuncSpecDie.addAttribute(DW_AT_MIPS_linkage_name, DW_FORM_strp, DieMangled);
1699 FuncDie.addAttribute(DW_AT_specification, DW_FORM_ref4, FuncSpecDie);
1702 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1703 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1704 EXPECT_TRUE((bool)Obj);
1705 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
1707 // Verify the number of compile units is correct.
1708 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
1709 EXPECT_EQ(NumCUs, 1u);
1710 DWARFCompileUnit *U =
1711 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));
1713 // Get the compile unit DIE is valid.
1714 auto CUDie = U->getUnitDIE(false);
1715 EXPECT_TRUE(CUDie.isValid());
1717 auto FuncSpecDie = CUDie.getFirstChild();
1718 auto FuncDie = FuncSpecDie.getSibling();
1720 // Make sure that passing in an empty attribute list behave correctly.
1721 EXPECT_FALSE(FuncDie.find(ArrayRef<dwarf::Attribute>()).hasValue());
1723 // Make sure that passing in a list of attribute that are not contained
1724 // in the DIE returns nothing.
1725 EXPECT_FALSE(FuncDie.find({DW_AT_low_pc, DW_AT_entry_pc}).hasValue());
1727 const dwarf::Attribute Attrs[] = {DW_AT_linkage_name,
1728 DW_AT_MIPS_linkage_name};
1730 // Make sure we can't extract the linkage name attributes when using
1731 // DWARFDie::find() since it won't check the DW_AT_specification DIE.
1732 EXPECT_FALSE(FuncDie.find(Attrs).hasValue());
1734 // Make sure we can extract the name from the specification die when using
1735 // DWARFDie::findRecursively() since it should recurse through the
1736 // DW_AT_specification DIE.
1737 auto NameOpt = FuncDie.findRecursively(Attrs);
1738 EXPECT_TRUE(NameOpt.hasValue());
1739 EXPECT_EQ(DieMangled, toString(NameOpt, ""));
1742 TEST(DWARFDebugInfo, TestImplicitConstAbbrevs) {
1743 Triple Triple = getNormalizedDefaultTargetTriple();
1744 if (!isConfigurationSupported(Triple))
1745 return;
1747 uint16_t Version = 5;
1748 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1749 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
1750 dwarfgen::Generator *DG = ExpectedDG.get().get();
1751 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1752 dwarfgen::DIE CUDie = CU.getUnitDIE();
1753 const dwarf::Attribute Attr = DW_AT_lo_user;
1754 const int64_t Val1 = 42;
1755 const int64_t Val2 = 43;
1757 auto FirstVal1DIE = CUDie.addChild(DW_TAG_class_type);
1758 FirstVal1DIE.addAttribute(Attr, DW_FORM_implicit_const, Val1);
1760 auto SecondVal1DIE = CUDie.addChild(DW_TAG_class_type);
1761 SecondVal1DIE.addAttribute(Attr, DW_FORM_implicit_const, Val1);
1763 auto Val2DIE = CUDie.addChild(DW_TAG_class_type);
1764 Val2DIE.addAttribute(Attr, DW_FORM_implicit_const, Val2);
1766 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1767 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1768 EXPECT_TRUE((bool)Obj);
1769 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
1770 DWARFCompileUnit *U =
1771 cast<DWARFCompileUnit>(DwarfContext->getUnitAtIndex(0));
1772 EXPECT_TRUE((bool)U);
1774 const auto *Abbrevs = U->getAbbreviations();
1775 EXPECT_TRUE((bool)Abbrevs);
1777 // Let's find implicit_const abbrevs and verify,
1778 // that there are exactly two of them and both of them
1779 // can be dumped correctly.
1780 typedef decltype(Abbrevs->begin()) AbbrevIt;
1781 AbbrevIt Val1Abbrev = Abbrevs->end();
1782 AbbrevIt Val2Abbrev = Abbrevs->end();
1783 for(auto it = Abbrevs->begin(); it != Abbrevs->end(); ++it) {
1784 if (it->getNumAttributes() == 0)
1785 continue; // root abbrev for DW_TAG_compile_unit
1787 auto A = it->getAttrByIndex(0);
1788 EXPECT_EQ(A, Attr);
1790 auto FormValue = it->getAttributeValue(/* offset */ 0, A, *U);
1791 EXPECT_TRUE((bool)FormValue);
1792 EXPECT_EQ(FormValue->getForm(), dwarf::DW_FORM_implicit_const);
1794 const auto V = FormValue->getAsSignedConstant();
1795 EXPECT_TRUE((bool)V);
1797 auto VerifyAbbrevDump = [&V](AbbrevIt it) {
1798 std::string S;
1799 llvm::raw_string_ostream OS(S);
1800 it->dump(OS);
1801 auto FormPos = OS.str().find("DW_FORM_implicit_const");
1802 EXPECT_NE(FormPos, std::string::npos);
1803 auto ValPos = S.find_first_of("-0123456789", FormPos);
1804 EXPECT_NE(ValPos, std::string::npos);
1805 int64_t Val = std::atoll(S.substr(ValPos).c_str());
1806 EXPECT_EQ(Val, *V);
1809 switch(*V) {
1810 case Val1:
1811 EXPECT_EQ(Val1Abbrev, Abbrevs->end());
1812 Val1Abbrev = it;
1813 VerifyAbbrevDump(it);
1814 break;
1815 case Val2:
1816 EXPECT_EQ(Val2Abbrev, Abbrevs->end());
1817 Val2Abbrev = it;
1818 VerifyAbbrevDump(it);
1819 break;
1820 default:
1821 FAIL() << "Unexpected attribute value: " << *V;
1825 // Now let's make sure that two Val1-DIEs refer to the same abbrev,
1826 // and Val2-DIE refers to another one.
1827 auto DieDG = U->getUnitDIE(false);
1828 auto it = DieDG.begin();
1829 std::multimap<int64_t, decltype(it->getAbbreviationDeclarationPtr())> DIEs;
1830 const DWARFAbbreviationDeclaration *AbbrevPtrVal1 = nullptr;
1831 const DWARFAbbreviationDeclaration *AbbrevPtrVal2 = nullptr;
1832 for (; it != DieDG.end(); ++it) {
1833 const auto *AbbrevPtr = it->getAbbreviationDeclarationPtr();
1834 EXPECT_TRUE((bool)AbbrevPtr);
1835 auto FormValue = it->find(Attr);
1836 EXPECT_TRUE((bool)FormValue);
1837 const auto V = FormValue->getAsSignedConstant();
1838 EXPECT_TRUE((bool)V);
1839 switch(*V) {
1840 case Val1:
1841 AbbrevPtrVal1 = AbbrevPtr;
1842 break;
1843 case Val2:
1844 AbbrevPtrVal2 = AbbrevPtr;
1845 break;
1846 default:
1847 FAIL() << "Unexpected attribute value: " << *V;
1849 DIEs.insert(std::make_pair(*V, AbbrevPtr));
1851 EXPECT_EQ(DIEs.count(Val1), 2u);
1852 EXPECT_EQ(DIEs.count(Val2), 1u);
1853 auto Val1Range = DIEs.equal_range(Val1);
1854 for (auto it = Val1Range.first; it != Val1Range.second; ++it)
1855 EXPECT_EQ(it->second, AbbrevPtrVal1);
1856 EXPECT_EQ(DIEs.find(Val2)->second, AbbrevPtrVal2);
1859 void VerifyWarning(DWARFContext &DwarfContext, StringRef Error) {
1860 SmallString<1024> Str;
1861 raw_svector_ostream Strm(Str);
1862 EXPECT_TRUE(DwarfContext.verify(Strm));
1863 EXPECT_TRUE(Str.str().contains(Error));
1866 void VerifyError(DWARFContext &DwarfContext, StringRef Error) {
1867 SmallString<1024> Str;
1868 raw_svector_ostream Strm(Str);
1869 EXPECT_FALSE(DwarfContext.verify(Strm));
1870 EXPECT_TRUE(Str.str().contains(Error));
1873 void VerifySuccess(DWARFContext &DwarfContext) {
1874 SmallString<1024> Str;
1875 raw_svector_ostream Strm(Str);
1876 EXPECT_TRUE(DwarfContext.verify(Strm));
1879 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidCURef) {
1880 // Create a single compile unit with a single function that has a DW_AT_type
1881 // that is CU relative. The CU offset is not valid because it is larger than
1882 // the compile unit itself.
1884 const char *yamldata = R"(
1885 debug_str:
1886 - ''
1887 - /tmp/main.c
1888 - main
1889 debug_abbrev:
1890 - Code: 0x00000001
1891 Tag: DW_TAG_compile_unit
1892 Children: DW_CHILDREN_yes
1893 Attributes:
1894 - Attribute: DW_AT_name
1895 Form: DW_FORM_strp
1896 - Code: 0x00000002
1897 Tag: DW_TAG_subprogram
1898 Children: DW_CHILDREN_no
1899 Attributes:
1900 - Attribute: DW_AT_name
1901 Form: DW_FORM_strp
1902 - Attribute: DW_AT_type
1903 Form: DW_FORM_ref4
1904 debug_info:
1905 - Length:
1906 TotalLength: 22
1907 Version: 4
1908 AbbrOffset: 0
1909 AddrSize: 8
1910 Entries:
1911 - AbbrCode: 0x00000001
1912 Values:
1913 - Value: 0x0000000000000001
1914 - AbbrCode: 0x00000002
1915 Values:
1916 - Value: 0x000000000000000D
1917 - Value: 0x0000000000001234
1918 - AbbrCode: 0x00000000
1919 Values:
1921 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
1922 ASSERT_TRUE((bool)ErrOrSections);
1923 std::unique_ptr<DWARFContext> DwarfContext =
1924 DWARFContext::create(*ErrOrSections, 8);
1925 VerifyError(*DwarfContext, "error: DW_FORM_ref4 CU offset 0x00001234 is "
1926 "invalid (must be less than CU size of "
1927 "0x0000001a):");
1930 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidRefAddr) {
1931 // Create a single compile unit with a single function that has an invalid
1932 // DW_AT_type with an invalid .debug_info offset in its DW_FORM_ref_addr.
1933 const char *yamldata = R"(
1934 debug_str:
1935 - ''
1936 - /tmp/main.c
1937 - main
1938 debug_abbrev:
1939 - Code: 0x00000001
1940 Tag: DW_TAG_compile_unit
1941 Children: DW_CHILDREN_yes
1942 Attributes:
1943 - Attribute: DW_AT_name
1944 Form: DW_FORM_strp
1945 - Code: 0x00000002
1946 Tag: DW_TAG_subprogram
1947 Children: DW_CHILDREN_no
1948 Attributes:
1949 - Attribute: DW_AT_name
1950 Form: DW_FORM_strp
1951 - Attribute: DW_AT_type
1952 Form: DW_FORM_ref_addr
1953 debug_info:
1954 - Length:
1955 TotalLength: 22
1956 Version: 4
1957 AbbrOffset: 0
1958 AddrSize: 8
1959 Entries:
1960 - AbbrCode: 0x00000001
1961 Values:
1962 - Value: 0x0000000000000001
1963 - AbbrCode: 0x00000002
1964 Values:
1965 - Value: 0x000000000000000D
1966 - Value: 0x0000000000001234
1967 - AbbrCode: 0x00000000
1968 Values:
1970 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
1971 ASSERT_TRUE((bool)ErrOrSections);
1972 std::unique_ptr<DWARFContext> DwarfContext =
1973 DWARFContext::create(*ErrOrSections, 8);
1974 VerifyError(*DwarfContext,
1975 "error: DW_FORM_ref_addr offset beyond .debug_info bounds:");
1978 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidRanges) {
1979 // Create a single compile unit with a DW_AT_ranges whose section offset
1980 // isn't valid.
1981 const char *yamldata = R"(
1982 debug_str:
1983 - ''
1984 - /tmp/main.c
1985 debug_abbrev:
1986 - Code: 0x00000001
1987 Tag: DW_TAG_compile_unit
1988 Children: DW_CHILDREN_no
1989 Attributes:
1990 - Attribute: DW_AT_name
1991 Form: DW_FORM_strp
1992 - Attribute: DW_AT_ranges
1993 Form: DW_FORM_sec_offset
1994 debug_info:
1995 - Length:
1996 TotalLength: 16
1997 Version: 4
1998 AbbrOffset: 0
1999 AddrSize: 8
2000 Entries:
2001 - AbbrCode: 0x00000001
2002 Values:
2003 - Value: 0x0000000000000001
2004 - Value: 0x0000000000001000
2007 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
2008 ASSERT_TRUE((bool)ErrOrSections);
2009 std::unique_ptr<DWARFContext> DwarfContext =
2010 DWARFContext::create(*ErrOrSections, 8);
2011 VerifyError(*DwarfContext,
2012 "error: DW_AT_ranges offset is beyond .debug_ranges bounds:");
2015 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidStmtList) {
2016 // Create a single compile unit with a DW_AT_stmt_list whose section offset
2017 // isn't valid.
2018 const char *yamldata = R"(
2019 debug_str:
2020 - ''
2021 - /tmp/main.c
2022 debug_abbrev:
2023 - Code: 0x00000001
2024 Tag: DW_TAG_compile_unit
2025 Children: DW_CHILDREN_no
2026 Attributes:
2027 - Attribute: DW_AT_name
2028 Form: DW_FORM_strp
2029 - Attribute: DW_AT_stmt_list
2030 Form: DW_FORM_sec_offset
2031 debug_info:
2032 - Length:
2033 TotalLength: 16
2034 Version: 4
2035 AbbrOffset: 0
2036 AddrSize: 8
2037 Entries:
2038 - AbbrCode: 0x00000001
2039 Values:
2040 - Value: 0x0000000000000001
2041 - Value: 0x0000000000001000
2044 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
2045 ASSERT_TRUE((bool)ErrOrSections);
2046 std::unique_ptr<DWARFContext> DwarfContext =
2047 DWARFContext::create(*ErrOrSections, 8);
2048 VerifyError(
2049 *DwarfContext,
2050 "error: DW_AT_stmt_list offset is beyond .debug_line bounds: 0x00001000");
2053 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidStrp) {
2054 // Create a single compile unit with a single function that has an invalid
2055 // DW_FORM_strp for the DW_AT_name.
2056 const char *yamldata = R"(
2057 debug_str:
2058 - ''
2059 debug_abbrev:
2060 - Code: 0x00000001
2061 Tag: DW_TAG_compile_unit
2062 Children: DW_CHILDREN_no
2063 Attributes:
2064 - Attribute: DW_AT_name
2065 Form: DW_FORM_strp
2066 debug_info:
2067 - Length:
2068 TotalLength: 12
2069 Version: 4
2070 AbbrOffset: 0
2071 AddrSize: 8
2072 Entries:
2073 - AbbrCode: 0x00000001
2074 Values:
2075 - Value: 0x0000000000001234
2077 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
2078 ASSERT_TRUE((bool)ErrOrSections);
2079 std::unique_ptr<DWARFContext> DwarfContext =
2080 DWARFContext::create(*ErrOrSections, 8);
2081 VerifyError(*DwarfContext,
2082 "error: DW_FORM_strp offset beyond .debug_str bounds:");
2085 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidRefAddrBetween) {
2086 // Create a single compile unit with a single function that has a DW_AT_type
2087 // with a valid .debug_info offset, but the offset is between two DIEs.
2088 const char *yamldata = R"(
2089 debug_str:
2090 - ''
2091 - /tmp/main.c
2092 - main
2093 debug_abbrev:
2094 - Code: 0x00000001
2095 Tag: DW_TAG_compile_unit
2096 Children: DW_CHILDREN_yes
2097 Attributes:
2098 - Attribute: DW_AT_name
2099 Form: DW_FORM_strp
2100 - Code: 0x00000002
2101 Tag: DW_TAG_subprogram
2102 Children: DW_CHILDREN_no
2103 Attributes:
2104 - Attribute: DW_AT_name
2105 Form: DW_FORM_strp
2106 - Attribute: DW_AT_type
2107 Form: DW_FORM_ref_addr
2108 debug_info:
2109 - Length:
2110 TotalLength: 22
2111 Version: 4
2112 AbbrOffset: 0
2113 AddrSize: 8
2114 Entries:
2115 - AbbrCode: 0x00000001
2116 Values:
2117 - Value: 0x0000000000000001
2118 - AbbrCode: 0x00000002
2119 Values:
2120 - Value: 0x000000000000000D
2121 - Value: 0x0000000000000011
2122 - AbbrCode: 0x00000000
2123 Values:
2125 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
2126 ASSERT_TRUE((bool)ErrOrSections);
2127 std::unique_ptr<DWARFContext> DwarfContext =
2128 DWARFContext::create(*ErrOrSections, 8);
2129 VerifyError(
2130 *DwarfContext,
2131 "error: invalid DIE reference 0x00000011. Offset is in between DIEs:");
2134 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidLineSequence) {
2135 // Create a single compile unit whose line table has a sequence in it where
2136 // the address decreases.
2137 StringRef yamldata = R"(
2138 debug_str:
2139 - ''
2140 - /tmp/main.c
2141 debug_abbrev:
2142 - Code: 0x00000001
2143 Tag: DW_TAG_compile_unit
2144 Children: DW_CHILDREN_no
2145 Attributes:
2146 - Attribute: DW_AT_name
2147 Form: DW_FORM_strp
2148 - Attribute: DW_AT_stmt_list
2149 Form: DW_FORM_sec_offset
2150 debug_info:
2151 - Length:
2152 TotalLength: 16
2153 Version: 4
2154 AbbrOffset: 0
2155 AddrSize: 8
2156 Entries:
2157 - AbbrCode: 0x00000001
2158 Values:
2159 - Value: 0x0000000000000001
2160 - Value: 0x0000000000000000
2161 debug_line:
2162 - Length:
2163 TotalLength: 68
2164 Version: 2
2165 PrologueLength: 34
2166 MinInstLength: 1
2167 DefaultIsStmt: 1
2168 LineBase: 251
2169 LineRange: 14
2170 OpcodeBase: 13
2171 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
2172 IncludeDirs:
2173 - /tmp
2174 Files:
2175 - Name: main.c
2176 DirIdx: 1
2177 ModTime: 0
2178 Length: 0
2179 Opcodes:
2180 - Opcode: DW_LNS_extended_op
2181 ExtLen: 9
2182 SubOpcode: DW_LNE_set_address
2183 Data: 4112
2184 - Opcode: DW_LNS_advance_line
2185 SData: 9
2186 Data: 4112
2187 - Opcode: DW_LNS_copy
2188 Data: 4112
2189 - Opcode: DW_LNS_advance_pc
2190 Data: 18446744073709551600
2191 - Opcode: DW_LNS_extended_op
2192 ExtLen: 1
2193 SubOpcode: DW_LNE_end_sequence
2194 Data: 18446744073709551600
2196 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2197 ASSERT_TRUE((bool)ErrOrSections);
2198 std::unique_ptr<DWARFContext> DwarfContext =
2199 DWARFContext::create(*ErrOrSections, 8);
2200 VerifyError(*DwarfContext, "error: .debug_line[0x00000000] row[1] decreases "
2201 "in address from previous row:");
2204 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidLineFileIndex) {
2205 // Create a single compile unit whose line table has a line table row with
2206 // an invalid file index.
2207 StringRef yamldata = R"(
2208 debug_str:
2209 - ''
2210 - /tmp/main.c
2211 debug_abbrev:
2212 - Code: 0x00000001
2213 Tag: DW_TAG_compile_unit
2214 Children: DW_CHILDREN_no
2215 Attributes:
2216 - Attribute: DW_AT_name
2217 Form: DW_FORM_strp
2218 - Attribute: DW_AT_stmt_list
2219 Form: DW_FORM_sec_offset
2220 debug_info:
2221 - Length:
2222 TotalLength: 16
2223 Version: 4
2224 AbbrOffset: 0
2225 AddrSize: 8
2226 Entries:
2227 - AbbrCode: 0x00000001
2228 Values:
2229 - Value: 0x0000000000000001
2230 - Value: 0x0000000000000000
2231 debug_line:
2232 - Length:
2233 TotalLength: 61
2234 Version: 2
2235 PrologueLength: 34
2236 MinInstLength: 1
2237 DefaultIsStmt: 1
2238 LineBase: 251
2239 LineRange: 14
2240 OpcodeBase: 13
2241 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
2242 IncludeDirs:
2243 - /tmp
2244 Files:
2245 - Name: main.c
2246 DirIdx: 1
2247 ModTime: 0
2248 Length: 0
2249 Opcodes:
2250 - Opcode: DW_LNS_extended_op
2251 ExtLen: 9
2252 SubOpcode: DW_LNE_set_address
2253 Data: 4096
2254 - Opcode: DW_LNS_advance_line
2255 SData: 9
2256 Data: 4096
2257 - Opcode: DW_LNS_copy
2258 Data: 4096
2259 - Opcode: DW_LNS_advance_pc
2260 Data: 16
2261 - Opcode: DW_LNS_set_file
2262 Data: 5
2263 - Opcode: DW_LNS_extended_op
2264 ExtLen: 1
2265 SubOpcode: DW_LNE_end_sequence
2266 Data: 5
2268 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2269 ASSERT_TRUE((bool)ErrOrSections);
2270 std::unique_ptr<DWARFContext> DwarfContext =
2271 DWARFContext::create(*ErrOrSections, 8);
2272 VerifyError(*DwarfContext, "error: .debug_line[0x00000000][1] has invalid "
2273 "file index 5 (valid values are [1,1]):");
2276 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidLineTablePorlogueDirIndex) {
2277 // Create a single compile unit whose line table has a prologue with an
2278 // invalid dir index.
2279 StringRef yamldata = R"(
2280 debug_str:
2281 - ''
2282 - /tmp/main.c
2283 debug_abbrev:
2284 - Code: 0x00000001
2285 Tag: DW_TAG_compile_unit
2286 Children: DW_CHILDREN_no
2287 Attributes:
2288 - Attribute: DW_AT_name
2289 Form: DW_FORM_strp
2290 - Attribute: DW_AT_stmt_list
2291 Form: DW_FORM_sec_offset
2292 debug_info:
2293 - Length:
2294 TotalLength: 16
2295 Version: 4
2296 AbbrOffset: 0
2297 AddrSize: 8
2298 Entries:
2299 - AbbrCode: 0x00000001
2300 Values:
2301 - Value: 0x0000000000000001
2302 - Value: 0x0000000000000000
2303 debug_line:
2304 - Length:
2305 TotalLength: 61
2306 Version: 2
2307 PrologueLength: 34
2308 MinInstLength: 1
2309 DefaultIsStmt: 1
2310 LineBase: 251
2311 LineRange: 14
2312 OpcodeBase: 13
2313 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
2314 IncludeDirs:
2315 - /tmp
2316 Files:
2317 - Name: main.c
2318 DirIdx: 2
2319 ModTime: 0
2320 Length: 0
2321 Opcodes:
2322 - Opcode: DW_LNS_extended_op
2323 ExtLen: 9
2324 SubOpcode: DW_LNE_set_address
2325 Data: 4096
2326 - Opcode: DW_LNS_advance_line
2327 SData: 9
2328 Data: 4096
2329 - Opcode: DW_LNS_copy
2330 Data: 4096
2331 - Opcode: DW_LNS_advance_pc
2332 Data: 16
2333 - Opcode: DW_LNS_set_file
2334 Data: 1
2335 - Opcode: DW_LNS_extended_op
2336 ExtLen: 1
2337 SubOpcode: DW_LNE_end_sequence
2338 Data: 1
2340 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2341 ASSERT_TRUE((bool)ErrOrSections);
2342 std::unique_ptr<DWARFContext> DwarfContext =
2343 DWARFContext::create(*ErrOrSections, 8);
2344 VerifyError(*DwarfContext,
2345 "error: .debug_line[0x00000000].prologue."
2346 "file_names[1].dir_idx contains an invalid index: 2");
2349 TEST(DWARFDebugInfo, TestDwarfVerifyDuplicateFileWarning) {
2350 // Create a single compile unit whose line table has a prologue with an
2351 // invalid dir index.
2352 StringRef yamldata = R"(
2353 debug_str:
2354 - ''
2355 - /tmp/main.c
2356 debug_abbrev:
2357 - Code: 0x00000001
2358 Tag: DW_TAG_compile_unit
2359 Children: DW_CHILDREN_no
2360 Attributes:
2361 - Attribute: DW_AT_name
2362 Form: DW_FORM_strp
2363 - Attribute: DW_AT_stmt_list
2364 Form: DW_FORM_sec_offset
2365 debug_info:
2366 - Length:
2367 TotalLength: 16
2368 Version: 4
2369 AbbrOffset: 0
2370 AddrSize: 8
2371 Entries:
2372 - AbbrCode: 0x00000001
2373 Values:
2374 - Value: 0x0000000000000001
2375 - Value: 0x0000000000000000
2376 debug_line:
2377 - Length:
2378 TotalLength: 71
2379 Version: 2
2380 PrologueLength: 44
2381 MinInstLength: 1
2382 DefaultIsStmt: 1
2383 LineBase: 251
2384 LineRange: 14
2385 OpcodeBase: 13
2386 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
2387 IncludeDirs:
2388 - /tmp
2389 Files:
2390 - Name: main.c
2391 DirIdx: 1
2392 ModTime: 0
2393 Length: 0
2394 - Name: main.c
2395 DirIdx: 1
2396 ModTime: 0
2397 Length: 0
2398 Opcodes:
2399 - Opcode: DW_LNS_extended_op
2400 ExtLen: 9
2401 SubOpcode: DW_LNE_set_address
2402 Data: 4096
2403 - Opcode: DW_LNS_advance_line
2404 SData: 9
2405 Data: 4096
2406 - Opcode: DW_LNS_copy
2407 Data: 4096
2408 - Opcode: DW_LNS_advance_pc
2409 Data: 16
2410 - Opcode: DW_LNS_set_file
2411 Data: 1
2412 - Opcode: DW_LNS_extended_op
2413 ExtLen: 1
2414 SubOpcode: DW_LNE_end_sequence
2415 Data: 2
2417 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2418 ASSERT_TRUE((bool)ErrOrSections);
2419 std::unique_ptr<DWARFContext> DwarfContext =
2420 DWARFContext::create(*ErrOrSections, 8);
2421 VerifyWarning(*DwarfContext,
2422 "warning: .debug_line[0x00000000].prologue.file_names[2] is "
2423 "a duplicate of file_names[1]");
2426 TEST(DWARFDebugInfo, TestDwarfVerifyCUDontShareLineTable) {
2427 // Create a two compile units where both compile units share the same
2428 // DW_AT_stmt_list value and verify we report the error correctly.
2429 StringRef yamldata = R"(
2430 debug_str:
2431 - ''
2432 - /tmp/main.c
2433 - /tmp/foo.c
2434 debug_abbrev:
2435 - Code: 0x00000001
2436 Tag: DW_TAG_compile_unit
2437 Children: DW_CHILDREN_no
2438 Attributes:
2439 - Attribute: DW_AT_name
2440 Form: DW_FORM_strp
2441 - Attribute: DW_AT_stmt_list
2442 Form: DW_FORM_sec_offset
2443 debug_info:
2444 - Length:
2445 TotalLength: 16
2446 Version: 4
2447 AbbrOffset: 0
2448 AddrSize: 8
2449 Entries:
2450 - AbbrCode: 0x00000001
2451 Values:
2452 - Value: 0x0000000000000001
2453 - Value: 0x0000000000000000
2454 - Length:
2455 TotalLength: 16
2456 Version: 4
2457 AbbrOffset: 0
2458 AddrSize: 8
2459 Entries:
2460 - AbbrCode: 0x00000001
2461 Values:
2462 - Value: 0x000000000000000D
2463 - Value: 0x0000000000000000
2464 debug_line:
2465 - Length:
2466 TotalLength: 60
2467 Version: 2
2468 PrologueLength: 34
2469 MinInstLength: 1
2470 DefaultIsStmt: 1
2471 LineBase: 251
2472 LineRange: 14
2473 OpcodeBase: 13
2474 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
2475 IncludeDirs:
2476 - /tmp
2477 Files:
2478 - Name: main.c
2479 DirIdx: 1
2480 ModTime: 0
2481 Length: 0
2482 Opcodes:
2483 - Opcode: DW_LNS_extended_op
2484 ExtLen: 9
2485 SubOpcode: DW_LNE_set_address
2486 Data: 4096
2487 - Opcode: DW_LNS_advance_line
2488 SData: 9
2489 Data: 4096
2490 - Opcode: DW_LNS_copy
2491 Data: 4096
2492 - Opcode: DW_LNS_advance_pc
2493 Data: 256
2494 - Opcode: DW_LNS_extended_op
2495 ExtLen: 1
2496 SubOpcode: DW_LNE_end_sequence
2497 Data: 256
2499 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2500 ASSERT_TRUE((bool)ErrOrSections);
2501 std::unique_ptr<DWARFContext> DwarfContext =
2502 DWARFContext::create(*ErrOrSections, 8);
2503 VerifyError(*DwarfContext,
2504 "error: two compile unit DIEs, 0x0000000b and "
2505 "0x0000001f, have the same DW_AT_stmt_list section "
2506 "offset:");
2509 TEST(DWARFDebugInfo, TestErrorReportingPolicy) {
2510 Triple Triple("x86_64-pc-linux");
2511 if (!isConfigurationSupported(Triple))
2512 return;
2514 auto ExpectedDG = dwarfgen::Generator::create(Triple, 4 /*DwarfVersion*/);
2515 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
2516 dwarfgen::Generator *DG = ExpectedDG.get().get();
2517 AsmPrinter *AP = DG->getAsmPrinter();
2518 MCContext *MC = DG->getMCContext();
2520 // Emit two compressed sections with broken headers.
2521 AP->OutStreamer->SwitchSection(
2522 MC->getELFSection(".zdebug_foo", 0 /*Type*/, 0 /*Flags*/));
2523 AP->OutStreamer->EmitBytes("0");
2524 AP->OutStreamer->SwitchSection(
2525 MC->getELFSection(".zdebug_bar", 0 /*Type*/, 0 /*Flags*/));
2526 AP->OutStreamer->EmitBytes("0");
2528 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
2529 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
2530 EXPECT_TRUE((bool)Obj);
2532 // Case 1: error handler handles all errors. That allows
2533 // DWARFContext to parse whole file and find both two errors we know about.
2534 int Errors = 0;
2535 std::unique_ptr<DWARFContext> Ctx1 =
2536 DWARFContext::create(**Obj, nullptr, [&](Error E) {
2537 ++Errors;
2538 consumeError(std::move(E));
2539 return ErrorPolicy::Continue;
2541 EXPECT_TRUE(Errors == 2);
2543 // Case 2: error handler stops parsing of object after first error.
2544 Errors = 0;
2545 std::unique_ptr<DWARFContext> Ctx2 =
2546 DWARFContext::create(**Obj, nullptr, [&](Error E) {
2547 ++Errors;
2548 consumeError(std::move(E));
2549 return ErrorPolicy::Halt;
2551 EXPECT_TRUE(Errors == 1);
2554 TEST(DWARFDebugInfo, TestDwarfVerifyCURangesIncomplete) {
2555 // Create a single compile unit with a single function. The compile
2556 // unit has a DW_AT_ranges attribute that doesn't fully contain the
2557 // address range of the function. The verification should fail due to
2558 // the CU ranges not containing all of the address ranges of all of the
2559 // functions.
2560 StringRef yamldata = R"(
2561 debug_str:
2562 - ''
2563 - /tmp/main.c
2564 debug_abbrev:
2565 - Code: 0x00000001
2566 Tag: DW_TAG_compile_unit
2567 Children: DW_CHILDREN_yes
2568 Attributes:
2569 - Attribute: DW_AT_low_pc
2570 Form: DW_FORM_addr
2571 - Attribute: DW_AT_high_pc
2572 Form: DW_FORM_addr
2573 - Attribute: DW_AT_name
2574 Form: DW_FORM_strp
2575 - Code: 0x00000002
2576 Tag: DW_TAG_subprogram
2577 Children: DW_CHILDREN_no
2578 Attributes:
2579 - Attribute: DW_AT_low_pc
2580 Form: DW_FORM_addr
2581 - Attribute: DW_AT_high_pc
2582 Form: DW_FORM_addr
2583 debug_info:
2584 - Length:
2585 TotalLength: 46
2586 Version: 4
2587 AbbrOffset: 0
2588 AddrSize: 8
2589 Entries:
2590 - AbbrCode: 0x00000001
2591 Values:
2592 - Value: 0x0000000000001000
2593 - Value: 0x0000000000001500
2594 - Value: 0x0000000000000001
2595 - AbbrCode: 0x00000002
2596 Values:
2597 - Value: 0x0000000000001000
2598 - Value: 0x0000000000002000
2599 - AbbrCode: 0x00000000
2600 Values:
2602 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2603 ASSERT_TRUE((bool)ErrOrSections);
2604 std::unique_ptr<DWARFContext> DwarfContext =
2605 DWARFContext::create(*ErrOrSections, 8);
2606 VerifyError(*DwarfContext, "error: DIE address ranges are not "
2607 "contained in its parent's ranges:");
2610 TEST(DWARFDebugInfo, TestDwarfVerifyLexicalBlockRanges) {
2611 // Create a single compile unit with a single function that has a lexical
2612 // block whose address range is not contained in the function address range.
2613 StringRef yamldata = R"(
2614 debug_str:
2615 - ''
2616 - /tmp/main.c
2617 - main
2618 debug_abbrev:
2619 - Code: 0x00000001
2620 Tag: DW_TAG_compile_unit
2621 Children: DW_CHILDREN_yes
2622 Attributes:
2623 - Attribute: DW_AT_name
2624 Form: DW_FORM_strp
2625 - Code: 0x00000002
2626 Tag: DW_TAG_subprogram
2627 Children: DW_CHILDREN_yes
2628 Attributes:
2629 - Attribute: DW_AT_name
2630 Form: DW_FORM_strp
2631 - Attribute: DW_AT_low_pc
2632 Form: DW_FORM_addr
2633 - Attribute: DW_AT_high_pc
2634 Form: DW_FORM_addr
2635 - Code: 0x00000003
2636 Tag: DW_TAG_lexical_block
2637 Children: DW_CHILDREN_no
2638 Attributes:
2639 - Attribute: DW_AT_low_pc
2640 Form: DW_FORM_addr
2641 - Attribute: DW_AT_high_pc
2642 Form: DW_FORM_addr
2643 debug_info:
2644 - Length:
2645 TotalLength: 52
2646 Version: 4
2647 AbbrOffset: 0
2648 AddrSize: 8
2649 Entries:
2650 - AbbrCode: 0x00000001
2651 Values:
2652 - Value: 0x0000000000000001
2653 - AbbrCode: 0x00000002
2654 Values:
2655 - Value: 0x000000000000000D
2656 - Value: 0x0000000000001000
2657 - Value: 0x0000000000002000
2658 - AbbrCode: 0x00000003
2659 Values:
2660 - Value: 0x0000000000001000
2661 - Value: 0x0000000000002001
2662 - AbbrCode: 0x00000000
2663 Values:
2664 - AbbrCode: 0x00000000
2665 Values:
2667 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2668 ASSERT_TRUE((bool)ErrOrSections);
2669 std::unique_ptr<DWARFContext> DwarfContext =
2670 DWARFContext::create(*ErrOrSections, 8);
2671 VerifyError(*DwarfContext, "error: DIE address ranges are not "
2672 "contained in its parent's ranges:");
2675 TEST(DWARFDebugInfo, TestDwarfVerifyOverlappingFunctionRanges) {
2676 // Create a single compile unit with a two functions that have overlapping
2677 // address ranges.
2678 StringRef yamldata = R"(
2679 debug_str:
2680 - ''
2681 - /tmp/main.c
2682 - main
2683 - foo
2684 debug_abbrev:
2685 - Code: 0x00000001
2686 Tag: DW_TAG_compile_unit
2687 Children: DW_CHILDREN_yes
2688 Attributes:
2689 - Attribute: DW_AT_name
2690 Form: DW_FORM_strp
2691 - Code: 0x00000002
2692 Tag: DW_TAG_subprogram
2693 Children: DW_CHILDREN_no
2694 Attributes:
2695 - Attribute: DW_AT_name
2696 Form: DW_FORM_strp
2697 - Attribute: DW_AT_low_pc
2698 Form: DW_FORM_addr
2699 - Attribute: DW_AT_high_pc
2700 Form: DW_FORM_addr
2701 debug_info:
2702 - Length:
2703 TotalLength: 55
2704 Version: 4
2705 AbbrOffset: 0
2706 AddrSize: 8
2707 Entries:
2708 - AbbrCode: 0x00000001
2709 Values:
2710 - Value: 0x0000000000000001
2711 - AbbrCode: 0x00000002
2712 Values:
2713 - Value: 0x000000000000000D
2714 - Value: 0x0000000000001000
2715 - Value: 0x0000000000002000
2716 - AbbrCode: 0x00000002
2717 Values:
2718 - Value: 0x0000000000000012
2719 - Value: 0x0000000000001FFF
2720 - Value: 0x0000000000002000
2721 - AbbrCode: 0x00000000
2722 Values:
2724 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2725 ASSERT_TRUE((bool)ErrOrSections);
2726 std::unique_ptr<DWARFContext> DwarfContext =
2727 DWARFContext::create(*ErrOrSections, 8);
2728 VerifyError(*DwarfContext, "error: DIEs have overlapping address ranges:");
2731 TEST(DWARFDebugInfo, TestDwarfVerifyOverlappingLexicalBlockRanges) {
2732 // Create a single compile unit with a one function that has two lexical
2733 // blocks with overlapping address ranges.
2734 StringRef yamldata = R"(
2735 debug_str:
2736 - ''
2737 - /tmp/main.c
2738 - main
2739 debug_abbrev:
2740 - Code: 0x00000001
2741 Tag: DW_TAG_compile_unit
2742 Children: DW_CHILDREN_yes
2743 Attributes:
2744 - Attribute: DW_AT_low_pc
2745 Form: DW_FORM_addr
2746 - Attribute: DW_AT_high_pc
2747 Form: DW_FORM_addr
2748 - Attribute: DW_AT_name
2749 Form: DW_FORM_strp
2750 - Code: 0x00000002
2751 Tag: DW_TAG_subprogram
2752 Children: DW_CHILDREN_yes
2753 Attributes:
2754 - Attribute: DW_AT_name
2755 Form: DW_FORM_strp
2756 - Attribute: DW_AT_low_pc
2757 Form: DW_FORM_addr
2758 - Attribute: DW_AT_high_pc
2759 Form: DW_FORM_addr
2760 - Code: 0x00000003
2761 Tag: DW_TAG_lexical_block
2762 Children: DW_CHILDREN_no
2763 Attributes:
2764 - Attribute: DW_AT_low_pc
2765 Form: DW_FORM_addr
2766 - Attribute: DW_AT_high_pc
2767 Form: DW_FORM_addr
2768 debug_info:
2769 - Length:
2770 TotalLength: 85
2771 Version: 4
2772 AbbrOffset: 0
2773 AddrSize: 8
2774 Entries:
2775 - AbbrCode: 0x00000001
2776 Values:
2777 - Value: 0x0000000000001000
2778 - Value: 0x0000000000002000
2779 - Value: 0x0000000000000001
2780 - AbbrCode: 0x00000002
2781 Values:
2782 - Value: 0x000000000000000D
2783 - Value: 0x0000000000001000
2784 - Value: 0x0000000000002000
2785 - AbbrCode: 0x00000003
2786 Values:
2787 - Value: 0x0000000000001100
2788 - Value: 0x0000000000001300
2789 - AbbrCode: 0x00000003
2790 Values:
2791 - Value: 0x00000000000012FF
2792 - Value: 0x0000000000001300
2793 - AbbrCode: 0x00000000
2794 Values:
2795 - AbbrCode: 0x00000000
2796 Values:
2798 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2799 ASSERT_TRUE((bool)ErrOrSections);
2800 std::unique_ptr<DWARFContext> DwarfContext =
2801 DWARFContext::create(*ErrOrSections, 8);
2802 VerifyError(*DwarfContext, "error: DIEs have overlapping address ranges:");
2805 TEST(DWARFDebugInfo, TestDwarfVerifyInvalidDIERange) {
2806 // Create a single compile unit with a single function that has an invalid
2807 // address range where the high PC is smaller than the low PC.
2808 StringRef yamldata = R"(
2809 debug_str:
2810 - ''
2811 - /tmp/main.c
2812 - main
2813 debug_abbrev:
2814 - Code: 0x00000001
2815 Tag: DW_TAG_compile_unit
2816 Children: DW_CHILDREN_yes
2817 Attributes:
2818 - Attribute: DW_AT_name
2819 Form: DW_FORM_strp
2820 - Code: 0x00000002
2821 Tag: DW_TAG_subprogram
2822 Children: DW_CHILDREN_no
2823 Attributes:
2824 - Attribute: DW_AT_name
2825 Form: DW_FORM_strp
2826 - Attribute: DW_AT_low_pc
2827 Form: DW_FORM_addr
2828 - Attribute: DW_AT_high_pc
2829 Form: DW_FORM_addr
2830 debug_info:
2831 - Length:
2832 TotalLength: 34
2833 Version: 4
2834 AbbrOffset: 0
2835 AddrSize: 8
2836 Entries:
2837 - AbbrCode: 0x00000001
2838 Values:
2839 - Value: 0x0000000000000001
2840 - AbbrCode: 0x00000002
2841 Values:
2842 - Value: 0x000000000000000D
2843 - Value: 0x0000000000001000
2844 - Value: 0x0000000000000900
2845 - AbbrCode: 0x00000000
2846 Values:
2848 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2849 ASSERT_TRUE((bool)ErrOrSections);
2850 std::unique_ptr<DWARFContext> DwarfContext =
2851 DWARFContext::create(*ErrOrSections, 8);
2852 VerifyError(*DwarfContext, "error: Invalid address range");
2855 TEST(DWARFDebugInfo, TestDwarfVerifyElidedDoesntFail) {
2856 // Create a single compile unit with two functions: one that has a valid range
2857 // and one whose low and high PC are the same. When the low and high PC are
2858 // the same, this indicates the function was dead code stripped. We want to
2859 // ensure that verification succeeds.
2860 StringRef yamldata = R"(
2861 debug_str:
2862 - ''
2863 - /tmp/main.c
2864 - main
2865 - elided
2866 debug_abbrev:
2867 - Code: 0x00000001
2868 Tag: DW_TAG_compile_unit
2869 Children: DW_CHILDREN_yes
2870 Attributes:
2871 - Attribute: DW_AT_low_pc
2872 Form: DW_FORM_addr
2873 - Attribute: DW_AT_high_pc
2874 Form: DW_FORM_addr
2875 - Attribute: DW_AT_name
2876 Form: DW_FORM_strp
2877 - Code: 0x00000002
2878 Tag: DW_TAG_subprogram
2879 Children: DW_CHILDREN_no
2880 Attributes:
2881 - Attribute: DW_AT_name
2882 Form: DW_FORM_strp
2883 - Attribute: DW_AT_low_pc
2884 Form: DW_FORM_addr
2885 - Attribute: DW_AT_high_pc
2886 Form: DW_FORM_addr
2887 debug_info:
2888 - Length:
2889 TotalLength: 71
2890 Version: 4
2891 AbbrOffset: 0
2892 AddrSize: 8
2893 Entries:
2894 - AbbrCode: 0x00000001
2895 Values:
2896 - Value: 0x0000000000001000
2897 - Value: 0x0000000000002000
2898 - Value: 0x0000000000000001
2899 - AbbrCode: 0x00000002
2900 Values:
2901 - Value: 0x000000000000000D
2902 - Value: 0x0000000000001000
2903 - Value: 0x0000000000002000
2904 - AbbrCode: 0x00000002
2905 Values:
2906 - Value: 0x0000000000000012
2907 - Value: 0x0000000000002000
2908 - Value: 0x0000000000002000
2909 - AbbrCode: 0x00000000
2910 Values:
2912 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2913 ASSERT_TRUE((bool)ErrOrSections);
2914 std::unique_ptr<DWARFContext> DwarfContext =
2915 DWARFContext::create(*ErrOrSections, 8);
2916 VerifySuccess(*DwarfContext);
2919 TEST(DWARFDebugInfo, TestDwarfVerifyNestedFunctions) {
2920 // Create a single compile unit with a nested function which is not contained
2921 // in its parent. Although LLVM doesn't generate this, it is valid accoridng
2922 // to the DWARF standard.
2923 StringRef yamldata = R"(
2924 debug_str:
2925 - ''
2926 - /tmp/main.c
2927 - main
2928 - nested
2929 debug_abbrev:
2930 - Code: 0x00000001
2931 Tag: DW_TAG_compile_unit
2932 Children: DW_CHILDREN_yes
2933 Attributes:
2934 - Attribute: DW_AT_low_pc
2935 Form: DW_FORM_addr
2936 - Attribute: DW_AT_high_pc
2937 Form: DW_FORM_addr
2938 - Attribute: DW_AT_name
2939 Form: DW_FORM_strp
2940 - Code: 0x00000002
2941 Tag: DW_TAG_subprogram
2942 Children: DW_CHILDREN_yes
2943 Attributes:
2944 - Attribute: DW_AT_name
2945 Form: DW_FORM_strp
2946 - Attribute: DW_AT_low_pc
2947 Form: DW_FORM_addr
2948 - Attribute: DW_AT_high_pc
2949 Form: DW_FORM_addr
2950 debug_info:
2951 - Length:
2952 TotalLength: 73
2953 Version: 4
2954 AbbrOffset: 0
2955 AddrSize: 8
2956 Entries:
2957 - AbbrCode: 0x00000001
2958 Values:
2959 - Value: 0x0000000000001000
2960 - Value: 0x0000000000002000
2961 - Value: 0x0000000000000001
2962 - AbbrCode: 0x00000002
2963 Values:
2964 - Value: 0x000000000000000D
2965 - Value: 0x0000000000001000
2966 - Value: 0x0000000000001500
2967 - AbbrCode: 0x00000002
2968 Values:
2969 - Value: 0x0000000000000012
2970 - Value: 0x0000000000001500
2971 - Value: 0x0000000000002000
2972 - AbbrCode: 0x00000000
2973 Values:
2974 - AbbrCode: 0x00000000
2975 Values:
2976 - AbbrCode: 0x00000000
2977 Values:
2979 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2980 ASSERT_TRUE((bool)ErrOrSections);
2981 std::unique_ptr<DWARFContext> DwarfContext =
2982 DWARFContext::create(*ErrOrSections, 8);
2983 VerifySuccess(*DwarfContext);
2986 TEST(DWARFDebugInfo, TestDWARFDieRangeInfoContains) {
2987 DWARFVerifier::DieRangeInfo Empty;
2988 ASSERT_TRUE(Empty.contains(Empty));
2990 DWARFVerifier::DieRangeInfo Ranges(
2991 {{0x10, 0x20}, {0x30, 0x40}, {0x40, 0x50}});
2993 ASSERT_TRUE(Ranges.contains(Empty));
2994 ASSERT_FALSE(Ranges.contains({{{0x0f, 0x10}}}));
2995 ASSERT_FALSE(Ranges.contains({{{0x0f, 0x20}}}));
2996 ASSERT_FALSE(Ranges.contains({{{0x0f, 0x21}}}));
2998 // Test ranges that start at R's start address
2999 ASSERT_TRUE(Ranges.contains({{{0x10, 0x10}}}));
3000 ASSERT_TRUE(Ranges.contains({{{0x10, 0x11}}}));
3001 ASSERT_TRUE(Ranges.contains({{{0x10, 0x20}}}));
3002 ASSERT_FALSE(Ranges.contains({{{0x10, 0x21}}}));
3004 ASSERT_TRUE(Ranges.contains({{{0x11, 0x12}}}));
3006 // Test ranges that start at last bytes of Range
3007 ASSERT_TRUE(Ranges.contains({{{0x1f, 0x20}}}));
3008 ASSERT_FALSE(Ranges.contains({{{0x1f, 0x21}}}));
3010 // Test ranges that start after Range
3011 ASSERT_TRUE(Ranges.contains({{{0x20, 0x20}}}));
3012 ASSERT_FALSE(Ranges.contains({{{0x20, 0x21}}}));
3014 ASSERT_TRUE(Ranges.contains({{{0x31, 0x32}}}));
3015 ASSERT_TRUE(Ranges.contains({{{0x3f, 0x40}}}));
3016 ASSERT_TRUE(Ranges.contains({{{0x10, 0x20}, {0x30, 0x40}}}));
3017 ASSERT_TRUE(Ranges.contains({{{0x11, 0x12}, {0x31, 0x32}}}));
3018 ASSERT_TRUE(Ranges.contains(
3019 {{{0x11, 0x12}, {0x12, 0x13}, {0x31, 0x32}, {0x32, 0x33}}}));
3020 ASSERT_FALSE(Ranges.contains({{{0x11, 0x12},
3021 {0x12, 0x13},
3022 {0x20, 0x21},
3023 {0x31, 0x32},
3024 {0x32, 0x33}}}));
3025 ASSERT_FALSE(Ranges.contains(
3026 {{{0x11, 0x12}, {0x12, 0x13}, {0x31, 0x32}, {0x32, 0x51}}}));
3027 ASSERT_TRUE(Ranges.contains({{{0x11, 0x12}, {0x30, 0x50}}}));
3028 ASSERT_FALSE(Ranges.contains({{{0x30, 0x51}}}));
3029 ASSERT_FALSE(Ranges.contains({{{0x50, 0x51}}}));
3032 namespace {
3034 void AssertRangesIntersect(const DWARFAddressRange &LHS,
3035 const DWARFAddressRange &RHS) {
3036 ASSERT_TRUE(LHS.intersects(RHS));
3037 ASSERT_TRUE(RHS.intersects(LHS));
3039 void AssertRangesDontIntersect(const DWARFAddressRange &LHS,
3040 const DWARFAddressRange &RHS) {
3041 ASSERT_FALSE(LHS.intersects(RHS));
3042 ASSERT_FALSE(RHS.intersects(LHS));
3045 void AssertRangesIntersect(const DWARFVerifier::DieRangeInfo &LHS,
3046 const DWARFAddressRangesVector &Ranges) {
3047 DWARFVerifier::DieRangeInfo RHS(Ranges);
3048 ASSERT_TRUE(LHS.intersects(RHS));
3049 ASSERT_TRUE(RHS.intersects(LHS));
3052 void AssertRangesDontIntersect(const DWARFVerifier::DieRangeInfo &LHS,
3053 const DWARFAddressRangesVector &Ranges) {
3054 DWARFVerifier::DieRangeInfo RHS(Ranges);
3055 ASSERT_FALSE(LHS.intersects(RHS));
3056 ASSERT_FALSE(RHS.intersects(LHS));
3059 } // namespace
3060 TEST(DWARFDebugInfo, TestDwarfRangesIntersect) {
3061 DWARFAddressRange R(0x10, 0x20);
3063 //----------------------------------------------------------------------
3064 // Test ranges that start before R...
3065 //----------------------------------------------------------------------
3066 // Other range ends before start of R
3067 AssertRangesDontIntersect(R, {0x00, 0x10});
3068 // Other range end address is start of a R
3069 AssertRangesIntersect(R, {0x00, 0x11});
3070 // Other range end address is in R
3071 AssertRangesIntersect(R, {0x00, 0x15});
3072 // Other range end address is at and of R
3073 AssertRangesIntersect(R, {0x00, 0x20});
3074 // Other range end address is past end of R
3075 AssertRangesIntersect(R, {0x00, 0x40});
3077 //----------------------------------------------------------------------
3078 // Test ranges that start at R's start address
3079 //----------------------------------------------------------------------
3080 // Ensure empty ranges doesn't match
3081 AssertRangesDontIntersect(R, {0x10, 0x10});
3082 // 1 byte of Range
3083 AssertRangesIntersect(R, {0x10, 0x11});
3084 // same as Range
3085 AssertRangesIntersect(R, {0x10, 0x20});
3086 // 1 byte past Range
3087 AssertRangesIntersect(R, {0x10, 0x21});
3089 //----------------------------------------------------------------------
3090 // Test ranges that start inside Range
3091 //----------------------------------------------------------------------
3092 // empty in range
3093 AssertRangesDontIntersect(R, {0x11, 0x11});
3094 // all in Range
3095 AssertRangesIntersect(R, {0x11, 0x1f});
3096 // ends at end of Range
3097 AssertRangesIntersect(R, {0x11, 0x20});
3098 // ends past Range
3099 AssertRangesIntersect(R, {0x11, 0x21});
3101 //----------------------------------------------------------------------
3102 // Test ranges that start at last bytes of Range
3103 //----------------------------------------------------------------------
3104 // ends at end of Range
3105 AssertRangesIntersect(R, {0x1f, 0x20});
3106 // ends past Range
3107 AssertRangesIntersect(R, {0x1f, 0x21});
3109 //----------------------------------------------------------------------
3110 // Test ranges that start after Range
3111 //----------------------------------------------------------------------
3112 // empty just past in Range
3113 AssertRangesDontIntersect(R, {0x20, 0x20});
3114 // valid past Range
3115 AssertRangesDontIntersect(R, {0x20, 0x21});
3118 TEST(DWARFDebugInfo, TestDWARFDieRangeInfoIntersects) {
3120 DWARFVerifier::DieRangeInfo Ranges({{0x10, 0x20}, {0x30, 0x40}});
3122 // Test empty range
3123 AssertRangesDontIntersect(Ranges, {});
3124 // Test range that appears before all ranges in Ranges
3125 AssertRangesDontIntersect(Ranges, {{0x00, 0x10}});
3126 // Test range that appears between ranges in Ranges
3127 AssertRangesDontIntersect(Ranges, {{0x20, 0x30}});
3128 // Test range that appears after ranges in Ranges
3129 AssertRangesDontIntersect(Ranges, {{0x40, 0x50}});
3131 // Test range that start before first range
3132 AssertRangesIntersect(Ranges, {{0x00, 0x11}});
3133 // Test range that start at first range
3134 AssertRangesIntersect(Ranges, {{0x10, 0x11}});
3135 // Test range that start in first range
3136 AssertRangesIntersect(Ranges, {{0x11, 0x12}});
3137 // Test range that start at end of first range
3138 AssertRangesIntersect(Ranges, {{0x1f, 0x20}});
3139 // Test range that starts at end of first range
3140 AssertRangesDontIntersect(Ranges, {{0x20, 0x21}});
3141 // Test range that starts at end of first range
3142 AssertRangesIntersect(Ranges, {{0x20, 0x31}});
3144 // Test range that start before second range and ends before second
3145 AssertRangesDontIntersect(Ranges, {{0x2f, 0x30}});
3146 // Test range that start before second range and ends in second
3147 AssertRangesIntersect(Ranges, {{0x2f, 0x31}});
3148 // Test range that start at second range
3149 AssertRangesIntersect(Ranges, {{0x30, 0x31}});
3150 // Test range that start in second range
3151 AssertRangesIntersect(Ranges, {{0x31, 0x32}});
3152 // Test range that start at end of second range
3153 AssertRangesIntersect(Ranges, {{0x3f, 0x40}});
3154 // Test range that starts at end of second range
3155 AssertRangesDontIntersect(Ranges, {{0x40, 0x41}});
3157 AssertRangesDontIntersect(Ranges, {{0x20, 0x21}, {0x2f, 0x30}});
3158 AssertRangesIntersect(Ranges, {{0x20, 0x21}, {0x2f, 0x31}});
3161 TEST(DWARFDebugInfo, TestDWARF64UnitLength) {
3162 static const char DebugInfoSecRaw[] =
3163 "\xff\xff\xff\xff" // DWARF64 mark
3164 "\x88\x77\x66\x55\x44\x33\x22\x11" // Length
3165 "\x05\x00" // Version
3166 "\x01" // DW_UT_compile
3167 "\x04" // Address size
3168 "\0\0\0\0\0\0\0\0"; // Offset Into Abbrev. Sec.
3169 StringMap<std::unique_ptr<MemoryBuffer>> Sections;
3170 Sections.insert(std::make_pair(
3171 "debug_info", MemoryBuffer::getMemBuffer(StringRef(
3172 DebugInfoSecRaw, sizeof(DebugInfoSecRaw) - 1))));
3173 auto Context = DWARFContext::create(Sections, /* AddrSize = */ 4,
3174 /* isLittleEndian = */ true);
3175 const auto &Obj = Context->getDWARFObj();
3176 Obj.forEachInfoSections([&](const DWARFSection &Sec) {
3177 DWARFUnitHeader Header;
3178 DWARFDataExtractor Data(Obj, Sec, /* IsLittleEndian = */ true,
3179 /* AddressSize = */ 4);
3180 uint64_t Offset = 0;
3181 EXPECT_FALSE(Header.extract(*Context, Data, &Offset));
3182 // Header.extract() returns false because there is not enough space
3183 // in the section for the declared length. Anyway, we can check that
3184 // the properties are read correctly.
3185 ASSERT_EQ(DwarfFormat::DWARF64, Header.getFormat());
3186 ASSERT_EQ(0x1122334455667788ULL, Header.getLength());
3187 ASSERT_EQ(5, Header.getVersion());
3188 ASSERT_EQ(DW_UT_compile, Header.getUnitType());
3189 ASSERT_EQ(4, Header.getAddressByteSize());
3191 // Check that the length can be correctly read in the unit class.
3192 DWARFUnitVector DummyUnitVector;
3193 DWARFSection DummySec;
3194 DWARFCompileUnit CU(*Context, Sec, Header, /* DA = */ 0, /* RS = */ 0,
3195 /* LocSection = */ 0, /* SS = */ StringRef(),
3196 /* SOS = */ DummySec, /* AOS = */ 0,
3197 /* LS = */ DummySec, /* LE = */ true,
3198 /* isDWO= */ false, DummyUnitVector);
3199 ASSERT_EQ(0x1122334455667788ULL, CU.getLength());
3203 } // end anonymous namespace