blob: 02af290a123ee6e1c3f32cca2d7b9be513bdc856 [file] [log] [blame]
James Henderson66702622018-03-08 10:53:34 +00001//===- llvm/unittest/DebugInfo/DWARFDebugInfoTest.cpp ---------------------===//
Greg Clayton3462a422016-12-08 01:03:48 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Jasper0f778692016-12-08 12:45:29 +000010#include "DwarfGenerator.h"
James Henderson66702622018-03-08 10:53:34 +000011#include "DwarfUtils.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000012#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/Optional.h"
Greg Clayton67070462017-05-02 22:48:52 +000014#include "llvm/ADT/SmallString.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000015#include "llvm/ADT/StringRef.h"
16#include "llvm/ADT/Triple.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000017#include "llvm/BinaryFormat/Dwarf.h"
George Rimar1af3cb22017-06-28 08:21:19 +000018#include "llvm/CodeGen/AsmPrinter.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000019#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
Greg Clayton3462a422016-12-08 01:03:48 +000020#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Greg Claytonc8c10322016-12-13 18:25:19 +000021#include "llvm/DebugInfo/DWARF/DWARFDie.h"
Greg Clayton3462a422016-12-08 01:03:48 +000022#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Jonas Devlieghere58910602017-09-14 11:33:42 +000023#include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
George Rimar1af3cb22017-06-28 08:21:19 +000024#include "llvm/MC/MCContext.h"
25#include "llvm/MC/MCSectionELF.h"
26#include "llvm/MC/MCStreamer.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000027#include "llvm/Object/ObjectFile.h"
Chris Bieneman2e752db2017-01-20 19:03:14 +000028#include "llvm/ObjectYAML/DWARFEmitter.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000029#include "llvm/Support/Error.h"
30#include "llvm/Support/MemoryBuffer.h"
George Rimard8508b02017-06-30 10:09:01 +000031#include "llvm/Support/TargetRegistry.h"
Rafael Espindolac398e672017-07-19 22:27:28 +000032#include "llvm/Support/TargetSelect.h"
George Rimard8508b02017-06-30 10:09:01 +000033#include "llvm/Testing/Support/Error.h"
Greg Clayton3462a422016-12-08 01:03:48 +000034#include "gtest/gtest.h"
Eugene Zelenko44d95122017-02-09 01:09:54 +000035#include <string>
Greg Clayton3462a422016-12-08 01:03:48 +000036
37using namespace llvm;
38using namespace dwarf;
James Henderson66702622018-03-08 10:53:34 +000039using namespace utils;
Greg Clayton3462a422016-12-08 01:03:48 +000040
41namespace {
42
Greg Clayton3462a422016-12-08 01:03:48 +000043template <uint16_t Version, class AddrType, class RefAddrType>
44void TestAllForms() {
George Rimard8508b02017-06-30 10:09:01 +000045 Triple Triple = getHostTripleForAddrSize(sizeof(AddrType));
46 if (!isConfigurationSupported(Triple))
47 return;
Greg Clayton3462a422016-12-08 01:03:48 +000048
George Rimard8508b02017-06-30 10:09:01 +000049 // Test that we can decode all DW_FORM values correctly.
Greg Clayton3462a422016-12-08 01:03:48 +000050 const AddrType AddrValue = (AddrType)0x0123456789abcdefULL;
51 const uint8_t BlockData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
52 const uint32_t BlockSize = sizeof(BlockData);
53 const RefAddrType RefAddr = 0x12345678;
54 const uint8_t Data1 = 0x01U;
55 const uint16_t Data2 = 0x2345U;
56 const uint32_t Data4 = 0x6789abcdU;
57 const uint64_t Data8 = 0x0011223344556677ULL;
58 const uint64_t Data8_2 = 0xAABBCCDDEEFF0011ULL;
Paul Robinsona06f8dc2017-12-18 19:08:35 +000059 const uint8_t Data16[16] = {1, 2, 3, 4, 5, 6, 7, 8,
60 9, 10, 11, 12, 13, 14, 15, 16};
Greg Clayton3462a422016-12-08 01:03:48 +000061 const int64_t SData = INT64_MIN;
Victor Leschukcbddae72017-01-10 21:18:26 +000062 const int64_t ICSData = INT64_MAX; // DW_FORM_implicit_const SData
Greg Clayton3462a422016-12-08 01:03:48 +000063 const uint64_t UData[] = {UINT64_MAX - 1, UINT64_MAX - 2, UINT64_MAX - 3,
64 UINT64_MAX - 4, UINT64_MAX - 5, UINT64_MAX - 6,
65 UINT64_MAX - 7, UINT64_MAX - 8, UINT64_MAX - 9};
66#define UDATA_1 18446744073709551614ULL
67 const uint32_t Dwarf32Values[] = {1, 2, 3, 4, 5, 6, 7, 8};
68 const char *StringValue = "Hello";
69 const char *StrpValue = "World";
George Rimard8508b02017-06-30 10:09:01 +000070
Greg Clayton3462a422016-12-08 01:03:48 +000071 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
George Rimard8508b02017-06-30 10:09:01 +000072 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Clayton3462a422016-12-08 01:03:48 +000073 dwarfgen::Generator *DG = ExpectedDG.get().get();
74 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
75 dwarfgen::DIE CUDie = CU.getUnitDIE();
76 uint16_t Attr = DW_AT_lo_user;
77
78 //----------------------------------------------------------------------
79 // Test address forms
80 //----------------------------------------------------------------------
81 const auto Attr_DW_FORM_addr = static_cast<dwarf::Attribute>(Attr++);
82 CUDie.addAttribute(Attr_DW_FORM_addr, DW_FORM_addr, AddrValue);
83
84 //----------------------------------------------------------------------
85 // Test block forms
86 //----------------------------------------------------------------------
87 const auto Attr_DW_FORM_block = static_cast<dwarf::Attribute>(Attr++);
88 CUDie.addAttribute(Attr_DW_FORM_block, DW_FORM_block, BlockData, BlockSize);
89
90 const auto Attr_DW_FORM_block1 = static_cast<dwarf::Attribute>(Attr++);
91 CUDie.addAttribute(Attr_DW_FORM_block1, DW_FORM_block1, BlockData, BlockSize);
92
93 const auto Attr_DW_FORM_block2 = static_cast<dwarf::Attribute>(Attr++);
94 CUDie.addAttribute(Attr_DW_FORM_block2, DW_FORM_block2, BlockData, BlockSize);
95
96 const auto Attr_DW_FORM_block4 = static_cast<dwarf::Attribute>(Attr++);
97 CUDie.addAttribute(Attr_DW_FORM_block4, DW_FORM_block4, BlockData, BlockSize);
98
Paul Robinsona06f8dc2017-12-18 19:08:35 +000099 // We handle data16 as a block form.
100 const auto Attr_DW_FORM_data16 = static_cast<dwarf::Attribute>(Attr++);
101 if (Version >= 5)
102 CUDie.addAttribute(Attr_DW_FORM_data16, DW_FORM_data16, Data16, 16);
103
Greg Clayton3462a422016-12-08 01:03:48 +0000104 //----------------------------------------------------------------------
105 // Test data forms
106 //----------------------------------------------------------------------
107 const auto Attr_DW_FORM_data1 = static_cast<dwarf::Attribute>(Attr++);
108 CUDie.addAttribute(Attr_DW_FORM_data1, DW_FORM_data1, Data1);
109
110 const auto Attr_DW_FORM_data2 = static_cast<dwarf::Attribute>(Attr++);
111 CUDie.addAttribute(Attr_DW_FORM_data2, DW_FORM_data2, Data2);
112
113 const auto Attr_DW_FORM_data4 = static_cast<dwarf::Attribute>(Attr++);
114 CUDie.addAttribute(Attr_DW_FORM_data4, DW_FORM_data4, Data4);
115
116 const auto Attr_DW_FORM_data8 = static_cast<dwarf::Attribute>(Attr++);
117 CUDie.addAttribute(Attr_DW_FORM_data8, DW_FORM_data8, Data8);
118
119 //----------------------------------------------------------------------
120 // Test string forms
121 //----------------------------------------------------------------------
122 const auto Attr_DW_FORM_string = static_cast<dwarf::Attribute>(Attr++);
123 CUDie.addAttribute(Attr_DW_FORM_string, DW_FORM_string, StringValue);
124
125 const auto Attr_DW_FORM_strp = static_cast<dwarf::Attribute>(Attr++);
126 CUDie.addAttribute(Attr_DW_FORM_strp, DW_FORM_strp, StrpValue);
127
128 //----------------------------------------------------------------------
129 // Test reference forms
130 //----------------------------------------------------------------------
131 const auto Attr_DW_FORM_ref_addr = static_cast<dwarf::Attribute>(Attr++);
132 CUDie.addAttribute(Attr_DW_FORM_ref_addr, DW_FORM_ref_addr, RefAddr);
133
134 const auto Attr_DW_FORM_ref1 = static_cast<dwarf::Attribute>(Attr++);
135 CUDie.addAttribute(Attr_DW_FORM_ref1, DW_FORM_ref1, Data1);
136
137 const auto Attr_DW_FORM_ref2 = static_cast<dwarf::Attribute>(Attr++);
138 CUDie.addAttribute(Attr_DW_FORM_ref2, DW_FORM_ref2, Data2);
139
140 const auto Attr_DW_FORM_ref4 = static_cast<dwarf::Attribute>(Attr++);
141 CUDie.addAttribute(Attr_DW_FORM_ref4, DW_FORM_ref4, Data4);
142
143 const auto Attr_DW_FORM_ref8 = static_cast<dwarf::Attribute>(Attr++);
144 CUDie.addAttribute(Attr_DW_FORM_ref8, DW_FORM_ref8, Data8);
145
146 const auto Attr_DW_FORM_ref_sig8 = static_cast<dwarf::Attribute>(Attr++);
Paul Robinson70b34532017-04-20 19:16:51 +0000147 if (Version >= 4)
148 CUDie.addAttribute(Attr_DW_FORM_ref_sig8, DW_FORM_ref_sig8, Data8_2);
Greg Clayton3462a422016-12-08 01:03:48 +0000149
150 const auto Attr_DW_FORM_ref_udata = static_cast<dwarf::Attribute>(Attr++);
151 CUDie.addAttribute(Attr_DW_FORM_ref_udata, DW_FORM_ref_udata, UData[0]);
152
153 //----------------------------------------------------------------------
154 // Test flag forms
155 //----------------------------------------------------------------------
156 const auto Attr_DW_FORM_flag_true = static_cast<dwarf::Attribute>(Attr++);
157 CUDie.addAttribute(Attr_DW_FORM_flag_true, DW_FORM_flag, true);
158
159 const auto Attr_DW_FORM_flag_false = static_cast<dwarf::Attribute>(Attr++);
160 CUDie.addAttribute(Attr_DW_FORM_flag_false, DW_FORM_flag, false);
161
162 const auto Attr_DW_FORM_flag_present = static_cast<dwarf::Attribute>(Attr++);
Paul Robinson70b34532017-04-20 19:16:51 +0000163 if (Version >= 4)
164 CUDie.addAttribute(Attr_DW_FORM_flag_present, DW_FORM_flag_present);
Greg Clayton3462a422016-12-08 01:03:48 +0000165
166 //----------------------------------------------------------------------
167 // Test SLEB128 based forms
168 //----------------------------------------------------------------------
169 const auto Attr_DW_FORM_sdata = static_cast<dwarf::Attribute>(Attr++);
170 CUDie.addAttribute(Attr_DW_FORM_sdata, DW_FORM_sdata, SData);
171
Victor Leschukcbddae72017-01-10 21:18:26 +0000172 const auto Attr_DW_FORM_implicit_const =
173 static_cast<dwarf::Attribute>(Attr++);
174 if (Version >= 5)
175 CUDie.addAttribute(Attr_DW_FORM_implicit_const, DW_FORM_implicit_const,
176 ICSData);
177
Greg Clayton3462a422016-12-08 01:03:48 +0000178 //----------------------------------------------------------------------
179 // Test ULEB128 based forms
180 //----------------------------------------------------------------------
181 const auto Attr_DW_FORM_udata = static_cast<dwarf::Attribute>(Attr++);
182 CUDie.addAttribute(Attr_DW_FORM_udata, DW_FORM_udata, UData[0]);
183
184 //----------------------------------------------------------------------
185 // Test DWARF32/DWARF64 forms
186 //----------------------------------------------------------------------
187 const auto Attr_DW_FORM_GNU_ref_alt = static_cast<dwarf::Attribute>(Attr++);
188 CUDie.addAttribute(Attr_DW_FORM_GNU_ref_alt, DW_FORM_GNU_ref_alt,
189 Dwarf32Values[0]);
190
191 const auto Attr_DW_FORM_sec_offset = static_cast<dwarf::Attribute>(Attr++);
Paul Robinson70b34532017-04-20 19:16:51 +0000192 if (Version >= 4)
193 CUDie.addAttribute(Attr_DW_FORM_sec_offset, DW_FORM_sec_offset,
194 Dwarf32Values[1]);
Greg Clayton3462a422016-12-08 01:03:48 +0000195
196 //----------------------------------------------------------------------
197 // Add an address at the end to make sure we can decode this value
198 //----------------------------------------------------------------------
199 const auto Attr_Last = static_cast<dwarf::Attribute>(Attr++);
200 CUDie.addAttribute(Attr_Last, DW_FORM_addr, AddrValue);
201
202 //----------------------------------------------------------------------
203 // Generate the DWARF
204 //----------------------------------------------------------------------
205 StringRef FileBytes = DG->generate();
206 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
207 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
208 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +0000209 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
210 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton3462a422016-12-08 01:03:48 +0000211 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +0000212 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
Greg Claytonc8c10322016-12-13 18:25:19 +0000213 auto DieDG = U->getUnitDIE(false);
214 EXPECT_TRUE(DieDG.isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000215
216 //----------------------------------------------------------------------
217 // Test address forms
218 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000219 EXPECT_EQ(AddrValue, toAddress(DieDG.find(Attr_DW_FORM_addr), 0));
Greg Clayton3462a422016-12-08 01:03:48 +0000220
221 //----------------------------------------------------------------------
222 // Test block forms
223 //----------------------------------------------------------------------
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000224 Optional<DWARFFormValue> FormValue;
Greg Clayton3462a422016-12-08 01:03:48 +0000225 ArrayRef<uint8_t> ExtractedBlockData;
226 Optional<ArrayRef<uint8_t>> BlockDataOpt;
227
Greg Clayton97d22182017-01-13 21:08:18 +0000228 FormValue = DieDG.find(Attr_DW_FORM_block);
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000229 EXPECT_TRUE((bool)FormValue);
230 BlockDataOpt = FormValue->getAsBlock();
Greg Clayton3462a422016-12-08 01:03:48 +0000231 EXPECT_TRUE(BlockDataOpt.hasValue());
232 ExtractedBlockData = BlockDataOpt.getValue();
233 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
234 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
235
Greg Clayton97d22182017-01-13 21:08:18 +0000236 FormValue = DieDG.find(Attr_DW_FORM_block1);
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000237 EXPECT_TRUE((bool)FormValue);
238 BlockDataOpt = FormValue->getAsBlock();
Greg Clayton3462a422016-12-08 01:03:48 +0000239 EXPECT_TRUE(BlockDataOpt.hasValue());
240 ExtractedBlockData = BlockDataOpt.getValue();
241 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
242 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
243
Greg Clayton97d22182017-01-13 21:08:18 +0000244 FormValue = DieDG.find(Attr_DW_FORM_block2);
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000245 EXPECT_TRUE((bool)FormValue);
246 BlockDataOpt = FormValue->getAsBlock();
Greg Clayton3462a422016-12-08 01:03:48 +0000247 EXPECT_TRUE(BlockDataOpt.hasValue());
248 ExtractedBlockData = BlockDataOpt.getValue();
249 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
250 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
251
Greg Clayton97d22182017-01-13 21:08:18 +0000252 FormValue = DieDG.find(Attr_DW_FORM_block4);
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000253 EXPECT_TRUE((bool)FormValue);
254 BlockDataOpt = FormValue->getAsBlock();
Greg Clayton3462a422016-12-08 01:03:48 +0000255 EXPECT_TRUE(BlockDataOpt.hasValue());
256 ExtractedBlockData = BlockDataOpt.getValue();
257 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
258 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
259
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000260 // Data16 is handled like a block.
261 if (Version >= 5) {
262 FormValue = DieDG.find(Attr_DW_FORM_data16);
263 EXPECT_TRUE((bool)FormValue);
264 BlockDataOpt = FormValue->getAsBlock();
265 EXPECT_TRUE(BlockDataOpt.hasValue());
266 ExtractedBlockData = BlockDataOpt.getValue();
267 EXPECT_EQ(ExtractedBlockData.size(), 16u);
268 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), Data16, 16) == 0);
269 }
270
Greg Clayton3462a422016-12-08 01:03:48 +0000271 //----------------------------------------------------------------------
272 // Test data forms
273 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000274 EXPECT_EQ(Data1, toUnsigned(DieDG.find(Attr_DW_FORM_data1), 0));
275 EXPECT_EQ(Data2, toUnsigned(DieDG.find(Attr_DW_FORM_data2), 0));
276 EXPECT_EQ(Data4, toUnsigned(DieDG.find(Attr_DW_FORM_data4), 0));
277 EXPECT_EQ(Data8, toUnsigned(DieDG.find(Attr_DW_FORM_data8), 0));
Greg Clayton3462a422016-12-08 01:03:48 +0000278
279 //----------------------------------------------------------------------
280 // Test string forms
281 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000282 auto ExtractedStringValue = toString(DieDG.find(Attr_DW_FORM_string));
283 EXPECT_TRUE((bool)ExtractedStringValue);
284 EXPECT_TRUE(strcmp(StringValue, *ExtractedStringValue) == 0);
Greg Clayton3462a422016-12-08 01:03:48 +0000285
Greg Clayton97d22182017-01-13 21:08:18 +0000286 auto ExtractedStrpValue = toString(DieDG.find(Attr_DW_FORM_strp));
287 EXPECT_TRUE((bool)ExtractedStrpValue);
288 EXPECT_TRUE(strcmp(StrpValue, *ExtractedStrpValue) == 0);
Greg Clayton3462a422016-12-08 01:03:48 +0000289
290 //----------------------------------------------------------------------
291 // Test reference forms
292 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000293 EXPECT_EQ(RefAddr, toReference(DieDG.find(Attr_DW_FORM_ref_addr), 0));
294 EXPECT_EQ(Data1, toReference(DieDG.find(Attr_DW_FORM_ref1), 0));
295 EXPECT_EQ(Data2, toReference(DieDG.find(Attr_DW_FORM_ref2), 0));
296 EXPECT_EQ(Data4, toReference(DieDG.find(Attr_DW_FORM_ref4), 0));
297 EXPECT_EQ(Data8, toReference(DieDG.find(Attr_DW_FORM_ref8), 0));
Galina Kistanovafcae62d2017-06-15 21:00:40 +0000298 if (Version >= 4) {
Paul Robinson70b34532017-04-20 19:16:51 +0000299 EXPECT_EQ(Data8_2, toReference(DieDG.find(Attr_DW_FORM_ref_sig8), 0));
Galina Kistanovafcae62d2017-06-15 21:00:40 +0000300 }
Greg Clayton97d22182017-01-13 21:08:18 +0000301 EXPECT_EQ(UData[0], toReference(DieDG.find(Attr_DW_FORM_ref_udata), 0));
Greg Clayton3462a422016-12-08 01:03:48 +0000302
303 //----------------------------------------------------------------------
304 // Test flag forms
305 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000306 EXPECT_EQ(1ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_true), 0));
307 EXPECT_EQ(0ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_false), 1));
Galina Kistanovafcae62d2017-06-15 21:00:40 +0000308 if (Version >= 4) {
Paul Robinson70b34532017-04-20 19:16:51 +0000309 EXPECT_EQ(1ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_present), 0));
Galina Kistanovafcae62d2017-06-15 21:00:40 +0000310 }
Greg Clayton3462a422016-12-08 01:03:48 +0000311
Greg Clayton3462a422016-12-08 01:03:48 +0000312 //----------------------------------------------------------------------
313 // Test SLEB128 based forms
314 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000315 EXPECT_EQ(SData, toSigned(DieDG.find(Attr_DW_FORM_sdata), 0));
Galina Kistanovafcae62d2017-06-15 21:00:40 +0000316 if (Version >= 5) {
Greg Clayton97d22182017-01-13 21:08:18 +0000317 EXPECT_EQ(ICSData, toSigned(DieDG.find(Attr_DW_FORM_implicit_const), 0));
Galina Kistanovafcae62d2017-06-15 21:00:40 +0000318 }
Greg Clayton3462a422016-12-08 01:03:48 +0000319
320 //----------------------------------------------------------------------
321 // Test ULEB128 based forms
322 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000323 EXPECT_EQ(UData[0], toUnsigned(DieDG.find(Attr_DW_FORM_udata), 0));
Greg Clayton3462a422016-12-08 01:03:48 +0000324
325 //----------------------------------------------------------------------
326 // Test DWARF32/DWARF64 forms
327 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000328 EXPECT_EQ(Dwarf32Values[0],
329 toReference(DieDG.find(Attr_DW_FORM_GNU_ref_alt), 0));
Galina Kistanovafcae62d2017-06-15 21:00:40 +0000330 if (Version >= 4) {
Paul Robinson70b34532017-04-20 19:16:51 +0000331 EXPECT_EQ(Dwarf32Values[1],
332 toSectionOffset(DieDG.find(Attr_DW_FORM_sec_offset), 0));
Galina Kistanovafcae62d2017-06-15 21:00:40 +0000333 }
Greg Clayton3462a422016-12-08 01:03:48 +0000334
335 //----------------------------------------------------------------------
336 // Add an address at the end to make sure we can decode this value
337 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000338 EXPECT_EQ(AddrValue, toAddress(DieDG.find(Attr_Last), 0));
Greg Clayton3462a422016-12-08 01:03:48 +0000339}
340
341TEST(DWARFDebugInfo, TestDWARF32Version2Addr4AllForms) {
342 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
343 // addresses.
344 typedef uint32_t AddrType;
345 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2.
346 typedef AddrType RefAddrType;
347 TestAllForms<2, AddrType, RefAddrType>();
348}
349
350TEST(DWARFDebugInfo, TestDWARF32Version2Addr8AllForms) {
351 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
352 // addresses.
353 typedef uint64_t AddrType;
354 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2.
355 typedef AddrType RefAddrType;
356 TestAllForms<2, AddrType, RefAddrType>();
357}
358
359TEST(DWARFDebugInfo, TestDWARF32Version3Addr4AllForms) {
360 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
361 // addresses.
362 typedef uint32_t AddrType;
363 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later.
364 typedef uint32_t RefAddrType;
365 TestAllForms<3, AddrType, RefAddrType>();
366}
367
368TEST(DWARFDebugInfo, TestDWARF32Version3Addr8AllForms) {
369 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
370 // addresses.
371 typedef uint64_t AddrType;
372 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
373 typedef uint32_t RefAddrType;
374 TestAllForms<3, AddrType, RefAddrType>();
375}
376
377TEST(DWARFDebugInfo, TestDWARF32Version4Addr4AllForms) {
378 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
379 // addresses.
380 typedef uint32_t AddrType;
381 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
382 typedef uint32_t RefAddrType;
383 TestAllForms<4, AddrType, RefAddrType>();
384}
385
386TEST(DWARFDebugInfo, TestDWARF32Version4Addr8AllForms) {
387 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
388 // addresses.
389 typedef uint64_t AddrType;
390 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
391 typedef uint32_t RefAddrType;
392 TestAllForms<4, AddrType, RefAddrType>();
393}
394
Victor Leschukcbddae72017-01-10 21:18:26 +0000395TEST(DWARFDebugInfo, TestDWARF32Version5Addr4AllForms) {
396 // Test that we can decode all forms for DWARF32, version 5, with 4 byte
397 // addresses.
398 typedef uint32_t AddrType;
399 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
400 typedef uint32_t RefAddrType;
401 TestAllForms<5, AddrType, RefAddrType>();
402}
403
404TEST(DWARFDebugInfo, TestDWARF32Version5Addr8AllForms) {
405 // Test that we can decode all forms for DWARF32, version 5, with 8 byte
406 // addresses.
407 typedef uint64_t AddrType;
408 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
409 typedef uint32_t RefAddrType;
410 TestAllForms<5, AddrType, RefAddrType>();
411}
412
Greg Clayton3462a422016-12-08 01:03:48 +0000413template <uint16_t Version, class AddrType> void TestChildren() {
George Rimard8508b02017-06-30 10:09:01 +0000414 Triple Triple = getHostTripleForAddrSize(sizeof(AddrType));
415 if (!isConfigurationSupported(Triple))
416 return;
417
Greg Clayton3462a422016-12-08 01:03:48 +0000418 // Test that we can decode DW_FORM_ref_addr values correctly in DWARF 2 with
419 // 4 byte addresses. DW_FORM_ref_addr values should be 4 bytes when using
420 // 8 byte addresses.
421
Greg Clayton3462a422016-12-08 01:03:48 +0000422 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
George Rimard8508b02017-06-30 10:09:01 +0000423 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Clayton3462a422016-12-08 01:03:48 +0000424 dwarfgen::Generator *DG = ExpectedDG.get().get();
425 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
426 dwarfgen::DIE CUDie = CU.getUnitDIE();
427
428 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
429 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
430
431 dwarfgen::DIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram);
432 SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main");
433 SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U);
434 SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U);
435
436 dwarfgen::DIE IntDie = CUDie.addChild(DW_TAG_base_type);
437 IntDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");
438 IntDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
439 IntDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
440
441 dwarfgen::DIE ArgcDie = SubprogramDie.addChild(DW_TAG_formal_parameter);
442 ArgcDie.addAttribute(DW_AT_name, DW_FORM_strp, "argc");
443 // ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref4, IntDie);
444 ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, IntDie);
445
446 StringRef FileBytes = DG->generate();
447 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
448 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
449 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +0000450 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
Greg Clayton3462a422016-12-08 01:03:48 +0000451
452 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +0000453 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton3462a422016-12-08 01:03:48 +0000454 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +0000455 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
Greg Clayton3462a422016-12-08 01:03:48 +0000456
457 // Get the compile unit DIE is valid.
Greg Claytonc8c10322016-12-13 18:25:19 +0000458 auto DieDG = U->getUnitDIE(false);
459 EXPECT_TRUE(DieDG.isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000460
461 // Verify the first child of the compile unit DIE is our subprogram.
Greg Claytonc8c10322016-12-13 18:25:19 +0000462 auto SubprogramDieDG = DieDG.getFirstChild();
463 EXPECT_TRUE(SubprogramDieDG.isValid());
464 EXPECT_EQ(SubprogramDieDG.getTag(), DW_TAG_subprogram);
Greg Clayton3462a422016-12-08 01:03:48 +0000465
466 // Verify the first child of the subprogram is our formal parameter.
Greg Claytonc8c10322016-12-13 18:25:19 +0000467 auto ArgcDieDG = SubprogramDieDG.getFirstChild();
468 EXPECT_TRUE(ArgcDieDG.isValid());
469 EXPECT_EQ(ArgcDieDG.getTag(), DW_TAG_formal_parameter);
Greg Clayton3462a422016-12-08 01:03:48 +0000470
471 // Verify our formal parameter has a NULL tag sibling.
Greg Claytonc8c10322016-12-13 18:25:19 +0000472 auto NullDieDG = ArgcDieDG.getSibling();
473 EXPECT_TRUE(NullDieDG.isValid());
474 if (NullDieDG) {
475 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null);
476 EXPECT_TRUE(!NullDieDG.getSibling().isValid());
477 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000478 }
479
480 // Verify the sibling of our subprogram is our integer base type.
Greg Claytonc8c10322016-12-13 18:25:19 +0000481 auto IntDieDG = SubprogramDieDG.getSibling();
482 EXPECT_TRUE(IntDieDG.isValid());
483 EXPECT_EQ(IntDieDG.getTag(), DW_TAG_base_type);
Greg Clayton3462a422016-12-08 01:03:48 +0000484
485 // Verify the sibling of our subprogram is our integer base is a NULL tag.
Greg Claytonc8c10322016-12-13 18:25:19 +0000486 NullDieDG = IntDieDG.getSibling();
487 EXPECT_TRUE(NullDieDG.isValid());
488 if (NullDieDG) {
489 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null);
490 EXPECT_TRUE(!NullDieDG.getSibling().isValid());
491 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000492 }
493}
494
495TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Children) {
496 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
497 // addresses.
498 typedef uint32_t AddrType;
499 TestChildren<2, AddrType>();
500}
501
502TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Children) {
503 // Test that we can decode all forms for DWARF32, version 2, with 8 byte
504 // addresses.
505 typedef uint64_t AddrType;
506 TestChildren<2, AddrType>();
507}
508
509TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Children) {
510 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
511 // addresses.
512 typedef uint32_t AddrType;
513 TestChildren<3, AddrType>();
514}
515
516TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Children) {
517 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
518 // addresses.
519 typedef uint64_t AddrType;
520 TestChildren<3, AddrType>();
521}
522
523TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Children) {
524 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
525 // addresses.
526 typedef uint32_t AddrType;
527 TestChildren<4, AddrType>();
528}
529
530TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Children) {
531 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
532 // addresses.
533 typedef uint64_t AddrType;
534 TestChildren<4, AddrType>();
535}
536
537template <uint16_t Version, class AddrType> void TestReferences() {
George Rimard8508b02017-06-30 10:09:01 +0000538 Triple Triple = getHostTripleForAddrSize(sizeof(AddrType));
539 if (!isConfigurationSupported(Triple))
Greg Clayton3462a422016-12-08 01:03:48 +0000540 return;
George Rimard8508b02017-06-30 10:09:01 +0000541
542 // Test that we can decode DW_FORM_refXXX values correctly in DWARF.
543 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
544 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Clayton3462a422016-12-08 01:03:48 +0000545 dwarfgen::Generator *DG = ExpectedDG.get().get();
546 dwarfgen::CompileUnit &CU1 = DG->addCompileUnit();
547 dwarfgen::CompileUnit &CU2 = DG->addCompileUnit();
548
549 dwarfgen::DIE CU1Die = CU1.getUnitDIE();
550 CU1Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
551 CU1Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
552
553 dwarfgen::DIE CU1TypeDie = CU1Die.addChild(DW_TAG_base_type);
554 CU1TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");
555 CU1TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
556 CU1TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
557
558 dwarfgen::DIE CU1Ref1Die = CU1Die.addChild(DW_TAG_variable);
559 CU1Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref1");
560 CU1Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU1TypeDie);
561
562 dwarfgen::DIE CU1Ref2Die = CU1Die.addChild(DW_TAG_variable);
563 CU1Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref2");
564 CU1Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU1TypeDie);
565
566 dwarfgen::DIE CU1Ref4Die = CU1Die.addChild(DW_TAG_variable);
567 CU1Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref4");
568 CU1Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU1TypeDie);
569
570 dwarfgen::DIE CU1Ref8Die = CU1Die.addChild(DW_TAG_variable);
571 CU1Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref8");
572 CU1Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU1TypeDie);
573
574 dwarfgen::DIE CU1RefAddrDie = CU1Die.addChild(DW_TAG_variable);
575 CU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1RefAddr");
576 CU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie);
577
578 dwarfgen::DIE CU2Die = CU2.getUnitDIE();
579 CU2Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/foo.c");
580 CU2Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
581
582 dwarfgen::DIE CU2TypeDie = CU2Die.addChild(DW_TAG_base_type);
583 CU2TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "float");
584 CU2TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_float);
585 CU2TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
586
587 dwarfgen::DIE CU2Ref1Die = CU2Die.addChild(DW_TAG_variable);
588 CU2Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref1");
589 CU2Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU2TypeDie);
590
591 dwarfgen::DIE CU2Ref2Die = CU2Die.addChild(DW_TAG_variable);
592 CU2Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref2");
593 CU2Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU2TypeDie);
594
595 dwarfgen::DIE CU2Ref4Die = CU2Die.addChild(DW_TAG_variable);
596 CU2Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref4");
597 CU2Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU2TypeDie);
598
599 dwarfgen::DIE CU2Ref8Die = CU2Die.addChild(DW_TAG_variable);
600 CU2Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref8");
601 CU2Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU2TypeDie);
602
603 dwarfgen::DIE CU2RefAddrDie = CU2Die.addChild(DW_TAG_variable);
604 CU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2RefAddr");
605 CU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie);
606
607 // Refer to a type in CU1 from CU2
608 dwarfgen::DIE CU2ToCU1RefAddrDie = CU2Die.addChild(DW_TAG_variable);
609 CU2ToCU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2ToCU1RefAddr");
610 CU2ToCU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie);
611
612 // Refer to a type in CU2 from CU1
613 dwarfgen::DIE CU1ToCU2RefAddrDie = CU1Die.addChild(DW_TAG_variable);
614 CU1ToCU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1ToCU2RefAddr");
615 CU1ToCU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie);
616
617 StringRef FileBytes = DG->generate();
618 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
619 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
620 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +0000621 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
Greg Clayton3462a422016-12-08 01:03:48 +0000622
623 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +0000624 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton3462a422016-12-08 01:03:48 +0000625 EXPECT_EQ(NumCUs, 2u);
Rafael Espindolac398e672017-07-19 22:27:28 +0000626 DWARFCompileUnit *U1 = DwarfContext->getCompileUnitAtIndex(0);
627 DWARFCompileUnit *U2 = DwarfContext->getCompileUnitAtIndex(1);
Greg Clayton3462a422016-12-08 01:03:48 +0000628
629 // Get the compile unit DIE is valid.
Greg Claytonc8c10322016-12-13 18:25:19 +0000630 auto Unit1DieDG = U1->getUnitDIE(false);
631 EXPECT_TRUE(Unit1DieDG.isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000632
Greg Claytonc8c10322016-12-13 18:25:19 +0000633 auto Unit2DieDG = U2->getUnitDIE(false);
634 EXPECT_TRUE(Unit2DieDG.isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000635
636 // Verify the first child of the compile unit 1 DIE is our int base type.
Greg Claytonc8c10322016-12-13 18:25:19 +0000637 auto CU1TypeDieDG = Unit1DieDG.getFirstChild();
638 EXPECT_TRUE(CU1TypeDieDG.isValid());
639 EXPECT_EQ(CU1TypeDieDG.getTag(), DW_TAG_base_type);
Greg Clayton97d22182017-01-13 21:08:18 +0000640 EXPECT_EQ(DW_ATE_signed, toUnsigned(CU1TypeDieDG.find(DW_AT_encoding), 0));
Greg Clayton3462a422016-12-08 01:03:48 +0000641
642 // Verify the first child of the compile unit 2 DIE is our float base type.
Greg Claytonc8c10322016-12-13 18:25:19 +0000643 auto CU2TypeDieDG = Unit2DieDG.getFirstChild();
644 EXPECT_TRUE(CU2TypeDieDG.isValid());
645 EXPECT_EQ(CU2TypeDieDG.getTag(), DW_TAG_base_type);
Greg Clayton97d22182017-01-13 21:08:18 +0000646 EXPECT_EQ(DW_ATE_float, toUnsigned(CU2TypeDieDG.find(DW_AT_encoding), 0));
Greg Clayton3462a422016-12-08 01:03:48 +0000647
648 // Verify the sibling of the base type DIE is our Ref1 DIE and that its
649 // DW_AT_type points to our base type DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000650 auto CU1Ref1DieDG = CU1TypeDieDG.getSibling();
651 EXPECT_TRUE(CU1Ref1DieDG.isValid());
652 EXPECT_EQ(CU1Ref1DieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000653 EXPECT_EQ(CU1TypeDieDG.getOffset(),
654 toReference(CU1Ref1DieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000655 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our
656 // base type DIE in CU1.
Greg Claytonc8c10322016-12-13 18:25:19 +0000657 auto CU1Ref2DieDG = CU1Ref1DieDG.getSibling();
658 EXPECT_TRUE(CU1Ref2DieDG.isValid());
659 EXPECT_EQ(CU1Ref2DieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000660 EXPECT_EQ(CU1TypeDieDG.getOffset(),
661 toReference(CU1Ref2DieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000662
663 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our
664 // base type DIE in CU1.
Greg Claytonc8c10322016-12-13 18:25:19 +0000665 auto CU1Ref4DieDG = CU1Ref2DieDG.getSibling();
666 EXPECT_TRUE(CU1Ref4DieDG.isValid());
667 EXPECT_EQ(CU1Ref4DieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000668 EXPECT_EQ(CU1TypeDieDG.getOffset(),
669 toReference(CU1Ref4DieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000670
671 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our
672 // base type DIE in CU1.
Greg Claytonc8c10322016-12-13 18:25:19 +0000673 auto CU1Ref8DieDG = CU1Ref4DieDG.getSibling();
674 EXPECT_TRUE(CU1Ref8DieDG.isValid());
675 EXPECT_EQ(CU1Ref8DieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000676 EXPECT_EQ(CU1TypeDieDG.getOffset(),
677 toReference(CU1Ref8DieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000678
679 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our
680 // base type DIE in CU1.
Greg Claytonc8c10322016-12-13 18:25:19 +0000681 auto CU1RefAddrDieDG = CU1Ref8DieDG.getSibling();
682 EXPECT_TRUE(CU1RefAddrDieDG.isValid());
683 EXPECT_EQ(CU1RefAddrDieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000684 EXPECT_EQ(CU1TypeDieDG.getOffset(),
685 toReference(CU1RefAddrDieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000686
687 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its
688 // DW_AT_type points to our base type DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000689 auto CU1ToCU2RefAddrDieDG = CU1RefAddrDieDG.getSibling();
690 EXPECT_TRUE(CU1ToCU2RefAddrDieDG.isValid());
691 EXPECT_EQ(CU1ToCU2RefAddrDieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000692 EXPECT_EQ(CU2TypeDieDG.getOffset(),
693 toReference(CU1ToCU2RefAddrDieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000694
695 // Verify the sibling of the base type DIE is our Ref1 DIE and that its
696 // DW_AT_type points to our base type DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000697 auto CU2Ref1DieDG = CU2TypeDieDG.getSibling();
698 EXPECT_TRUE(CU2Ref1DieDG.isValid());
699 EXPECT_EQ(CU2Ref1DieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000700 EXPECT_EQ(CU2TypeDieDG.getOffset(),
701 toReference(CU2Ref1DieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000702 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our
703 // base type DIE in CU2.
Greg Claytonc8c10322016-12-13 18:25:19 +0000704 auto CU2Ref2DieDG = CU2Ref1DieDG.getSibling();
705 EXPECT_TRUE(CU2Ref2DieDG.isValid());
706 EXPECT_EQ(CU2Ref2DieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000707 EXPECT_EQ(CU2TypeDieDG.getOffset(),
708 toReference(CU2Ref2DieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000709
710 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our
711 // base type DIE in CU2.
Greg Claytonc8c10322016-12-13 18:25:19 +0000712 auto CU2Ref4DieDG = CU2Ref2DieDG.getSibling();
713 EXPECT_TRUE(CU2Ref4DieDG.isValid());
714 EXPECT_EQ(CU2Ref4DieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000715 EXPECT_EQ(CU2TypeDieDG.getOffset(),
716 toReference(CU2Ref4DieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000717
718 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our
719 // base type DIE in CU2.
Greg Claytonc8c10322016-12-13 18:25:19 +0000720 auto CU2Ref8DieDG = CU2Ref4DieDG.getSibling();
721 EXPECT_TRUE(CU2Ref8DieDG.isValid());
722 EXPECT_EQ(CU2Ref8DieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000723 EXPECT_EQ(CU2TypeDieDG.getOffset(),
724 toReference(CU2Ref8DieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000725
726 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our
727 // base type DIE in CU2.
Greg Claytonc8c10322016-12-13 18:25:19 +0000728 auto CU2RefAddrDieDG = CU2Ref8DieDG.getSibling();
729 EXPECT_TRUE(CU2RefAddrDieDG.isValid());
730 EXPECT_EQ(CU2RefAddrDieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000731 EXPECT_EQ(CU2TypeDieDG.getOffset(),
732 toReference(CU2RefAddrDieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000733
734 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its
735 // DW_AT_type points to our base type DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000736 auto CU2ToCU1RefAddrDieDG = CU2RefAddrDieDG.getSibling();
737 EXPECT_TRUE(CU2ToCU1RefAddrDieDG.isValid());
738 EXPECT_EQ(CU2ToCU1RefAddrDieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000739 EXPECT_EQ(CU1TypeDieDG.getOffset(),
740 toReference(CU2ToCU1RefAddrDieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000741}
742
743TEST(DWARFDebugInfo, TestDWARF32Version2Addr4References) {
744 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
745 // addresses.
746 typedef uint32_t AddrType;
747 TestReferences<2, AddrType>();
748}
749
750TEST(DWARFDebugInfo, TestDWARF32Version2Addr8References) {
751 // Test that we can decode all forms for DWARF32, version 2, with 8 byte
752 // addresses.
753 typedef uint64_t AddrType;
754 TestReferences<2, AddrType>();
755}
756
757TEST(DWARFDebugInfo, TestDWARF32Version3Addr4References) {
758 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
759 // addresses.
760 typedef uint32_t AddrType;
761 TestReferences<3, AddrType>();
762}
763
764TEST(DWARFDebugInfo, TestDWARF32Version3Addr8References) {
765 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
766 // addresses.
767 typedef uint64_t AddrType;
768 TestReferences<3, AddrType>();
769}
770
771TEST(DWARFDebugInfo, TestDWARF32Version4Addr4References) {
772 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
773 // addresses.
774 typedef uint32_t AddrType;
775 TestReferences<4, AddrType>();
776}
777
778TEST(DWARFDebugInfo, TestDWARF32Version4Addr8References) {
779 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
780 // addresses.
781 typedef uint64_t AddrType;
782 TestReferences<4, AddrType>();
783}
784
Greg Clayton2520c9e2016-12-19 20:36:41 +0000785template <uint16_t Version, class AddrType> void TestAddresses() {
George Rimard8508b02017-06-30 10:09:01 +0000786 Triple Triple = getHostTripleForAddrSize(sizeof(AddrType));
787 if (!isConfigurationSupported(Triple))
788 return;
789
Greg Clayton2520c9e2016-12-19 20:36:41 +0000790 // Test the DWARF APIs related to accessing the DW_AT_low_pc and
791 // DW_AT_high_pc.
Greg Clayton2520c9e2016-12-19 20:36:41 +0000792 const bool SupportsHighPCAsOffset = Version >= 4;
Greg Clayton2520c9e2016-12-19 20:36:41 +0000793 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
George Rimard8508b02017-06-30 10:09:01 +0000794 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Clayton2520c9e2016-12-19 20:36:41 +0000795 dwarfgen::Generator *DG = ExpectedDG.get().get();
796 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
797 dwarfgen::DIE CUDie = CU.getUnitDIE();
George Rimar002655d2017-06-28 08:26:57 +0000798
Greg Clayton2520c9e2016-12-19 20:36:41 +0000799 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
800 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
George Rimar002655d2017-06-28 08:26:57 +0000801
Greg Clayton2520c9e2016-12-19 20:36:41 +0000802 // Create a subprogram DIE with no low or high PC.
803 dwarfgen::DIE SubprogramNoPC = CUDie.addChild(DW_TAG_subprogram);
804 SubprogramNoPC.addAttribute(DW_AT_name, DW_FORM_strp, "no_pc");
805
806 // Create a subprogram DIE with a low PC only.
807 dwarfgen::DIE SubprogramLowPC = CUDie.addChild(DW_TAG_subprogram);
808 SubprogramLowPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_pc");
809 const uint64_t ActualLowPC = 0x1000;
810 const uint64_t ActualHighPC = 0x2000;
811 const uint64_t ActualHighPCOffset = ActualHighPC - ActualLowPC;
812 SubprogramLowPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC);
813
814 // Create a subprogram DIE with a low and high PC.
815 dwarfgen::DIE SubprogramLowHighPC = CUDie.addChild(DW_TAG_subprogram);
816 SubprogramLowHighPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_high_pc");
817 SubprogramLowHighPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC);
818 // Encode the high PC as an offset from the low PC if supported.
819 if (SupportsHighPCAsOffset)
820 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_data4,
821 ActualHighPCOffset);
822 else
823 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_addr, ActualHighPC);
George Rimar002655d2017-06-28 08:26:57 +0000824
Greg Clayton2520c9e2016-12-19 20:36:41 +0000825 StringRef FileBytes = DG->generate();
826 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
827 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
828 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +0000829 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
George Rimar002655d2017-06-28 08:26:57 +0000830
Greg Clayton2520c9e2016-12-19 20:36:41 +0000831 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +0000832 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton2520c9e2016-12-19 20:36:41 +0000833 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +0000834 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
George Rimar002655d2017-06-28 08:26:57 +0000835
Greg Clayton2520c9e2016-12-19 20:36:41 +0000836 // Get the compile unit DIE is valid.
837 auto DieDG = U->getUnitDIE(false);
838 EXPECT_TRUE(DieDG.isValid());
George Rimara25d3292017-05-27 18:10:23 +0000839
840 uint64_t LowPC, HighPC, SectionIndex;
Greg Clayton2520c9e2016-12-19 20:36:41 +0000841 Optional<uint64_t> OptU64;
842 // Verify the that our subprogram with no PC value fails appropriately when
843 // asked for any PC values.
844 auto SubprogramDieNoPC = DieDG.getFirstChild();
845 EXPECT_TRUE(SubprogramDieNoPC.isValid());
846 EXPECT_EQ(SubprogramDieNoPC.getTag(), DW_TAG_subprogram);
Greg Clayton97d22182017-01-13 21:08:18 +0000847 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_low_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000848 EXPECT_FALSE((bool)OptU64);
Greg Clayton97d22182017-01-13 21:08:18 +0000849 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_high_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000850 EXPECT_FALSE((bool)OptU64);
George Rimara25d3292017-05-27 18:10:23 +0000851 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC, SectionIndex));
Greg Clayton97d22182017-01-13 21:08:18 +0000852 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_high_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000853 EXPECT_FALSE((bool)OptU64);
Greg Clayton97d22182017-01-13 21:08:18 +0000854 OptU64 = toUnsigned(SubprogramDieNoPC.find(DW_AT_high_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000855 EXPECT_FALSE((bool)OptU64);
856 OptU64 = SubprogramDieNoPC.getHighPC(ActualLowPC);
857 EXPECT_FALSE((bool)OptU64);
George Rimara25d3292017-05-27 18:10:23 +0000858 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC, SectionIndex));
George Rimar002655d2017-06-28 08:26:57 +0000859
Greg Clayton2520c9e2016-12-19 20:36:41 +0000860 // Verify the that our subprogram with only a low PC value succeeds when
861 // we ask for the Low PC, but fails appropriately when asked for the high PC
862 // or both low and high PC values.
863 auto SubprogramDieLowPC = SubprogramDieNoPC.getSibling();
864 EXPECT_TRUE(SubprogramDieLowPC.isValid());
865 EXPECT_EQ(SubprogramDieLowPC.getTag(), DW_TAG_subprogram);
Greg Clayton97d22182017-01-13 21:08:18 +0000866 OptU64 = toAddress(SubprogramDieLowPC.find(DW_AT_low_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000867 EXPECT_TRUE((bool)OptU64);
868 EXPECT_EQ(OptU64.getValue(), ActualLowPC);
Greg Clayton97d22182017-01-13 21:08:18 +0000869 OptU64 = toAddress(SubprogramDieLowPC.find(DW_AT_high_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000870 EXPECT_FALSE((bool)OptU64);
Greg Clayton97d22182017-01-13 21:08:18 +0000871 OptU64 = toUnsigned(SubprogramDieLowPC.find(DW_AT_high_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000872 EXPECT_FALSE((bool)OptU64);
873 OptU64 = SubprogramDieLowPC.getHighPC(ActualLowPC);
874 EXPECT_FALSE((bool)OptU64);
George Rimara25d3292017-05-27 18:10:23 +0000875 EXPECT_FALSE(SubprogramDieLowPC.getLowAndHighPC(LowPC, HighPC, SectionIndex));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000876
Greg Clayton2520c9e2016-12-19 20:36:41 +0000877 // Verify the that our subprogram with only a low PC value succeeds when
878 // we ask for the Low PC, but fails appropriately when asked for the high PC
879 // or both low and high PC values.
880 auto SubprogramDieLowHighPC = SubprogramDieLowPC.getSibling();
881 EXPECT_TRUE(SubprogramDieLowHighPC.isValid());
882 EXPECT_EQ(SubprogramDieLowHighPC.getTag(), DW_TAG_subprogram);
Greg Clayton97d22182017-01-13 21:08:18 +0000883 OptU64 = toAddress(SubprogramDieLowHighPC.find(DW_AT_low_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000884 EXPECT_TRUE((bool)OptU64);
885 EXPECT_EQ(OptU64.getValue(), ActualLowPC);
886 // Get the high PC as an address. This should succeed if the high PC was
887 // encoded as an address and fail if the high PC was encoded as an offset.
Greg Clayton97d22182017-01-13 21:08:18 +0000888 OptU64 = toAddress(SubprogramDieLowHighPC.find(DW_AT_high_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000889 if (SupportsHighPCAsOffset) {
890 EXPECT_FALSE((bool)OptU64);
891 } else {
892 EXPECT_TRUE((bool)OptU64);
893 EXPECT_EQ(OptU64.getValue(), ActualHighPC);
894 }
895 // Get the high PC as an unsigned constant. This should succeed if the high PC
896 // was encoded as an offset and fail if the high PC was encoded as an address.
Greg Clayton97d22182017-01-13 21:08:18 +0000897 OptU64 = toUnsigned(SubprogramDieLowHighPC.find(DW_AT_high_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000898 if (SupportsHighPCAsOffset) {
899 EXPECT_TRUE((bool)OptU64);
900 EXPECT_EQ(OptU64.getValue(), ActualHighPCOffset);
901 } else {
902 EXPECT_FALSE((bool)OptU64);
903 }
904
905 OptU64 = SubprogramDieLowHighPC.getHighPC(ActualLowPC);
906 EXPECT_TRUE((bool)OptU64);
907 EXPECT_EQ(OptU64.getValue(), ActualHighPC);
908
George Rimara25d3292017-05-27 18:10:23 +0000909 EXPECT_TRUE(SubprogramDieLowHighPC.getLowAndHighPC(LowPC, HighPC, SectionIndex));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000910 EXPECT_EQ(LowPC, ActualLowPC);
911 EXPECT_EQ(HighPC, ActualHighPC);
912}
913
914TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Addresses) {
915 // Test that we can decode address values in DWARF32, version 2, with 4 byte
916 // addresses.
917 typedef uint32_t AddrType;
918 TestAddresses<2, AddrType>();
919}
920
921TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Addresses) {
922 // Test that we can decode address values in DWARF32, version 2, with 8 byte
923 // addresses.
924 typedef uint64_t AddrType;
925 TestAddresses<2, AddrType>();
926}
927
928TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Addresses) {
929 // Test that we can decode address values in DWARF32, version 3, with 4 byte
930 // addresses.
931 typedef uint32_t AddrType;
932 TestAddresses<3, AddrType>();
933}
934
935TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Addresses) {
936 // Test that we can decode address values in DWARF32, version 3, with 8 byte
937 // addresses.
938 typedef uint64_t AddrType;
939 TestAddresses<3, AddrType>();
940}
941
942TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Addresses) {
943 // Test that we can decode address values in DWARF32, version 4, with 4 byte
944 // addresses.
945 typedef uint32_t AddrType;
946 TestAddresses<4, AddrType>();
947}
948
949TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Addresses) {
950 // Test that we can decode address values in DWARF32, version 4, with 8 byte
951 // addresses.
952 typedef uint64_t AddrType;
953 TestAddresses<4, AddrType>();
954}
955
Greg Clayton78a07bf2016-12-21 21:37:06 +0000956TEST(DWARFDebugInfo, TestRelations) {
George Rimard8508b02017-06-30 10:09:01 +0000957 Triple Triple = getHostTripleForAddrSize(sizeof(void *));
958 if (!isConfigurationSupported(Triple))
959 return;
960
Greg Clayton78a07bf2016-12-21 21:37:06 +0000961 // Test the DWARF APIs related to accessing the DW_AT_low_pc and
962 // DW_AT_high_pc.
963 uint16_t Version = 4;
Greg Clayton78a07bf2016-12-21 21:37:06 +0000964 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
George Rimard8508b02017-06-30 10:09:01 +0000965 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Clayton78a07bf2016-12-21 21:37:06 +0000966 dwarfgen::Generator *DG = ExpectedDG.get().get();
967 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
George Rimar002655d2017-06-28 08:26:57 +0000968
Greg Clayton78a07bf2016-12-21 21:37:06 +0000969 enum class Tag: uint16_t {
970 A = dwarf::DW_TAG_lo_user,
971 B,
Greg Clayton78a07bf2016-12-21 21:37:06 +0000972 C,
Greg Claytona9ef7ee2017-01-04 00:10:50 +0000973 C1,
974 C2,
975 D,
976 D1
Greg Clayton78a07bf2016-12-21 21:37:06 +0000977 };
978
979 // Scope to allow us to re-use the same DIE names
980 {
981 // Create DWARF tree that looks like:
982 //
983 // CU
984 // A
Greg Claytona9ef7ee2017-01-04 00:10:50 +0000985 // B
986 // C
987 // C1
988 // C2
989 // D
990 // D1
Greg Clayton78a07bf2016-12-21 21:37:06 +0000991 dwarfgen::DIE CUDie = CU.getUnitDIE();
Greg Claytona9ef7ee2017-01-04 00:10:50 +0000992 dwarfgen::DIE A = CUDie.addChild((dwarf::Tag)Tag::A);
993 A.addChild((dwarf::Tag)Tag::B);
994 dwarfgen::DIE C = A.addChild((dwarf::Tag)Tag::C);
995 dwarfgen::DIE D = A.addChild((dwarf::Tag)Tag::D);
Greg Clayton78a07bf2016-12-21 21:37:06 +0000996 C.addChild((dwarf::Tag)Tag::C1);
Greg Claytona9ef7ee2017-01-04 00:10:50 +0000997 C.addChild((dwarf::Tag)Tag::C2);
998 D.addChild((dwarf::Tag)Tag::D1);
Greg Clayton78a07bf2016-12-21 21:37:06 +0000999 }
1000
1001 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1002 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1003 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +00001004 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
George Rimar002655d2017-06-28 08:26:57 +00001005
Greg Clayton78a07bf2016-12-21 21:37:06 +00001006 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +00001007 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton78a07bf2016-12-21 21:37:06 +00001008 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +00001009 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
George Rimar002655d2017-06-28 08:26:57 +00001010
Greg Clayton78a07bf2016-12-21 21:37:06 +00001011 // Get the compile unit DIE is valid.
1012 auto CUDie = U->getUnitDIE(false);
1013 EXPECT_TRUE(CUDie.isValid());
George Rimar002655d2017-06-28 08:26:57 +00001014
Greg Clayton78a07bf2016-12-21 21:37:06 +00001015 // The compile unit doesn't have a parent or a sibling.
1016 auto ParentDie = CUDie.getParent();
1017 EXPECT_FALSE(ParentDie.isValid());
1018 auto SiblingDie = CUDie.getSibling();
1019 EXPECT_FALSE(SiblingDie.isValid());
George Rimar002655d2017-06-28 08:26:57 +00001020
Greg Clayton78a07bf2016-12-21 21:37:06 +00001021 // Get the children of the compile unit
1022 auto A = CUDie.getFirstChild();
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001023 auto B = A.getFirstChild();
Greg Clayton78a07bf2016-12-21 21:37:06 +00001024 auto C = B.getSibling();
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001025 auto D = C.getSibling();
1026 auto Null = D.getSibling();
George Rimar002655d2017-06-28 08:26:57 +00001027
Greg Clayton78a07bf2016-12-21 21:37:06 +00001028 // Verify NULL Die is NULL and has no children or siblings
1029 EXPECT_TRUE(Null.isNULL());
1030 EXPECT_FALSE(Null.getSibling().isValid());
1031 EXPECT_FALSE(Null.getFirstChild().isValid());
George Rimar002655d2017-06-28 08:26:57 +00001032
Greg Clayton78a07bf2016-12-21 21:37:06 +00001033 // Verify all children of the compile unit DIE are correct.
1034 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A);
1035 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B);
1036 EXPECT_EQ(C.getTag(), (dwarf::Tag)Tag::C);
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001037 EXPECT_EQ(D.getTag(), (dwarf::Tag)Tag::D);
Greg Clayton78a07bf2016-12-21 21:37:06 +00001038
1039 // Verify who has children
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001040 EXPECT_TRUE(A.hasChildren());
1041 EXPECT_FALSE(B.hasChildren());
1042 EXPECT_TRUE(C.hasChildren());
1043 EXPECT_TRUE(D.hasChildren());
Greg Clayton78a07bf2016-12-21 21:37:06 +00001044
1045 // Make sure the parent of all the children of the compile unit are the
1046 // compile unit.
1047 EXPECT_EQ(A.getParent(), CUDie);
George Rimar002655d2017-06-28 08:26:57 +00001048
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001049 // Make sure the parent of all the children of A are the A.
1050 // B is the first child in A, so we need to verify we can get the previous
1051 // DIE as the parent.
1052 EXPECT_EQ(B.getParent(), A);
1053 // C is the second child in A, so we need to make sure we can backup across
1054 // other DIE (B) at the same level to get the correct parent.
1055 EXPECT_EQ(C.getParent(), A);
1056 // D is the third child of A. We need to verify we can backup across other DIE
1057 // (B and C) including DIE that have children (D) to get the correct parent.
1058 EXPECT_EQ(D.getParent(), A);
Greg Clayton78a07bf2016-12-21 21:37:06 +00001059
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001060 // Verify that a DIE with no children returns an invalid DWARFDie.
1061 EXPECT_FALSE(B.getFirstChild().isValid());
Greg Clayton78a07bf2016-12-21 21:37:06 +00001062
1063 // Verify the children of the B DIE
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001064 auto C1 = C.getFirstChild();
1065 auto C2 = C1.getSibling();
1066 EXPECT_TRUE(C2.getSibling().isNULL());
George Rimar002655d2017-06-28 08:26:57 +00001067
Greg Clayton78a07bf2016-12-21 21:37:06 +00001068 // Verify all children of the B DIE correctly valid or invalid.
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001069 EXPECT_EQ(C1.getTag(), (dwarf::Tag)Tag::C1);
1070 EXPECT_EQ(C2.getTag(), (dwarf::Tag)Tag::C2);
Greg Clayton78a07bf2016-12-21 21:37:06 +00001071
1072 // Make sure the parent of all the children of the B are the B.
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001073 EXPECT_EQ(C1.getParent(), C);
1074 EXPECT_EQ(C2.getParent(), C);
Greg Clayton78a07bf2016-12-21 21:37:06 +00001075}
1076
1077TEST(DWARFDebugInfo, TestDWARFDie) {
Greg Clayton78a07bf2016-12-21 21:37:06 +00001078 // Make sure a default constructed DWARFDie doesn't have any parent, sibling
1079 // or child;
1080 DWARFDie DefaultDie;
1081 EXPECT_FALSE(DefaultDie.getParent().isValid());
1082 EXPECT_FALSE(DefaultDie.getFirstChild().isValid());
1083 EXPECT_FALSE(DefaultDie.getSibling().isValid());
1084}
1085
Greg Clayton93e4fe82017-01-05 23:47:37 +00001086TEST(DWARFDebugInfo, TestChildIterators) {
George Rimard8508b02017-06-30 10:09:01 +00001087 Triple Triple = getHostTripleForAddrSize(sizeof(void *));
1088 if (!isConfigurationSupported(Triple))
1089 return;
1090
Greg Clayton93e4fe82017-01-05 23:47:37 +00001091 // Test the DWARF APIs related to iterating across the children of a DIE using
1092 // the DWARFDie::iterator class.
1093 uint16_t Version = 4;
Greg Clayton93e4fe82017-01-05 23:47:37 +00001094 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
George Rimard8508b02017-06-30 10:09:01 +00001095 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Clayton93e4fe82017-01-05 23:47:37 +00001096 dwarfgen::Generator *DG = ExpectedDG.get().get();
1097 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
George Rimar002655d2017-06-28 08:26:57 +00001098
Greg Clayton93e4fe82017-01-05 23:47:37 +00001099 enum class Tag: uint16_t {
1100 A = dwarf::DW_TAG_lo_user,
1101 B,
1102 };
George Rimar002655d2017-06-28 08:26:57 +00001103
Greg Clayton93e4fe82017-01-05 23:47:37 +00001104 // Scope to allow us to re-use the same DIE names
1105 {
1106 // Create DWARF tree that looks like:
1107 //
1108 // CU
1109 // A
1110 // B
1111 auto CUDie = CU.getUnitDIE();
1112 CUDie.addChild((dwarf::Tag)Tag::A);
1113 CUDie.addChild((dwarf::Tag)Tag::B);
1114 }
George Rimar002655d2017-06-28 08:26:57 +00001115
Greg Clayton93e4fe82017-01-05 23:47:37 +00001116 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1117 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1118 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +00001119 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
George Rimar002655d2017-06-28 08:26:57 +00001120
Greg Clayton93e4fe82017-01-05 23:47:37 +00001121 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +00001122 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton93e4fe82017-01-05 23:47:37 +00001123 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +00001124 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
George Rimar002655d2017-06-28 08:26:57 +00001125
Greg Clayton93e4fe82017-01-05 23:47:37 +00001126 // Get the compile unit DIE is valid.
1127 auto CUDie = U->getUnitDIE(false);
1128 EXPECT_TRUE(CUDie.isValid());
Greg Clayton93e4fe82017-01-05 23:47:37 +00001129 uint32_t Index;
1130 DWARFDie A;
1131 DWARFDie B;
George Rimar002655d2017-06-28 08:26:57 +00001132
Greg Clayton93e4fe82017-01-05 23:47:37 +00001133 // Verify the compile unit DIE's children.
1134 Index = 0;
1135 for (auto Die : CUDie.children()) {
1136 switch (Index++) {
1137 case 0: A = Die; break;
1138 case 1: B = Die; break;
1139 }
1140 }
George Rimar002655d2017-06-28 08:26:57 +00001141
Greg Clayton93e4fe82017-01-05 23:47:37 +00001142 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A);
1143 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B);
1144
1145 // Verify that A has no children by verifying that the begin and end contain
1146 // invalid DIEs and also that the iterators are equal.
1147 EXPECT_EQ(A.begin(), A.end());
1148}
1149
1150TEST(DWARFDebugInfo, TestChildIteratorsOnInvalidDie) {
1151 // Verify that an invalid DIE has no children.
1152 DWARFDie Invalid;
1153 auto begin = Invalid.begin();
1154 auto end = Invalid.end();
1155 EXPECT_FALSE(begin->isValid());
1156 EXPECT_FALSE(end->isValid());
1157 EXPECT_EQ(begin, end);
1158}
1159
Greg Clayton93e4fe82017-01-05 23:47:37 +00001160TEST(DWARFDebugInfo, TestEmptyChildren) {
Chris Bieneman2e752db2017-01-20 19:03:14 +00001161 const char *yamldata = "debug_abbrev:\n"
1162 " - Code: 0x00000001\n"
1163 " Tag: DW_TAG_compile_unit\n"
1164 " Children: DW_CHILDREN_yes\n"
1165 " Attributes:\n"
1166 "debug_info:\n"
Chris Bienemanfaf1feb2017-03-03 21:11:55 +00001167 " - Length:\n"
Jonas Devlieghere5c709ed2018-04-20 12:33:49 +00001168 " TotalLength: 0\n"
Chris Bieneman2e752db2017-01-20 19:03:14 +00001169 " Version: 4\n"
1170 " AbbrOffset: 0\n"
1171 " AddrSize: 8\n"
1172 " Entries:\n"
1173 " - AbbrCode: 0x00000001\n"
1174 " Values:\n"
1175 " - AbbrCode: 0x00000000\n"
1176 " Values:\n";
1177
Jonas Devlieghere5c709ed2018-04-20 12:33:49 +00001178 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata), true);
Chris Bieneman977db092017-01-23 16:49:34 +00001179 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00001180 std::unique_ptr<DWARFContext> DwarfContext =
1181 DWARFContext::create(*ErrOrSections, 8);
Chris Bieneman2e752db2017-01-20 19:03:14 +00001182
Greg Clayton93e4fe82017-01-05 23:47:37 +00001183 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +00001184 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton93e4fe82017-01-05 23:47:37 +00001185 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +00001186 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
Chris Bieneman2e752db2017-01-20 19:03:14 +00001187
Greg Clayton93e4fe82017-01-05 23:47:37 +00001188 // Get the compile unit DIE is valid.
1189 auto CUDie = U->getUnitDIE(false);
1190 EXPECT_TRUE(CUDie.isValid());
George Rimar002655d2017-06-28 08:26:57 +00001191
Greg Clayton93e4fe82017-01-05 23:47:37 +00001192 // Verify that the CU Die that says it has children, but doesn't, actually
1193 // has begin and end iterators that are equal. We want to make sure we don't
1194 // see the Null DIEs during iteration.
1195 EXPECT_EQ(CUDie.begin(), CUDie.end());
1196}
Greg Clayton2520c9e2016-12-19 20:36:41 +00001197
Greg Clayton0e62ee72017-01-13 00:13:42 +00001198TEST(DWARFDebugInfo, TestAttributeIterators) {
George Rimard8508b02017-06-30 10:09:01 +00001199 Triple Triple = getHostTripleForAddrSize(sizeof(void *));
1200 if (!isConfigurationSupported(Triple))
1201 return;
1202
Greg Clayton0e62ee72017-01-13 00:13:42 +00001203 // Test the DWARF APIs related to iterating across all attribute values in a
1204 // a DWARFDie.
1205 uint16_t Version = 4;
Greg Clayton0e62ee72017-01-13 00:13:42 +00001206 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
George Rimard8508b02017-06-30 10:09:01 +00001207 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Clayton0e62ee72017-01-13 00:13:42 +00001208 dwarfgen::Generator *DG = ExpectedDG.get().get();
1209 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1210 const uint64_t CULowPC = 0x1000;
1211 StringRef CUPath("/tmp/main.c");
George Rimar002655d2017-06-28 08:26:57 +00001212
Greg Clayton0e62ee72017-01-13 00:13:42 +00001213 // Scope to allow us to re-use the same DIE names
1214 {
1215 auto CUDie = CU.getUnitDIE();
1216 // Encode an attribute value before an attribute with no data.
1217 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, CUPath.data());
1218 // Encode an attribute value with no data in .debug_info/types to ensure
1219 // the iteration works correctly.
1220 CUDie.addAttribute(DW_AT_declaration, DW_FORM_flag_present);
1221 // Encode an attribute value after an attribute with no data.
1222 CUDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, CULowPC);
1223 }
George Rimar002655d2017-06-28 08:26:57 +00001224
Greg Clayton0e62ee72017-01-13 00:13:42 +00001225 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1226 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1227 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +00001228 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
George Rimar002655d2017-06-28 08:26:57 +00001229
Greg Clayton0e62ee72017-01-13 00:13:42 +00001230 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +00001231 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton0e62ee72017-01-13 00:13:42 +00001232 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +00001233 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
George Rimar002655d2017-06-28 08:26:57 +00001234
Greg Clayton0e62ee72017-01-13 00:13:42 +00001235 // Get the compile unit DIE is valid.
1236 auto CUDie = U->getUnitDIE(false);
1237 EXPECT_TRUE(CUDie.isValid());
George Rimar002655d2017-06-28 08:26:57 +00001238
Greg Clayton0e62ee72017-01-13 00:13:42 +00001239 auto R = CUDie.attributes();
1240 auto I = R.begin();
1241 auto E = R.end();
George Rimar002655d2017-06-28 08:26:57 +00001242
Greg Clayton0e62ee72017-01-13 00:13:42 +00001243 ASSERT_NE(E, I);
1244 EXPECT_EQ(I->Attr, DW_AT_name);
1245 auto ActualCUPath = I->Value.getAsCString();
1246 EXPECT_EQ(CUPath, *ActualCUPath);
George Rimar002655d2017-06-28 08:26:57 +00001247
Greg Clayton0e62ee72017-01-13 00:13:42 +00001248 ASSERT_NE(E, ++I);
1249 EXPECT_EQ(I->Attr, DW_AT_declaration);
Greg Clayton71503432017-01-13 00:23:59 +00001250 EXPECT_EQ(1ull, *I->Value.getAsUnsignedConstant());
George Rimar002655d2017-06-28 08:26:57 +00001251
Greg Clayton0e62ee72017-01-13 00:13:42 +00001252 ASSERT_NE(E, ++I);
1253 EXPECT_EQ(I->Attr, DW_AT_low_pc);
Greg Clayton71503432017-01-13 00:23:59 +00001254 EXPECT_EQ(CULowPC, *I->Value.getAsAddress());
George Rimar002655d2017-06-28 08:26:57 +00001255
Greg Clayton0e62ee72017-01-13 00:13:42 +00001256 EXPECT_EQ(E, ++I);
1257}
1258
Greg Clayton97d22182017-01-13 21:08:18 +00001259TEST(DWARFDebugInfo, TestFindRecurse) {
George Rimard8508b02017-06-30 10:09:01 +00001260 Triple Triple = getHostTripleForAddrSize(sizeof(void *));
1261 if (!isConfigurationSupported(Triple))
Greg Clayton97d22182017-01-13 21:08:18 +00001262 return;
George Rimard8508b02017-06-30 10:09:01 +00001263
1264 uint16_t Version = 4;
1265 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1266 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Clayton97d22182017-01-13 21:08:18 +00001267 dwarfgen::Generator *DG = ExpectedDG.get().get();
1268 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
George Rimar002655d2017-06-28 08:26:57 +00001269
David Blaikie1914c822017-03-13 21:46:37 +00001270 StringRef SpecDieName = "spec";
1271 StringRef SpecLinkageName = "spec_linkage";
1272 StringRef AbsDieName = "abs";
Greg Clayton97d22182017-01-13 21:08:18 +00001273 // Scope to allow us to re-use the same DIE names
1274 {
Greg Clayton97d22182017-01-13 21:08:18 +00001275 auto CUDie = CU.getUnitDIE();
1276 auto FuncSpecDie = CUDie.addChild(DW_TAG_subprogram);
David Blaikie1914c822017-03-13 21:46:37 +00001277 auto FuncAbsDie = CUDie.addChild(DW_TAG_subprogram);
Greg Claytond6b67eb2017-11-27 22:12:44 +00001278 // Put the linkage name in a second abstract origin DIE to ensure we
1279 // recurse through more than just one DIE when looking for attributes.
1280 auto FuncAbsDie2 = CUDie.addChild(DW_TAG_subprogram);
Greg Clayton97d22182017-01-13 21:08:18 +00001281 auto FuncDie = CUDie.addChild(DW_TAG_subprogram);
1282 auto VarAbsDie = CUDie.addChild(DW_TAG_variable);
1283 auto VarDie = CUDie.addChild(DW_TAG_variable);
1284 FuncSpecDie.addAttribute(DW_AT_name, DW_FORM_strp, SpecDieName);
Greg Claytond6b67eb2017-11-27 22:12:44 +00001285 FuncAbsDie2.addAttribute(DW_AT_linkage_name, DW_FORM_strp, SpecLinkageName);
David Blaikie1914c822017-03-13 21:46:37 +00001286 FuncAbsDie.addAttribute(DW_AT_specification, DW_FORM_ref4, FuncSpecDie);
Greg Claytond6b67eb2017-11-27 22:12:44 +00001287 FuncAbsDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, FuncAbsDie2);
David Blaikie1914c822017-03-13 21:46:37 +00001288 FuncDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, FuncAbsDie);
Greg Clayton97d22182017-01-13 21:08:18 +00001289 VarAbsDie.addAttribute(DW_AT_name, DW_FORM_strp, AbsDieName);
1290 VarDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, VarAbsDie);
1291 }
George Rimar002655d2017-06-28 08:26:57 +00001292
Greg Clayton97d22182017-01-13 21:08:18 +00001293 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1294 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1295 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +00001296 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
George Rimar002655d2017-06-28 08:26:57 +00001297
Greg Clayton97d22182017-01-13 21:08:18 +00001298 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +00001299 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton97d22182017-01-13 21:08:18 +00001300 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +00001301 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
George Rimar002655d2017-06-28 08:26:57 +00001302
Greg Clayton97d22182017-01-13 21:08:18 +00001303 // Get the compile unit DIE is valid.
1304 auto CUDie = U->getUnitDIE(false);
1305 EXPECT_TRUE(CUDie.isValid());
George Rimar002655d2017-06-28 08:26:57 +00001306
Greg Clayton97d22182017-01-13 21:08:18 +00001307 auto FuncSpecDie = CUDie.getFirstChild();
David Blaikie1914c822017-03-13 21:46:37 +00001308 auto FuncAbsDie = FuncSpecDie.getSibling();
Greg Claytond6b67eb2017-11-27 22:12:44 +00001309 auto FuncAbsDie2 = FuncAbsDie.getSibling();
1310 auto FuncDie = FuncAbsDie2.getSibling();
Greg Clayton97d22182017-01-13 21:08:18 +00001311 auto VarAbsDie = FuncDie.getSibling();
1312 auto VarDie = VarAbsDie.getSibling();
1313
1314 // Make sure we can't extract the name from the specification die when using
1315 // DWARFDie::find() since it won't check the DW_AT_specification DIE.
David Blaikie1914c822017-03-13 21:46:37 +00001316 EXPECT_FALSE(FuncDie.find(DW_AT_name));
Greg Clayton97d22182017-01-13 21:08:18 +00001317
1318 // Make sure we can extract the name from the specification die when using
1319 // DWARFDie::findRecursively() since it should recurse through the
1320 // DW_AT_specification DIE.
1321 auto NameOpt = FuncDie.findRecursively(DW_AT_name);
David Blaikie1914c822017-03-13 21:46:37 +00001322 EXPECT_TRUE(NameOpt);
Greg Clayton97d22182017-01-13 21:08:18 +00001323 // Test the dwarf::toString() helper function.
1324 auto StringOpt = toString(NameOpt);
David Blaikie1914c822017-03-13 21:46:37 +00001325 EXPECT_TRUE(StringOpt);
Greg Clayton97d22182017-01-13 21:08:18 +00001326 EXPECT_EQ(SpecDieName, StringOpt.getValueOr(nullptr));
1327 // Test the dwarf::toString() helper function with a default value specified.
1328 EXPECT_EQ(SpecDieName, toString(NameOpt, nullptr));
David Blaikie1914c822017-03-13 21:46:37 +00001329
1330 auto LinkageNameOpt = FuncDie.findRecursively(DW_AT_linkage_name);
1331 EXPECT_EQ(SpecLinkageName, toString(LinkageNameOpt).getValueOr(nullptr));
George Rimar002655d2017-06-28 08:26:57 +00001332
Greg Clayton97d22182017-01-13 21:08:18 +00001333 // Make sure we can't extract the name from the abstract origin die when using
1334 // DWARFDie::find() since it won't check the DW_AT_abstract_origin DIE.
David Blaikie1914c822017-03-13 21:46:37 +00001335 EXPECT_FALSE(VarDie.find(DW_AT_name));
George Rimar002655d2017-06-28 08:26:57 +00001336
Greg Clayton97d22182017-01-13 21:08:18 +00001337 // Make sure we can extract the name from the abstract origin die when using
1338 // DWARFDie::findRecursively() since it should recurse through the
1339 // DW_AT_abstract_origin DIE.
1340 NameOpt = VarDie.findRecursively(DW_AT_name);
David Blaikie1914c822017-03-13 21:46:37 +00001341 EXPECT_TRUE(NameOpt);
Greg Clayton97d22182017-01-13 21:08:18 +00001342 // Test the dwarf::toString() helper function.
1343 StringOpt = toString(NameOpt);
David Blaikie1914c822017-03-13 21:46:37 +00001344 EXPECT_TRUE(StringOpt);
Greg Clayton97d22182017-01-13 21:08:18 +00001345 EXPECT_EQ(AbsDieName, StringOpt.getValueOr(nullptr));
Greg Clayton97d22182017-01-13 21:08:18 +00001346}
1347
1348TEST(DWARFDebugInfo, TestDwarfToFunctions) {
1349 // Test all of the dwarf::toXXX functions that take a
1350 // Optional<DWARFFormValue> and extract the values from it.
1351 DWARFFormValue FormVal;
1352 uint64_t InvalidU64 = 0xBADBADBADBADBADB;
1353 int64_t InvalidS64 = 0xBADBADBADBADBADB;
1354 // First test that we don't get valid values back when using an optional with
1355 // no value.
1356 Optional<DWARFFormValue> FormValOpt;
1357 EXPECT_FALSE(toString(FormValOpt).hasValue());
1358 EXPECT_FALSE(toUnsigned(FormValOpt).hasValue());
1359 EXPECT_FALSE(toReference(FormValOpt).hasValue());
1360 EXPECT_FALSE(toSigned(FormValOpt).hasValue());
1361 EXPECT_FALSE(toAddress(FormValOpt).hasValue());
1362 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue());
1363 EXPECT_FALSE(toBlock(FormValOpt).hasValue());
1364 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr));
1365 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt, InvalidU64));
1366 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64));
1367 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64));
1368 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64));
1369 EXPECT_EQ(InvalidS64, toSigned(FormValOpt, InvalidS64));
1370
Greg Clayton97d22182017-01-13 21:08:18 +00001371 // Test successful and unsuccessful address decoding.
1372 uint64_t Address = 0x100000000ULL;
1373 FormVal.setForm(DW_FORM_addr);
1374 FormVal.setUValue(Address);
1375 FormValOpt = FormVal;
1376
1377 EXPECT_FALSE(toString(FormValOpt).hasValue());
1378 EXPECT_FALSE(toUnsigned(FormValOpt).hasValue());
1379 EXPECT_FALSE(toReference(FormValOpt).hasValue());
1380 EXPECT_FALSE(toSigned(FormValOpt).hasValue());
1381 EXPECT_TRUE(toAddress(FormValOpt).hasValue());
1382 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue());
1383 EXPECT_FALSE(toBlock(FormValOpt).hasValue());
1384 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr));
1385 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt, InvalidU64));
1386 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64));
1387 EXPECT_EQ(Address, toAddress(FormValOpt, InvalidU64));
1388 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64));
1389 EXPECT_EQ(InvalidS64, toSigned(FormValOpt, InvalidU64));
1390
1391 // Test successful and unsuccessful unsigned constant decoding.
1392 uint64_t UData8 = 0x1020304050607080ULL;
1393 FormVal.setForm(DW_FORM_udata);
1394 FormVal.setUValue(UData8);
1395 FormValOpt = FormVal;
George Rimar002655d2017-06-28 08:26:57 +00001396
Greg Clayton97d22182017-01-13 21:08:18 +00001397 EXPECT_FALSE(toString(FormValOpt).hasValue());
1398 EXPECT_TRUE(toUnsigned(FormValOpt).hasValue());
1399 EXPECT_FALSE(toReference(FormValOpt).hasValue());
1400 EXPECT_TRUE(toSigned(FormValOpt).hasValue());
1401 EXPECT_FALSE(toAddress(FormValOpt).hasValue());
1402 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue());
1403 EXPECT_FALSE(toBlock(FormValOpt).hasValue());
1404 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr));
1405 EXPECT_EQ(UData8, toUnsigned(FormValOpt, InvalidU64));
1406 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64));
1407 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64));
1408 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64));
1409 EXPECT_EQ((int64_t)UData8, toSigned(FormValOpt, InvalidU64));
1410
1411 // Test successful and unsuccessful reference decoding.
1412 uint32_t RefData = 0x11223344U;
1413 FormVal.setForm(DW_FORM_ref_addr);
1414 FormVal.setUValue(RefData);
1415 FormValOpt = FormVal;
George Rimar002655d2017-06-28 08:26:57 +00001416
Greg Clayton97d22182017-01-13 21:08:18 +00001417 EXPECT_FALSE(toString(FormValOpt).hasValue());
1418 EXPECT_FALSE(toUnsigned(FormValOpt).hasValue());
1419 EXPECT_TRUE(toReference(FormValOpt).hasValue());
1420 EXPECT_FALSE(toSigned(FormValOpt).hasValue());
1421 EXPECT_FALSE(toAddress(FormValOpt).hasValue());
1422 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue());
1423 EXPECT_FALSE(toBlock(FormValOpt).hasValue());
1424 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr));
1425 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt, InvalidU64));
1426 EXPECT_EQ(RefData, toReference(FormValOpt, InvalidU64));
1427 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64));
1428 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64));
1429 EXPECT_EQ(InvalidS64, toSigned(FormValOpt, InvalidU64));
1430
1431 // Test successful and unsuccessful signed constant decoding.
1432 int64_t SData8 = 0x1020304050607080ULL;
1433 FormVal.setForm(DW_FORM_udata);
1434 FormVal.setSValue(SData8);
1435 FormValOpt = FormVal;
George Rimar002655d2017-06-28 08:26:57 +00001436
Greg Clayton97d22182017-01-13 21:08:18 +00001437 EXPECT_FALSE(toString(FormValOpt).hasValue());
1438 EXPECT_TRUE(toUnsigned(FormValOpt).hasValue());
1439 EXPECT_FALSE(toReference(FormValOpt).hasValue());
1440 EXPECT_TRUE(toSigned(FormValOpt).hasValue());
1441 EXPECT_FALSE(toAddress(FormValOpt).hasValue());
1442 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue());
1443 EXPECT_FALSE(toBlock(FormValOpt).hasValue());
1444 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr));
1445 EXPECT_EQ((uint64_t)SData8, toUnsigned(FormValOpt, InvalidU64));
1446 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64));
1447 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64));
1448 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64));
1449 EXPECT_EQ(SData8, toSigned(FormValOpt, InvalidU64));
1450
1451 // Test successful and unsuccessful block decoding.
1452 uint8_t Data[] = { 2, 3, 4 };
1453 ArrayRef<uint8_t> Array(Data);
1454 FormVal.setForm(DW_FORM_block1);
1455 FormVal.setBlockValue(Array);
1456 FormValOpt = FormVal;
George Rimar002655d2017-06-28 08:26:57 +00001457
Greg Clayton97d22182017-01-13 21:08:18 +00001458 EXPECT_FALSE(toString(FormValOpt).hasValue());
1459 EXPECT_FALSE(toUnsigned(FormValOpt).hasValue());
1460 EXPECT_FALSE(toReference(FormValOpt).hasValue());
1461 EXPECT_FALSE(toSigned(FormValOpt).hasValue());
1462 EXPECT_FALSE(toAddress(FormValOpt).hasValue());
1463 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue());
1464 auto BlockOpt = toBlock(FormValOpt);
1465 EXPECT_TRUE(BlockOpt.hasValue());
1466 EXPECT_EQ(*BlockOpt, Array);
1467 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr));
1468 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt, InvalidU64));
1469 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64));
1470 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64));
1471 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64));
1472 EXPECT_EQ(InvalidS64, toSigned(FormValOpt, InvalidU64));
1473
1474 // Test
1475}
1476
Greg Claytonc109bbe2017-01-13 22:32:12 +00001477TEST(DWARFDebugInfo, TestFindAttrs) {
George Rimard8508b02017-06-30 10:09:01 +00001478 Triple Triple = getHostTripleForAddrSize(sizeof(void *));
1479 if (!isConfigurationSupported(Triple))
1480 return;
1481
Greg Claytonc109bbe2017-01-13 22:32:12 +00001482 // Test the DWARFDie::find() and DWARFDie::findRecursively() that take an
1483 // ArrayRef<dwarf::Attribute> value to make sure they work correctly.
1484 uint16_t Version = 4;
Greg Claytonc109bbe2017-01-13 22:32:12 +00001485 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
George Rimard8508b02017-06-30 10:09:01 +00001486 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Claytonc109bbe2017-01-13 22:32:12 +00001487 dwarfgen::Generator *DG = ExpectedDG.get().get();
1488 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
George Rimar002655d2017-06-28 08:26:57 +00001489
Greg Claytonc109bbe2017-01-13 22:32:12 +00001490 StringRef DieMangled("_Z3fooi");
1491 // Scope to allow us to re-use the same DIE names
1492 {
Greg Claytonc109bbe2017-01-13 22:32:12 +00001493 auto CUDie = CU.getUnitDIE();
1494 auto FuncSpecDie = CUDie.addChild(DW_TAG_subprogram);
1495 auto FuncDie = CUDie.addChild(DW_TAG_subprogram);
1496 FuncSpecDie.addAttribute(DW_AT_MIPS_linkage_name, DW_FORM_strp, DieMangled);
1497 FuncDie.addAttribute(DW_AT_specification, DW_FORM_ref4, FuncSpecDie);
1498 }
George Rimar002655d2017-06-28 08:26:57 +00001499
Greg Claytonc109bbe2017-01-13 22:32:12 +00001500 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1501 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1502 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +00001503 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
George Rimar002655d2017-06-28 08:26:57 +00001504
Greg Claytonc109bbe2017-01-13 22:32:12 +00001505 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +00001506 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Claytonc109bbe2017-01-13 22:32:12 +00001507 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +00001508 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
George Rimar002655d2017-06-28 08:26:57 +00001509
Greg Claytonc109bbe2017-01-13 22:32:12 +00001510 // Get the compile unit DIE is valid.
1511 auto CUDie = U->getUnitDIE(false);
1512 EXPECT_TRUE(CUDie.isValid());
George Rimar002655d2017-06-28 08:26:57 +00001513
Greg Claytonc109bbe2017-01-13 22:32:12 +00001514 auto FuncSpecDie = CUDie.getFirstChild();
1515 auto FuncDie = FuncSpecDie.getSibling();
1516
1517 // Make sure that passing in an empty attribute list behave correctly.
1518 EXPECT_FALSE(FuncDie.find(ArrayRef<dwarf::Attribute>()).hasValue());
1519
1520 // Make sure that passing in a list of attribute that are not contained
1521 // in the DIE returns nothing.
1522 EXPECT_FALSE(FuncDie.find({DW_AT_low_pc, DW_AT_entry_pc}).hasValue());
1523
NAKAMURA Takumif2b135a2017-01-16 14:33:37 +00001524 const dwarf::Attribute Attrs[] = {DW_AT_linkage_name,
1525 DW_AT_MIPS_linkage_name};
1526
Greg Claytonc109bbe2017-01-13 22:32:12 +00001527 // Make sure we can't extract the linkage name attributes when using
1528 // DWARFDie::find() since it won't check the DW_AT_specification DIE.
1529 EXPECT_FALSE(FuncDie.find(Attrs).hasValue());
George Rimar002655d2017-06-28 08:26:57 +00001530
Greg Claytonc109bbe2017-01-13 22:32:12 +00001531 // Make sure we can extract the name from the specification die when using
1532 // DWARFDie::findRecursively() since it should recurse through the
1533 // DW_AT_specification DIE.
1534 auto NameOpt = FuncDie.findRecursively(Attrs);
1535 EXPECT_TRUE(NameOpt.hasValue());
1536 EXPECT_EQ(DieMangled, toString(NameOpt, ""));
Greg Claytonc109bbe2017-01-13 22:32:12 +00001537}
1538
Victor Leschukd7bfa402017-03-01 22:13:42 +00001539TEST(DWARFDebugInfo, TestImplicitConstAbbrevs) {
George Rimard8508b02017-06-30 10:09:01 +00001540 Triple Triple = getHostTripleForAddrSize(sizeof(void *));
1541 if (!isConfigurationSupported(Triple))
Victor Leschukd7bfa402017-03-01 22:13:42 +00001542 return;
George Rimard8508b02017-06-30 10:09:01 +00001543
1544 uint16_t Version = 5;
1545 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1546 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Victor Leschukd7bfa402017-03-01 22:13:42 +00001547 dwarfgen::Generator *DG = ExpectedDG.get().get();
1548 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1549 dwarfgen::DIE CUDie = CU.getUnitDIE();
1550 const dwarf::Attribute Attr = DW_AT_lo_user;
1551 const int64_t Val1 = 42;
1552 const int64_t Val2 = 43;
1553
1554 auto FirstVal1DIE = CUDie.addChild(DW_TAG_class_type);
1555 FirstVal1DIE.addAttribute(Attr, DW_FORM_implicit_const, Val1);
1556
1557 auto SecondVal1DIE = CUDie.addChild(DW_TAG_class_type);
1558 SecondVal1DIE.addAttribute(Attr, DW_FORM_implicit_const, Val1);
1559
1560 auto Val2DIE = CUDie.addChild(DW_TAG_class_type);
1561 Val2DIE.addAttribute(Attr, DW_FORM_implicit_const, Val2);
1562
1563 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1564 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1565 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +00001566 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
1567 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
Victor Leschukd7bfa402017-03-01 22:13:42 +00001568 EXPECT_TRUE((bool)U);
1569
1570 const auto *Abbrevs = U->getAbbreviations();
1571 EXPECT_TRUE((bool)Abbrevs);
1572
1573 // Let's find implicit_const abbrevs and verify,
1574 // that there are exactly two of them and both of them
1575 // can be dumped correctly.
1576 typedef decltype(Abbrevs->begin()) AbbrevIt;
1577 AbbrevIt Val1Abbrev = Abbrevs->end();
1578 AbbrevIt Val2Abbrev = Abbrevs->end();
1579 for(auto it = Abbrevs->begin(); it != Abbrevs->end(); ++it) {
1580 if (it->getNumAttributes() == 0)
1581 continue; // root abbrev for DW_TAG_compile_unit
1582
1583 auto A = it->getAttrByIndex(0);
1584 EXPECT_EQ(A, Attr);
1585
1586 auto FormValue = it->getAttributeValue(/* offset */ 0, A, *U);
1587 EXPECT_TRUE((bool)FormValue);
1588 EXPECT_EQ(FormValue->getForm(), dwarf::DW_FORM_implicit_const);
1589
1590 const auto V = FormValue->getAsSignedConstant();
1591 EXPECT_TRUE((bool)V);
1592
1593 auto VerifyAbbrevDump = [&V](AbbrevIt it) {
1594 std::string S;
1595 llvm::raw_string_ostream OS(S);
1596 it->dump(OS);
1597 auto FormPos = OS.str().find("DW_FORM_implicit_const");
1598 EXPECT_NE(FormPos, std::string::npos);
1599 auto ValPos = S.find_first_of("-0123456789", FormPos);
1600 EXPECT_NE(ValPos, std::string::npos);
1601 int64_t Val = std::atoll(S.substr(ValPos).c_str());
1602 EXPECT_EQ(Val, *V);
1603 };
1604
1605 switch(*V) {
1606 case Val1:
1607 EXPECT_EQ(Val1Abbrev, Abbrevs->end());
1608 Val1Abbrev = it;
1609 VerifyAbbrevDump(it);
1610 break;
1611 case Val2:
1612 EXPECT_EQ(Val2Abbrev, Abbrevs->end());
1613 Val2Abbrev = it;
1614 VerifyAbbrevDump(it);
1615 break;
1616 default:
1617 FAIL() << "Unexpected attribute value: " << *V;
1618 }
1619 }
1620
1621 // Now let's make sure that two Val1-DIEs refer to the same abbrev,
1622 // and Val2-DIE refers to another one.
1623 auto DieDG = U->getUnitDIE(false);
1624 auto it = DieDG.begin();
1625 std::multimap<int64_t, decltype(it->getAbbreviationDeclarationPtr())> DIEs;
1626 const DWARFAbbreviationDeclaration *AbbrevPtrVal1 = nullptr;
1627 const DWARFAbbreviationDeclaration *AbbrevPtrVal2 = nullptr;
1628 for (; it != DieDG.end(); ++it) {
1629 const auto *AbbrevPtr = it->getAbbreviationDeclarationPtr();
1630 EXPECT_TRUE((bool)AbbrevPtr);
1631 auto FormValue = it->find(Attr);
1632 EXPECT_TRUE((bool)FormValue);
1633 const auto V = FormValue->getAsSignedConstant();
1634 EXPECT_TRUE((bool)V);
1635 switch(*V) {
1636 case Val1:
1637 AbbrevPtrVal1 = AbbrevPtr;
1638 break;
1639 case Val2:
1640 AbbrevPtrVal2 = AbbrevPtr;
1641 break;
1642 default:
1643 FAIL() << "Unexpected attribute value: " << *V;
1644 }
1645 DIEs.insert(std::make_pair(*V, AbbrevPtr));
1646 }
1647 EXPECT_EQ(DIEs.count(Val1), 2u);
1648 EXPECT_EQ(DIEs.count(Val2), 1u);
1649 auto Val1Range = DIEs.equal_range(Val1);
1650 for (auto it = Val1Range.first; it != Val1Range.second; ++it)
1651 EXPECT_EQ(it->second, AbbrevPtrVal1);
1652 EXPECT_EQ(DIEs.find(Val2)->second, AbbrevPtrVal2);
1653}
1654
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +00001655void VerifyWarning(DWARFContext &DwarfContext, StringRef Error) {
1656 SmallString<1024> Str;
1657 raw_svector_ostream Strm(Str);
Jonas Devliegherec0a758d2017-09-18 14:15:57 +00001658 EXPECT_TRUE(DwarfContext.verify(Strm));
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +00001659 EXPECT_TRUE(Str.str().contains(Error));
1660}
1661
Greg Clayton67070462017-05-02 22:48:52 +00001662void VerifyError(DWARFContext &DwarfContext, StringRef Error) {
1663 SmallString<1024> Str;
1664 raw_svector_ostream Strm(Str);
Jonas Devliegherec0a758d2017-09-18 14:15:57 +00001665 EXPECT_FALSE(DwarfContext.verify(Strm));
Greg Clayton67070462017-05-02 22:48:52 +00001666 EXPECT_TRUE(Str.str().contains(Error));
1667}
1668
Jonas Devlieghere58910602017-09-14 11:33:42 +00001669void VerifySuccess(DWARFContext &DwarfContext) {
1670 SmallString<1024> Str;
1671 raw_svector_ostream Strm(Str);
Jonas Devliegherec0a758d2017-09-18 14:15:57 +00001672 EXPECT_TRUE(DwarfContext.verify(Strm));
Jonas Devlieghere58910602017-09-14 11:33:42 +00001673}
1674
Greg Clayton48432cf2017-05-01 22:07:02 +00001675TEST(DWARFDebugInfo, TestDwarfVerifyInvalidCURef) {
1676 // Create a single compile unit with a single function that has a DW_AT_type
Jonas Devlieghere58910602017-09-14 11:33:42 +00001677 // that is CU relative. The CU offset is not valid because it is larger than
Greg Clayton48432cf2017-05-01 22:07:02 +00001678 // the compile unit itself.
1679
1680 const char *yamldata = R"(
1681 debug_str:
1682 - ''
1683 - /tmp/main.c
1684 - main
1685 debug_abbrev:
1686 - Code: 0x00000001
1687 Tag: DW_TAG_compile_unit
1688 Children: DW_CHILDREN_yes
1689 Attributes:
1690 - Attribute: DW_AT_name
1691 Form: DW_FORM_strp
1692 - Code: 0x00000002
1693 Tag: DW_TAG_subprogram
1694 Children: DW_CHILDREN_no
1695 Attributes:
1696 - Attribute: DW_AT_name
1697 Form: DW_FORM_strp
1698 - Attribute: DW_AT_type
1699 Form: DW_FORM_ref4
1700 debug_info:
1701 - Length:
1702 TotalLength: 22
1703 Version: 4
1704 AbbrOffset: 0
1705 AddrSize: 8
1706 Entries:
1707 - AbbrCode: 0x00000001
1708 Values:
1709 - Value: 0x0000000000000001
1710 - AbbrCode: 0x00000002
1711 Values:
1712 - Value: 0x000000000000000D
1713 - Value: 0x0000000000001234
1714 - AbbrCode: 0x00000000
1715 Values:
1716 )";
1717 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
1718 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00001719 std::unique_ptr<DWARFContext> DwarfContext =
1720 DWARFContext::create(*ErrOrSections, 8);
1721 VerifyError(*DwarfContext, "error: DW_FORM_ref4 CU offset 0x00001234 is "
1722 "invalid (must be less than CU size of "
1723 "0x0000001a):");
Greg Clayton48432cf2017-05-01 22:07:02 +00001724}
1725
1726TEST(DWARFDebugInfo, TestDwarfVerifyInvalidRefAddr) {
1727 // Create a single compile unit with a single function that has an invalid
1728 // DW_AT_type with an invalid .debug_info offset in its DW_FORM_ref_addr.
1729 const char *yamldata = R"(
1730 debug_str:
1731 - ''
1732 - /tmp/main.c
1733 - main
1734 debug_abbrev:
1735 - Code: 0x00000001
1736 Tag: DW_TAG_compile_unit
1737 Children: DW_CHILDREN_yes
1738 Attributes:
1739 - Attribute: DW_AT_name
1740 Form: DW_FORM_strp
1741 - Code: 0x00000002
1742 Tag: DW_TAG_subprogram
1743 Children: DW_CHILDREN_no
1744 Attributes:
1745 - Attribute: DW_AT_name
1746 Form: DW_FORM_strp
1747 - Attribute: DW_AT_type
1748 Form: DW_FORM_ref_addr
1749 debug_info:
1750 - Length:
1751 TotalLength: 22
1752 Version: 4
1753 AbbrOffset: 0
1754 AddrSize: 8
1755 Entries:
1756 - AbbrCode: 0x00000001
1757 Values:
1758 - Value: 0x0000000000000001
1759 - AbbrCode: 0x00000002
1760 Values:
1761 - Value: 0x000000000000000D
1762 - Value: 0x0000000000001234
1763 - AbbrCode: 0x00000000
1764 Values:
1765 )";
1766 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
1767 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00001768 std::unique_ptr<DWARFContext> DwarfContext =
1769 DWARFContext::create(*ErrOrSections, 8);
1770 VerifyError(*DwarfContext,
Greg Clayton67070462017-05-02 22:48:52 +00001771 "error: DW_FORM_ref_addr offset beyond .debug_info bounds:");
Greg Clayton48432cf2017-05-01 22:07:02 +00001772}
1773
1774TEST(DWARFDebugInfo, TestDwarfVerifyInvalidRanges) {
1775 // Create a single compile unit with a DW_AT_ranges whose section offset
1776 // isn't valid.
1777 const char *yamldata = R"(
1778 debug_str:
1779 - ''
1780 - /tmp/main.c
1781 debug_abbrev:
1782 - Code: 0x00000001
1783 Tag: DW_TAG_compile_unit
1784 Children: DW_CHILDREN_no
1785 Attributes:
1786 - Attribute: DW_AT_name
1787 Form: DW_FORM_strp
1788 - Attribute: DW_AT_ranges
1789 Form: DW_FORM_sec_offset
1790 debug_info:
1791 - Length:
1792 TotalLength: 16
1793 Version: 4
1794 AbbrOffset: 0
1795 AddrSize: 8
1796 Entries:
1797 - AbbrCode: 0x00000001
1798 Values:
1799 - Value: 0x0000000000000001
1800 - Value: 0x0000000000001000
1801
1802 )";
1803 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
1804 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00001805 std::unique_ptr<DWARFContext> DwarfContext =
1806 DWARFContext::create(*ErrOrSections, 8);
1807 VerifyError(*DwarfContext,
Greg Clayton67070462017-05-02 22:48:52 +00001808 "error: DW_AT_ranges offset is beyond .debug_ranges bounds:");
Greg Clayton48432cf2017-05-01 22:07:02 +00001809}
1810
1811TEST(DWARFDebugInfo, TestDwarfVerifyInvalidStmtList) {
1812 // Create a single compile unit with a DW_AT_stmt_list whose section offset
1813 // isn't valid.
1814 const char *yamldata = R"(
1815 debug_str:
1816 - ''
1817 - /tmp/main.c
1818 debug_abbrev:
1819 - Code: 0x00000001
1820 Tag: DW_TAG_compile_unit
1821 Children: DW_CHILDREN_no
1822 Attributes:
1823 - Attribute: DW_AT_name
1824 Form: DW_FORM_strp
1825 - Attribute: DW_AT_stmt_list
1826 Form: DW_FORM_sec_offset
1827 debug_info:
1828 - Length:
1829 TotalLength: 16
1830 Version: 4
1831 AbbrOffset: 0
1832 AddrSize: 8
1833 Entries:
1834 - AbbrCode: 0x00000001
1835 Values:
1836 - Value: 0x0000000000000001
1837 - Value: 0x0000000000001000
1838
1839 )";
1840 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
1841 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00001842 std::unique_ptr<DWARFContext> DwarfContext =
1843 DWARFContext::create(*ErrOrSections, 8);
Greg Clayton67070462017-05-02 22:48:52 +00001844 VerifyError(
Rafael Espindolac398e672017-07-19 22:27:28 +00001845 *DwarfContext,
Greg Clayton67070462017-05-02 22:48:52 +00001846 "error: DW_AT_stmt_list offset is beyond .debug_line bounds: 0x00001000");
Greg Clayton48432cf2017-05-01 22:07:02 +00001847}
1848
1849TEST(DWARFDebugInfo, TestDwarfVerifyInvalidStrp) {
1850 // Create a single compile unit with a single function that has an invalid
1851 // DW_FORM_strp for the DW_AT_name.
1852 const char *yamldata = R"(
1853 debug_str:
1854 - ''
1855 debug_abbrev:
1856 - Code: 0x00000001
1857 Tag: DW_TAG_compile_unit
1858 Children: DW_CHILDREN_no
1859 Attributes:
1860 - Attribute: DW_AT_name
1861 Form: DW_FORM_strp
1862 debug_info:
1863 - Length:
1864 TotalLength: 12
1865 Version: 4
1866 AbbrOffset: 0
1867 AddrSize: 8
1868 Entries:
1869 - AbbrCode: 0x00000001
1870 Values:
1871 - Value: 0x0000000000001234
1872 )";
1873 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
1874 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00001875 std::unique_ptr<DWARFContext> DwarfContext =
1876 DWARFContext::create(*ErrOrSections, 8);
1877 VerifyError(*DwarfContext,
Greg Clayton67070462017-05-02 22:48:52 +00001878 "error: DW_FORM_strp offset beyond .debug_str bounds:");
Greg Clayton48432cf2017-05-01 22:07:02 +00001879}
1880
Greg Claytonc7695a82017-05-02 20:28:33 +00001881TEST(DWARFDebugInfo, TestDwarfVerifyInvalidRefAddrBetween) {
1882 // Create a single compile unit with a single function that has a DW_AT_type
1883 // with a valid .debug_info offset, but the offset is between two DIEs.
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_ref_addr
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: 0x0000000000000011
1918 - AbbrCode: 0x00000000
1919 Values:
1920 )";
1921 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
1922 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00001923 std::unique_ptr<DWARFContext> DwarfContext =
1924 DWARFContext::create(*ErrOrSections, 8);
Greg Clayton67070462017-05-02 22:48:52 +00001925 VerifyError(
Rafael Espindolac398e672017-07-19 22:27:28 +00001926 *DwarfContext,
Greg Clayton67070462017-05-02 22:48:52 +00001927 "error: invalid DIE reference 0x00000011. Offset is in between DIEs:");
Greg Claytonc7695a82017-05-02 20:28:33 +00001928}
Greg Clayton67070462017-05-02 22:48:52 +00001929
1930TEST(DWARFDebugInfo, TestDwarfVerifyInvalidLineSequence) {
1931 // Create a single compile unit whose line table has a sequence in it where
1932 // the address decreases.
1933 StringRef yamldata = R"(
1934 debug_str:
1935 - ''
1936 - /tmp/main.c
1937 debug_abbrev:
1938 - Code: 0x00000001
1939 Tag: DW_TAG_compile_unit
1940 Children: DW_CHILDREN_no
1941 Attributes:
1942 - Attribute: DW_AT_name
1943 Form: DW_FORM_strp
1944 - Attribute: DW_AT_stmt_list
1945 Form: DW_FORM_sec_offset
1946 debug_info:
1947 - Length:
1948 TotalLength: 16
1949 Version: 4
1950 AbbrOffset: 0
1951 AddrSize: 8
1952 Entries:
1953 - AbbrCode: 0x00000001
1954 Values:
1955 - Value: 0x0000000000000001
1956 - Value: 0x0000000000000000
1957 debug_line:
1958 - Length:
1959 TotalLength: 68
1960 Version: 2
1961 PrologueLength: 34
1962 MinInstLength: 1
1963 DefaultIsStmt: 1
1964 LineBase: 251
1965 LineRange: 14
1966 OpcodeBase: 13
1967 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
1968 IncludeDirs:
1969 - /tmp
1970 Files:
1971 - Name: main.c
1972 DirIdx: 1
1973 ModTime: 0
1974 Length: 0
1975 Opcodes:
1976 - Opcode: DW_LNS_extended_op
1977 ExtLen: 9
1978 SubOpcode: DW_LNE_set_address
1979 Data: 4112
1980 - Opcode: DW_LNS_advance_line
1981 SData: 9
1982 Data: 4112
1983 - Opcode: DW_LNS_copy
1984 Data: 4112
1985 - Opcode: DW_LNS_advance_pc
1986 Data: 18446744073709551600
1987 - Opcode: DW_LNS_extended_op
1988 ExtLen: 1
1989 SubOpcode: DW_LNE_end_sequence
1990 Data: 18446744073709551600
1991 )";
1992 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
1993 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00001994 std::unique_ptr<DWARFContext> DwarfContext =
1995 DWARFContext::create(*ErrOrSections, 8);
1996 VerifyError(*DwarfContext, "error: .debug_line[0x00000000] row[1] decreases "
1997 "in address from previous row:");
Greg Clayton67070462017-05-02 22:48:52 +00001998}
1999
2000TEST(DWARFDebugInfo, TestDwarfVerifyInvalidLineFileIndex) {
2001 // Create a single compile unit whose line table has a line table row with
2002 // an invalid file index.
2003 StringRef yamldata = R"(
2004 debug_str:
2005 - ''
2006 - /tmp/main.c
2007 debug_abbrev:
2008 - Code: 0x00000001
2009 Tag: DW_TAG_compile_unit
2010 Children: DW_CHILDREN_no
2011 Attributes:
2012 - Attribute: DW_AT_name
2013 Form: DW_FORM_strp
2014 - Attribute: DW_AT_stmt_list
2015 Form: DW_FORM_sec_offset
2016 debug_info:
2017 - Length:
2018 TotalLength: 16
2019 Version: 4
2020 AbbrOffset: 0
2021 AddrSize: 8
2022 Entries:
2023 - AbbrCode: 0x00000001
2024 Values:
2025 - Value: 0x0000000000000001
2026 - Value: 0x0000000000000000
2027 debug_line:
2028 - Length:
2029 TotalLength: 61
2030 Version: 2
2031 PrologueLength: 34
2032 MinInstLength: 1
2033 DefaultIsStmt: 1
2034 LineBase: 251
2035 LineRange: 14
2036 OpcodeBase: 13
2037 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
2038 IncludeDirs:
2039 - /tmp
2040 Files:
2041 - Name: main.c
2042 DirIdx: 1
2043 ModTime: 0
2044 Length: 0
2045 Opcodes:
2046 - Opcode: DW_LNS_extended_op
2047 ExtLen: 9
2048 SubOpcode: DW_LNE_set_address
2049 Data: 4096
2050 - Opcode: DW_LNS_advance_line
2051 SData: 9
2052 Data: 4096
2053 - Opcode: DW_LNS_copy
2054 Data: 4096
2055 - Opcode: DW_LNS_advance_pc
2056 Data: 16
2057 - Opcode: DW_LNS_set_file
2058 Data: 5
2059 - Opcode: DW_LNS_extended_op
2060 ExtLen: 1
2061 SubOpcode: DW_LNE_end_sequence
2062 Data: 5
2063 )";
2064 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2065 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00002066 std::unique_ptr<DWARFContext> DwarfContext =
2067 DWARFContext::create(*ErrOrSections, 8);
2068 VerifyError(*DwarfContext, "error: .debug_line[0x00000000][1] has invalid "
2069 "file index 5 (valid values are [1,1]):");
Greg Clayton67070462017-05-02 22:48:52 +00002070}
2071
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +00002072TEST(DWARFDebugInfo, TestDwarfVerifyInvalidLineTablePorlogueDirIndex) {
2073 // Create a single compile unit whose line table has a prologue with an
2074 // invalid dir index.
2075 StringRef yamldata = R"(
2076 debug_str:
2077 - ''
2078 - /tmp/main.c
2079 debug_abbrev:
2080 - Code: 0x00000001
2081 Tag: DW_TAG_compile_unit
2082 Children: DW_CHILDREN_no
2083 Attributes:
2084 - Attribute: DW_AT_name
2085 Form: DW_FORM_strp
2086 - Attribute: DW_AT_stmt_list
2087 Form: DW_FORM_sec_offset
2088 debug_info:
2089 - Length:
2090 TotalLength: 16
2091 Version: 4
2092 AbbrOffset: 0
2093 AddrSize: 8
2094 Entries:
2095 - AbbrCode: 0x00000001
2096 Values:
2097 - Value: 0x0000000000000001
2098 - Value: 0x0000000000000000
2099 debug_line:
2100 - Length:
2101 TotalLength: 61
2102 Version: 2
2103 PrologueLength: 34
2104 MinInstLength: 1
2105 DefaultIsStmt: 1
2106 LineBase: 251
2107 LineRange: 14
2108 OpcodeBase: 13
2109 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
2110 IncludeDirs:
2111 - /tmp
2112 Files:
2113 - Name: main.c
2114 DirIdx: 2
2115 ModTime: 0
2116 Length: 0
2117 Opcodes:
2118 - Opcode: DW_LNS_extended_op
2119 ExtLen: 9
2120 SubOpcode: DW_LNE_set_address
2121 Data: 4096
2122 - Opcode: DW_LNS_advance_line
2123 SData: 9
2124 Data: 4096
2125 - Opcode: DW_LNS_copy
2126 Data: 4096
2127 - Opcode: DW_LNS_advance_pc
2128 Data: 16
2129 - Opcode: DW_LNS_set_file
2130 Data: 1
2131 - Opcode: DW_LNS_extended_op
2132 ExtLen: 1
2133 SubOpcode: DW_LNE_end_sequence
2134 Data: 1
2135 )";
2136 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2137 ASSERT_TRUE((bool)ErrOrSections);
2138 std::unique_ptr<DWARFContext> DwarfContext =
2139 DWARFContext::create(*ErrOrSections, 8);
2140 VerifyError(*DwarfContext,
2141 "error: .debug_line[0x00000000].prologue."
2142 "file_names[1].dir_idx contains an invalid index: 2");
2143}
2144
2145TEST(DWARFDebugInfo, TestDwarfVerifyDuplicateFileWarning) {
2146 // Create a single compile unit whose line table has a prologue with an
2147 // invalid dir index.
2148 StringRef yamldata = R"(
2149 debug_str:
2150 - ''
2151 - /tmp/main.c
2152 debug_abbrev:
2153 - Code: 0x00000001
2154 Tag: DW_TAG_compile_unit
2155 Children: DW_CHILDREN_no
2156 Attributes:
2157 - Attribute: DW_AT_name
2158 Form: DW_FORM_strp
2159 - Attribute: DW_AT_stmt_list
2160 Form: DW_FORM_sec_offset
2161 debug_info:
2162 - Length:
2163 TotalLength: 16
2164 Version: 4
2165 AbbrOffset: 0
2166 AddrSize: 8
2167 Entries:
2168 - AbbrCode: 0x00000001
2169 Values:
2170 - Value: 0x0000000000000001
2171 - Value: 0x0000000000000000
2172 debug_line:
2173 - Length:
2174 TotalLength: 71
2175 Version: 2
2176 PrologueLength: 44
2177 MinInstLength: 1
2178 DefaultIsStmt: 1
2179 LineBase: 251
2180 LineRange: 14
2181 OpcodeBase: 13
2182 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
2183 IncludeDirs:
2184 - /tmp
2185 Files:
2186 - Name: main.c
2187 DirIdx: 1
2188 ModTime: 0
2189 Length: 0
2190 - Name: main.c
2191 DirIdx: 1
2192 ModTime: 0
2193 Length: 0
2194 Opcodes:
2195 - Opcode: DW_LNS_extended_op
2196 ExtLen: 9
2197 SubOpcode: DW_LNE_set_address
2198 Data: 4096
2199 - Opcode: DW_LNS_advance_line
2200 SData: 9
2201 Data: 4096
2202 - Opcode: DW_LNS_copy
2203 Data: 4096
2204 - Opcode: DW_LNS_advance_pc
2205 Data: 16
2206 - Opcode: DW_LNS_set_file
2207 Data: 1
2208 - Opcode: DW_LNS_extended_op
2209 ExtLen: 1
2210 SubOpcode: DW_LNE_end_sequence
2211 Data: 2
2212 )";
2213 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2214 ASSERT_TRUE((bool)ErrOrSections);
2215 std::unique_ptr<DWARFContext> DwarfContext =
2216 DWARFContext::create(*ErrOrSections, 8);
2217 VerifyWarning(*DwarfContext,
2218 "warning: .debug_line[0x00000000].prologue.file_names[2] is "
2219 "a duplicate of file_names[1]");
2220}
2221
Greg Clayton8df55b42017-05-03 15:45:31 +00002222TEST(DWARFDebugInfo, TestDwarfVerifyCUDontShareLineTable) {
2223 // Create a two compile units where both compile units share the same
2224 // DW_AT_stmt_list value and verify we report the error correctly.
2225 StringRef yamldata = R"(
2226 debug_str:
2227 - ''
2228 - /tmp/main.c
2229 - /tmp/foo.c
George Rimar002655d2017-06-28 08:26:57 +00002230 debug_abbrev:
Greg Clayton8df55b42017-05-03 15:45:31 +00002231 - Code: 0x00000001
2232 Tag: DW_TAG_compile_unit
2233 Children: DW_CHILDREN_no
George Rimar002655d2017-06-28 08:26:57 +00002234 Attributes:
Greg Clayton8df55b42017-05-03 15:45:31 +00002235 - Attribute: DW_AT_name
2236 Form: DW_FORM_strp
2237 - Attribute: DW_AT_stmt_list
2238 Form: DW_FORM_sec_offset
George Rimar002655d2017-06-28 08:26:57 +00002239 debug_info:
2240 - Length:
Greg Clayton8df55b42017-05-03 15:45:31 +00002241 TotalLength: 16
2242 Version: 4
2243 AbbrOffset: 0
2244 AddrSize: 8
George Rimar002655d2017-06-28 08:26:57 +00002245 Entries:
Greg Clayton8df55b42017-05-03 15:45:31 +00002246 - AbbrCode: 0x00000001
George Rimar002655d2017-06-28 08:26:57 +00002247 Values:
Greg Clayton8df55b42017-05-03 15:45:31 +00002248 - Value: 0x0000000000000001
2249 - Value: 0x0000000000000000
George Rimar002655d2017-06-28 08:26:57 +00002250 - Length:
Greg Clayton8df55b42017-05-03 15:45:31 +00002251 TotalLength: 16
2252 Version: 4
2253 AbbrOffset: 0
2254 AddrSize: 8
George Rimar002655d2017-06-28 08:26:57 +00002255 Entries:
Greg Clayton8df55b42017-05-03 15:45:31 +00002256 - AbbrCode: 0x00000001
George Rimar002655d2017-06-28 08:26:57 +00002257 Values:
Greg Clayton8df55b42017-05-03 15:45:31 +00002258 - Value: 0x000000000000000D
2259 - Value: 0x0000000000000000
George Rimar002655d2017-06-28 08:26:57 +00002260 debug_line:
2261 - Length:
Greg Clayton8df55b42017-05-03 15:45:31 +00002262 TotalLength: 60
2263 Version: 2
2264 PrologueLength: 34
2265 MinInstLength: 1
2266 DefaultIsStmt: 1
2267 LineBase: 251
2268 LineRange: 14
2269 OpcodeBase: 13
2270 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
George Rimar002655d2017-06-28 08:26:57 +00002271 IncludeDirs:
Greg Clayton8df55b42017-05-03 15:45:31 +00002272 - /tmp
George Rimar002655d2017-06-28 08:26:57 +00002273 Files:
Greg Clayton8df55b42017-05-03 15:45:31 +00002274 - Name: main.c
2275 DirIdx: 1
2276 ModTime: 0
2277 Length: 0
George Rimar002655d2017-06-28 08:26:57 +00002278 Opcodes:
Greg Clayton8df55b42017-05-03 15:45:31 +00002279 - Opcode: DW_LNS_extended_op
2280 ExtLen: 9
2281 SubOpcode: DW_LNE_set_address
2282 Data: 4096
2283 - Opcode: DW_LNS_advance_line
2284 SData: 9
2285 Data: 4096
2286 - Opcode: DW_LNS_copy
2287 Data: 4096
2288 - Opcode: DW_LNS_advance_pc
2289 Data: 256
2290 - Opcode: DW_LNS_extended_op
2291 ExtLen: 1
2292 SubOpcode: DW_LNE_end_sequence
2293 Data: 256
2294 )";
2295 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2296 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00002297 std::unique_ptr<DWARFContext> DwarfContext =
2298 DWARFContext::create(*ErrOrSections, 8);
2299 VerifyError(*DwarfContext,
2300 "error: two compile unit DIEs, 0x0000000b and "
2301 "0x0000001f, have the same DW_AT_stmt_list section "
2302 "offset:");
Greg Clayton8df55b42017-05-03 15:45:31 +00002303}
2304
George Rimar1af3cb22017-06-28 08:21:19 +00002305TEST(DWARFDebugInfo, TestErrorReportingPolicy) {
George Rimard8508b02017-06-30 10:09:01 +00002306 Triple Triple("x86_64-pc-linux");
2307 if (!isConfigurationSupported(Triple))
2308 return;
2309
2310 auto ExpectedDG = dwarfgen::Generator::create(Triple, 4 /*DwarfVersion*/);
2311 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
George Rimar1af3cb22017-06-28 08:21:19 +00002312 dwarfgen::Generator *DG = ExpectedDG.get().get();
2313 AsmPrinter *AP = DG->getAsmPrinter();
2314 MCContext *MC = DG->getMCContext();
2315
2316 // Emit two compressed sections with broken headers.
2317 AP->OutStreamer->SwitchSection(
2318 MC->getELFSection(".zdebug_foo", 0 /*Type*/, 0 /*Flags*/));
2319 AP->OutStreamer->EmitBytes("0");
2320 AP->OutStreamer->SwitchSection(
2321 MC->getELFSection(".zdebug_bar", 0 /*Type*/, 0 /*Flags*/));
2322 AP->OutStreamer->EmitBytes("0");
2323
2324 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
2325 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
2326 EXPECT_TRUE((bool)Obj);
2327
2328 // Case 1: error handler handles all errors. That allows
Rafael Espindola3ee9e112017-07-19 23:34:59 +00002329 // DWARFContext to parse whole file and find both two errors we know about.
George Rimar1af3cb22017-06-28 08:21:19 +00002330 int Errors = 0;
Rafael Espindolac398e672017-07-19 22:27:28 +00002331 std::unique_ptr<DWARFContext> Ctx1 =
2332 DWARFContext::create(**Obj, nullptr, [&](Error E) {
2333 ++Errors;
2334 consumeError(std::move(E));
2335 return ErrorPolicy::Continue;
2336 });
George Rimar1af3cb22017-06-28 08:21:19 +00002337 EXPECT_TRUE(Errors == 2);
2338
2339 // Case 2: error handler stops parsing of object after first error.
2340 Errors = 0;
Rafael Espindolac398e672017-07-19 22:27:28 +00002341 std::unique_ptr<DWARFContext> Ctx2 =
2342 DWARFContext::create(**Obj, nullptr, [&](Error E) {
2343 ++Errors;
2344 consumeError(std::move(E));
2345 return ErrorPolicy::Halt;
2346 });
George Rimar1af3cb22017-06-28 08:21:19 +00002347 EXPECT_TRUE(Errors == 1);
2348}
2349
Jonas Devlieghere58910602017-09-14 11:33:42 +00002350TEST(DWARFDebugInfo, TestDwarfVerifyCURangesIncomplete) {
2351 // Create a single compile unit with a single function. The compile
2352 // unit has a DW_AT_ranges attribute that doesn't fully contain the
2353 // address range of the function. The verification should fail due to
2354 // the CU ranges not containing all of the address ranges of all of the
2355 // functions.
2356 StringRef yamldata = R"(
2357 debug_str:
2358 - ''
2359 - /tmp/main.c
2360 debug_abbrev:
2361 - Code: 0x00000001
2362 Tag: DW_TAG_compile_unit
2363 Children: DW_CHILDREN_yes
2364 Attributes:
2365 - Attribute: DW_AT_low_pc
2366 Form: DW_FORM_addr
2367 - Attribute: DW_AT_high_pc
2368 Form: DW_FORM_addr
2369 - Attribute: DW_AT_name
2370 Form: DW_FORM_strp
2371 - Code: 0x00000002
2372 Tag: DW_TAG_subprogram
2373 Children: DW_CHILDREN_no
2374 Attributes:
2375 - Attribute: DW_AT_low_pc
2376 Form: DW_FORM_addr
2377 - Attribute: DW_AT_high_pc
2378 Form: DW_FORM_addr
2379 debug_info:
2380 - Length:
2381 TotalLength: 46
2382 Version: 4
2383 AbbrOffset: 0
2384 AddrSize: 8
2385 Entries:
2386 - AbbrCode: 0x00000001
2387 Values:
2388 - Value: 0x0000000000001000
2389 - Value: 0x0000000000001500
2390 - Value: 0x0000000000000001
2391 - AbbrCode: 0x00000002
2392 Values:
2393 - Value: 0x0000000000001000
2394 - Value: 0x0000000000002000
2395 - AbbrCode: 0x00000000
2396 Values:
2397 )";
2398 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2399 ASSERT_TRUE((bool)ErrOrSections);
2400 std::unique_ptr<DWARFContext> DwarfContext =
2401 DWARFContext::create(*ErrOrSections, 8);
2402 VerifyError(*DwarfContext, "error: DIE address ranges are not "
2403 "contained in its parent's ranges:");
2404}
2405
2406TEST(DWARFDebugInfo, TestDwarfVerifyLexicalBlockRanges) {
2407 // Create a single compile unit with a single function that has a lexical
2408 // block whose address range is not contained in the function address range.
2409 StringRef yamldata = R"(
2410 debug_str:
2411 - ''
2412 - /tmp/main.c
2413 - main
2414 debug_abbrev:
2415 - Code: 0x00000001
2416 Tag: DW_TAG_compile_unit
2417 Children: DW_CHILDREN_yes
2418 Attributes:
2419 - Attribute: DW_AT_name
2420 Form: DW_FORM_strp
2421 - Code: 0x00000002
2422 Tag: DW_TAG_subprogram
2423 Children: DW_CHILDREN_yes
2424 Attributes:
2425 - Attribute: DW_AT_name
2426 Form: DW_FORM_strp
2427 - Attribute: DW_AT_low_pc
2428 Form: DW_FORM_addr
2429 - Attribute: DW_AT_high_pc
2430 Form: DW_FORM_addr
2431 - Code: 0x00000003
2432 Tag: DW_TAG_lexical_block
2433 Children: DW_CHILDREN_no
2434 Attributes:
2435 - Attribute: DW_AT_low_pc
2436 Form: DW_FORM_addr
2437 - Attribute: DW_AT_high_pc
2438 Form: DW_FORM_addr
2439 debug_info:
2440 - Length:
2441 TotalLength: 52
2442 Version: 4
2443 AbbrOffset: 0
2444 AddrSize: 8
2445 Entries:
2446 - AbbrCode: 0x00000001
2447 Values:
2448 - Value: 0x0000000000000001
2449 - AbbrCode: 0x00000002
2450 Values:
2451 - Value: 0x000000000000000D
2452 - Value: 0x0000000000001000
2453 - Value: 0x0000000000002000
2454 - AbbrCode: 0x00000003
2455 Values:
2456 - Value: 0x0000000000001000
2457 - Value: 0x0000000000002001
2458 - AbbrCode: 0x00000000
2459 Values:
2460 - AbbrCode: 0x00000000
2461 Values:
2462 )";
2463 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2464 ASSERT_TRUE((bool)ErrOrSections);
2465 std::unique_ptr<DWARFContext> DwarfContext =
2466 DWARFContext::create(*ErrOrSections, 8);
2467 VerifyError(*DwarfContext, "error: DIE address ranges are not "
2468 "contained in its parent's ranges:");
2469}
2470
2471TEST(DWARFDebugInfo, TestDwarfVerifyOverlappingFunctionRanges) {
2472 // Create a single compile unit with a two functions that have overlapping
2473 // address ranges.
2474 StringRef yamldata = R"(
2475 debug_str:
2476 - ''
2477 - /tmp/main.c
2478 - main
2479 - foo
2480 debug_abbrev:
2481 - Code: 0x00000001
2482 Tag: DW_TAG_compile_unit
2483 Children: DW_CHILDREN_yes
2484 Attributes:
2485 - Attribute: DW_AT_name
2486 Form: DW_FORM_strp
2487 - Code: 0x00000002
2488 Tag: DW_TAG_subprogram
2489 Children: DW_CHILDREN_no
2490 Attributes:
2491 - Attribute: DW_AT_name
2492 Form: DW_FORM_strp
2493 - Attribute: DW_AT_low_pc
2494 Form: DW_FORM_addr
2495 - Attribute: DW_AT_high_pc
2496 Form: DW_FORM_addr
2497 debug_info:
2498 - Length:
2499 TotalLength: 55
2500 Version: 4
2501 AbbrOffset: 0
2502 AddrSize: 8
2503 Entries:
2504 - AbbrCode: 0x00000001
2505 Values:
2506 - Value: 0x0000000000000001
2507 - AbbrCode: 0x00000002
2508 Values:
2509 - Value: 0x000000000000000D
2510 - Value: 0x0000000000001000
2511 - Value: 0x0000000000002000
2512 - AbbrCode: 0x00000002
2513 Values:
2514 - Value: 0x0000000000000012
2515 - Value: 0x0000000000001FFF
2516 - Value: 0x0000000000002000
2517 - AbbrCode: 0x00000000
2518 Values:
2519 )";
2520 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2521 ASSERT_TRUE((bool)ErrOrSections);
2522 std::unique_ptr<DWARFContext> DwarfContext =
2523 DWARFContext::create(*ErrOrSections, 8);
2524 VerifyError(*DwarfContext, "error: DIEs have overlapping address ranges:");
2525}
2526
2527TEST(DWARFDebugInfo, TestDwarfVerifyOverlappingLexicalBlockRanges) {
2528 // Create a single compile unit with a one function that has two lexical
2529 // blocks with overlapping address ranges.
2530 StringRef yamldata = R"(
2531 debug_str:
2532 - ''
2533 - /tmp/main.c
2534 - main
2535 debug_abbrev:
2536 - Code: 0x00000001
2537 Tag: DW_TAG_compile_unit
2538 Children: DW_CHILDREN_yes
2539 Attributes:
2540 - Attribute: DW_AT_low_pc
2541 Form: DW_FORM_addr
2542 - Attribute: DW_AT_high_pc
2543 Form: DW_FORM_addr
2544 - Attribute: DW_AT_name
2545 Form: DW_FORM_strp
2546 - Code: 0x00000002
2547 Tag: DW_TAG_subprogram
2548 Children: DW_CHILDREN_yes
2549 Attributes:
2550 - Attribute: DW_AT_name
2551 Form: DW_FORM_strp
2552 - Attribute: DW_AT_low_pc
2553 Form: DW_FORM_addr
2554 - Attribute: DW_AT_high_pc
2555 Form: DW_FORM_addr
2556 - Code: 0x00000003
2557 Tag: DW_TAG_lexical_block
2558 Children: DW_CHILDREN_no
2559 Attributes:
2560 - Attribute: DW_AT_low_pc
2561 Form: DW_FORM_addr
2562 - Attribute: DW_AT_high_pc
2563 Form: DW_FORM_addr
2564 debug_info:
2565 - Length:
2566 TotalLength: 85
2567 Version: 4
2568 AbbrOffset: 0
2569 AddrSize: 8
2570 Entries:
2571 - AbbrCode: 0x00000001
2572 Values:
2573 - Value: 0x0000000000001000
2574 - Value: 0x0000000000002000
2575 - Value: 0x0000000000000001
2576 - AbbrCode: 0x00000002
2577 Values:
2578 - Value: 0x000000000000000D
2579 - Value: 0x0000000000001000
2580 - Value: 0x0000000000002000
2581 - AbbrCode: 0x00000003
2582 Values:
2583 - Value: 0x0000000000001100
2584 - Value: 0x0000000000001300
2585 - AbbrCode: 0x00000003
2586 Values:
2587 - Value: 0x00000000000012FF
2588 - Value: 0x0000000000001300
2589 - AbbrCode: 0x00000000
2590 Values:
2591 - AbbrCode: 0x00000000
2592 Values:
2593 )";
2594 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2595 ASSERT_TRUE((bool)ErrOrSections);
2596 std::unique_ptr<DWARFContext> DwarfContext =
2597 DWARFContext::create(*ErrOrSections, 8);
2598 VerifyError(*DwarfContext, "error: DIEs have overlapping address ranges:");
2599}
2600
2601TEST(DWARFDebugInfo, TestDwarfVerifyInvalidDIERange) {
2602 // Create a single compile unit with a single function that has an invalid
2603 // address range where the high PC is smaller than the low PC.
2604 StringRef yamldata = R"(
2605 debug_str:
2606 - ''
2607 - /tmp/main.c
2608 - main
2609 debug_abbrev:
2610 - Code: 0x00000001
2611 Tag: DW_TAG_compile_unit
2612 Children: DW_CHILDREN_yes
2613 Attributes:
2614 - Attribute: DW_AT_name
2615 Form: DW_FORM_strp
2616 - Code: 0x00000002
2617 Tag: DW_TAG_subprogram
2618 Children: DW_CHILDREN_no
2619 Attributes:
2620 - Attribute: DW_AT_name
2621 Form: DW_FORM_strp
2622 - Attribute: DW_AT_low_pc
2623 Form: DW_FORM_addr
2624 - Attribute: DW_AT_high_pc
2625 Form: DW_FORM_addr
2626 debug_info:
2627 - Length:
2628 TotalLength: 34
2629 Version: 4
2630 AbbrOffset: 0
2631 AddrSize: 8
2632 Entries:
2633 - AbbrCode: 0x00000001
2634 Values:
2635 - Value: 0x0000000000000001
2636 - AbbrCode: 0x00000002
2637 Values:
2638 - Value: 0x000000000000000D
2639 - Value: 0x0000000000001000
2640 - Value: 0x0000000000000900
2641 - AbbrCode: 0x00000000
2642 Values:
2643 )";
2644 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2645 ASSERT_TRUE((bool)ErrOrSections);
2646 std::unique_ptr<DWARFContext> DwarfContext =
2647 DWARFContext::create(*ErrOrSections, 8);
2648 VerifyError(*DwarfContext, "error: Invalid address range");
2649}
2650
2651TEST(DWARFDebugInfo, TestDwarfVerifyElidedDoesntFail) {
2652 // Create a single compile unit with two functions: one that has a valid range
2653 // and one whose low and high PC are the same. When the low and high PC are
2654 // the same, this indicates the function was dead code stripped. We want to
2655 // ensure that verification succeeds.
2656 StringRef yamldata = R"(
2657 debug_str:
2658 - ''
2659 - /tmp/main.c
2660 - main
2661 - elided
2662 debug_abbrev:
2663 - Code: 0x00000001
2664 Tag: DW_TAG_compile_unit
2665 Children: DW_CHILDREN_yes
2666 Attributes:
2667 - Attribute: DW_AT_low_pc
2668 Form: DW_FORM_addr
2669 - Attribute: DW_AT_high_pc
2670 Form: DW_FORM_addr
2671 - Attribute: DW_AT_name
2672 Form: DW_FORM_strp
2673 - Code: 0x00000002
2674 Tag: DW_TAG_subprogram
2675 Children: DW_CHILDREN_no
2676 Attributes:
2677 - Attribute: DW_AT_name
2678 Form: DW_FORM_strp
2679 - Attribute: DW_AT_low_pc
2680 Form: DW_FORM_addr
2681 - Attribute: DW_AT_high_pc
2682 Form: DW_FORM_addr
2683 debug_info:
2684 - Length:
2685 TotalLength: 71
2686 Version: 4
2687 AbbrOffset: 0
2688 AddrSize: 8
2689 Entries:
2690 - AbbrCode: 0x00000001
2691 Values:
2692 - Value: 0x0000000000001000
2693 - Value: 0x0000000000002000
2694 - Value: 0x0000000000000001
2695 - AbbrCode: 0x00000002
2696 Values:
2697 - Value: 0x000000000000000D
2698 - Value: 0x0000000000001000
2699 - Value: 0x0000000000002000
2700 - AbbrCode: 0x00000002
2701 Values:
2702 - Value: 0x0000000000000012
2703 - Value: 0x0000000000002000
2704 - Value: 0x0000000000002000
2705 - AbbrCode: 0x00000000
2706 Values:
2707 )";
2708 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2709 ASSERT_TRUE((bool)ErrOrSections);
2710 std::unique_ptr<DWARFContext> DwarfContext =
2711 DWARFContext::create(*ErrOrSections, 8);
2712 VerifySuccess(*DwarfContext);
2713}
2714
2715TEST(DWARFDebugInfo, TestDwarfVerifyNestedFunctions) {
2716 // Create a single compile unit with a nested function which is not contained
2717 // in its parent. Although LLVM doesn't generate this, it is valid accoridng
2718 // to the DWARF standard.
2719 StringRef yamldata = R"(
2720 debug_str:
2721 - ''
2722 - /tmp/main.c
2723 - main
2724 - nested
2725 debug_abbrev:
2726 - Code: 0x00000001
2727 Tag: DW_TAG_compile_unit
2728 Children: DW_CHILDREN_yes
2729 Attributes:
2730 - Attribute: DW_AT_low_pc
2731 Form: DW_FORM_addr
2732 - Attribute: DW_AT_high_pc
2733 Form: DW_FORM_addr
2734 - Attribute: DW_AT_name
2735 Form: DW_FORM_strp
2736 - Code: 0x00000002
2737 Tag: DW_TAG_subprogram
2738 Children: DW_CHILDREN_yes
2739 Attributes:
2740 - Attribute: DW_AT_name
2741 Form: DW_FORM_strp
2742 - Attribute: DW_AT_low_pc
2743 Form: DW_FORM_addr
2744 - Attribute: DW_AT_high_pc
2745 Form: DW_FORM_addr
2746 debug_info:
2747 - Length:
2748 TotalLength: 73
2749 Version: 4
2750 AbbrOffset: 0
2751 AddrSize: 8
2752 Entries:
2753 - AbbrCode: 0x00000001
2754 Values:
2755 - Value: 0x0000000000001000
2756 - Value: 0x0000000000002000
2757 - Value: 0x0000000000000001
2758 - AbbrCode: 0x00000002
2759 Values:
2760 - Value: 0x000000000000000D
2761 - Value: 0x0000000000001000
2762 - Value: 0x0000000000001500
2763 - AbbrCode: 0x00000002
2764 Values:
2765 - Value: 0x0000000000000012
2766 - Value: 0x0000000000001500
2767 - Value: 0x0000000000002000
2768 - AbbrCode: 0x00000000
2769 Values:
2770 - AbbrCode: 0x00000000
2771 Values:
2772 - AbbrCode: 0x00000000
2773 Values:
2774 )";
2775 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2776 ASSERT_TRUE((bool)ErrOrSections);
2777 std::unique_ptr<DWARFContext> DwarfContext =
2778 DWARFContext::create(*ErrOrSections, 8);
2779 VerifySuccess(*DwarfContext);
2780}
2781
2782TEST(DWARFDebugInfo, TestDwarfRangesContains) {
2783 DWARFAddressRange R(0x10, 0x20);
2784
2785 //----------------------------------------------------------------------
2786 // Test ranges that start before R...
2787 //----------------------------------------------------------------------
2788 // Other range ends before start of R
2789 ASSERT_FALSE(R.contains({0x0f, 0x10}));
2790 // Other range end address is start of a R
2791 ASSERT_FALSE(R.contains({0x0f, 0x11}));
2792 // Other range end address is at and of R
2793 ASSERT_FALSE(R.contains({0x0f, 0x20}));
2794 // Other range end address is past end of R
2795 ASSERT_FALSE(R.contains({0x0f, 0x40}));
2796
2797 //----------------------------------------------------------------------
2798 // Test ranges that start at R's start address
2799 //----------------------------------------------------------------------
2800 // Ensure empty ranges matches
2801 ASSERT_TRUE(R.contains({0x10, 0x10}));
2802 // 1 byte of Range
2803 ASSERT_TRUE(R.contains({0x10, 0x11}));
2804 // same as Range
2805 ASSERT_TRUE(R.contains({0x10, 0x20}));
2806 // 1 byte past Range
2807 ASSERT_FALSE(R.contains({0x10, 0x21}));
2808
2809 //----------------------------------------------------------------------
2810 // Test ranges that start inside Range
2811 //----------------------------------------------------------------------
2812 // empty in range
2813 ASSERT_TRUE(R.contains({0x11, 0x11}));
2814 // all in Range
2815 ASSERT_TRUE(R.contains({0x11, 0x1f}));
2816 // ends at end of Range
2817 ASSERT_TRUE(R.contains({0x11, 0x20}));
2818 // ends past Range
2819 ASSERT_FALSE(R.contains({0x11, 0x21}));
2820
2821 //----------------------------------------------------------------------
2822 // Test ranges that start at last bytes of Range
2823 //----------------------------------------------------------------------
2824 // ends at end of Range
2825 ASSERT_TRUE(R.contains({0x1f, 0x20}));
2826 // ends past Range
2827 ASSERT_FALSE(R.contains({0x1f, 0x21}));
2828
2829 //----------------------------------------------------------------------
2830 // Test ranges that start after Range
2831 //----------------------------------------------------------------------
2832 // empty considered in Range
2833 ASSERT_TRUE(R.contains({0x20, 0x20}));
2834 // valid past Range
2835 ASSERT_FALSE(R.contains({0x20, 0x21}));
2836}
2837
2838TEST(DWARFDebugInfo, TestDWARFDieRangeInfoContains) {
2839 DWARFVerifier::DieRangeInfo Ranges({{0x10, 0x20}, {0x30, 0x40}});
2840
2841 ASSERT_FALSE(Ranges.contains({{{0x0f, 0x10}}}));
2842 ASSERT_FALSE(Ranges.contains({{{0x20, 0x30}}}));
2843 ASSERT_FALSE(Ranges.contains({{{0x40, 0x41}}}));
2844 ASSERT_TRUE(Ranges.contains({{{0x10, 0x20}}}));
2845 ASSERT_TRUE(Ranges.contains({{{0x11, 0x12}}}));
2846 ASSERT_TRUE(Ranges.contains({{{0x1f, 0x20}}}));
2847 ASSERT_TRUE(Ranges.contains({{{0x30, 0x40}}}));
2848 ASSERT_TRUE(Ranges.contains({{{0x31, 0x32}}}));
2849 ASSERT_TRUE(Ranges.contains({{{0x3f, 0x40}}}));
2850 ASSERT_TRUE(Ranges.contains({{{0x10, 0x20}, {0x30, 0x40}}}));
2851 ASSERT_TRUE(Ranges.contains({{{0x11, 0x12}, {0x31, 0x32}}}));
2852 ASSERT_TRUE(Ranges.contains(
2853 {{{0x11, 0x12}, {0x12, 0x13}, {0x31, 0x32}, {0x32, 0x33}}}));
2854 ASSERT_FALSE(Ranges.contains({{{0x11, 0x12},
2855 {0x12, 0x13},
2856 {0x20, 0x21},
2857 {0x31, 0x32},
2858 {0x32, 0x33}}}));
2859 ASSERT_FALSE(Ranges.contains(
2860 {{{0x11, 0x12}, {0x12, 0x13}, {0x31, 0x32}, {0x32, 0x41}}}));
2861}
2862
2863namespace {
2864
2865void AssertRangesIntersect(const DWARFAddressRange &LHS,
2866 const DWARFAddressRange &RHS) {
2867 ASSERT_TRUE(LHS.intersects(RHS));
2868 ASSERT_TRUE(RHS.intersects(LHS));
2869}
2870void AssertRangesDontIntersect(const DWARFAddressRange &LHS,
2871 const DWARFAddressRange &RHS) {
2872 ASSERT_FALSE(LHS.intersects(RHS));
2873 ASSERT_FALSE(RHS.intersects(LHS));
2874}
2875
2876void AssertRangesIntersect(const DWARFVerifier::DieRangeInfo &LHS,
2877 const DWARFAddressRangesVector &Ranges) {
2878 DWARFVerifier::DieRangeInfo RHS(Ranges);
2879 ASSERT_TRUE(LHS.intersects(RHS));
2880 ASSERT_TRUE(RHS.intersects(LHS));
2881}
2882
2883void AssertRangesDontIntersect(const DWARFVerifier::DieRangeInfo &LHS,
2884 const DWARFAddressRangesVector &Ranges) {
2885 DWARFVerifier::DieRangeInfo RHS(Ranges);
2886 ASSERT_FALSE(LHS.intersects(RHS));
2887 ASSERT_FALSE(RHS.intersects(LHS));
2888}
2889
2890} // namespace
2891TEST(DWARFDebugInfo, TestDwarfRangesIntersect) {
2892 DWARFAddressRange R(0x10, 0x20);
2893
2894 //----------------------------------------------------------------------
2895 // Test ranges that start before R...
2896 //----------------------------------------------------------------------
2897 // Other range ends before start of R
2898 AssertRangesDontIntersect(R, {0x00, 0x10});
2899 // Other range end address is start of a R
2900 AssertRangesIntersect(R, {0x00, 0x11});
2901 // Other range end address is in R
2902 AssertRangesIntersect(R, {0x00, 0x15});
2903 // Other range end address is at and of R
2904 AssertRangesIntersect(R, {0x00, 0x20});
2905 // Other range end address is past end of R
2906 AssertRangesIntersect(R, {0x00, 0x40});
2907
2908 //----------------------------------------------------------------------
2909 // Test ranges that start at R's start address
2910 //----------------------------------------------------------------------
2911 // Ensure empty ranges doesn't match
2912 AssertRangesDontIntersect(R, {0x10, 0x10});
2913 // 1 byte of Range
2914 AssertRangesIntersect(R, {0x10, 0x11});
2915 // same as Range
2916 AssertRangesIntersect(R, {0x10, 0x20});
2917 // 1 byte past Range
2918 AssertRangesIntersect(R, {0x10, 0x21});
2919
2920 //----------------------------------------------------------------------
2921 // Test ranges that start inside Range
2922 //----------------------------------------------------------------------
2923 // empty in range
2924 AssertRangesDontIntersect(R, {0x11, 0x11});
2925 // all in Range
2926 AssertRangesIntersect(R, {0x11, 0x1f});
2927 // ends at end of Range
2928 AssertRangesIntersect(R, {0x11, 0x20});
2929 // ends past Range
2930 AssertRangesIntersect(R, {0x11, 0x21});
2931
2932 //----------------------------------------------------------------------
2933 // Test ranges that start at last bytes of Range
2934 //----------------------------------------------------------------------
2935 // ends at end of Range
2936 AssertRangesIntersect(R, {0x1f, 0x20});
2937 // ends past Range
2938 AssertRangesIntersect(R, {0x1f, 0x21});
2939
2940 //----------------------------------------------------------------------
2941 // Test ranges that start after Range
2942 //----------------------------------------------------------------------
2943 // empty just past in Range
2944 AssertRangesDontIntersect(R, {0x20, 0x20});
2945 // valid past Range
2946 AssertRangesDontIntersect(R, {0x20, 0x21});
2947}
2948
2949TEST(DWARFDebugInfo, TestDWARFDieRangeInfoIntersects) {
2950
2951 DWARFVerifier::DieRangeInfo Ranges({{0x10, 0x20}, {0x30, 0x40}});
2952
2953 // Test empty range
2954 AssertRangesDontIntersect(Ranges, {});
2955 // Test range that appears before all ranges in Ranges
2956 AssertRangesDontIntersect(Ranges, {{0x00, 0x10}});
2957 // Test range that appears between ranges in Ranges
2958 AssertRangesDontIntersect(Ranges, {{0x20, 0x30}});
2959 // Test range that appears after ranges in Ranges
2960 AssertRangesDontIntersect(Ranges, {{0x40, 0x50}});
2961
2962 // Test range that start before first range
2963 AssertRangesIntersect(Ranges, {{0x00, 0x11}});
2964 // Test range that start at first range
2965 AssertRangesIntersect(Ranges, {{0x10, 0x11}});
2966 // Test range that start in first range
2967 AssertRangesIntersect(Ranges, {{0x11, 0x12}});
2968 // Test range that start at end of first range
2969 AssertRangesIntersect(Ranges, {{0x1f, 0x20}});
2970 // Test range that starts at end of first range
2971 AssertRangesDontIntersect(Ranges, {{0x20, 0x21}});
2972 // Test range that starts at end of first range
2973 AssertRangesIntersect(Ranges, {{0x20, 0x31}});
2974
2975 // Test range that start before second range and ends before second
2976 AssertRangesDontIntersect(Ranges, {{0x2f, 0x30}});
2977 // Test range that start before second range and ends in second
2978 AssertRangesIntersect(Ranges, {{0x2f, 0x31}});
2979 // Test range that start at second range
2980 AssertRangesIntersect(Ranges, {{0x30, 0x31}});
2981 // Test range that start in second range
2982 AssertRangesIntersect(Ranges, {{0x31, 0x32}});
2983 // Test range that start at end of second range
2984 AssertRangesIntersect(Ranges, {{0x3f, 0x40}});
2985 // Test range that starts at end of second range
2986 AssertRangesDontIntersect(Ranges, {{0x40, 0x41}});
2987}
2988
Greg Clayton3462a422016-12-08 01:03:48 +00002989} // end anonymous namespace