blob: 60058be2e81c0fbf9468125b27893bf124811a59 [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";
Pavel Labath78ab6592018-07-25 15:33:32 +000070 const char *StrxValue = "Indexed";
71 const char *Strx1Value = "Indexed1";
72 const char *Strx2Value = "Indexed2";
73 const char *Strx3Value = "Indexed3";
74 const char *Strx4Value = "Indexed4";
George Rimard8508b02017-06-30 10:09:01 +000075
Greg Clayton3462a422016-12-08 01:03:48 +000076 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
George Rimard8508b02017-06-30 10:09:01 +000077 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Clayton3462a422016-12-08 01:03:48 +000078 dwarfgen::Generator *DG = ExpectedDG.get().get();
79 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
80 dwarfgen::DIE CUDie = CU.getUnitDIE();
Pavel Labath78ab6592018-07-25 15:33:32 +000081
82 if (Version >= 5)
83 CUDie.addAttribute(dwarf::DW_AT_str_offsets_base, dwarf::DW_FORM_sec_offset,
84 *MCSymbolRefExpr::create(DG->getStringOffsetsStartSym(),
85 *DG->getMCContext()));
86
Greg Clayton3462a422016-12-08 01:03:48 +000087 uint16_t Attr = DW_AT_lo_user;
88
89 //----------------------------------------------------------------------
90 // Test address forms
91 //----------------------------------------------------------------------
92 const auto Attr_DW_FORM_addr = static_cast<dwarf::Attribute>(Attr++);
93 CUDie.addAttribute(Attr_DW_FORM_addr, DW_FORM_addr, AddrValue);
94
95 //----------------------------------------------------------------------
96 // Test block forms
97 //----------------------------------------------------------------------
98 const auto Attr_DW_FORM_block = static_cast<dwarf::Attribute>(Attr++);
99 CUDie.addAttribute(Attr_DW_FORM_block, DW_FORM_block, BlockData, BlockSize);
100
101 const auto Attr_DW_FORM_block1 = static_cast<dwarf::Attribute>(Attr++);
102 CUDie.addAttribute(Attr_DW_FORM_block1, DW_FORM_block1, BlockData, BlockSize);
103
104 const auto Attr_DW_FORM_block2 = static_cast<dwarf::Attribute>(Attr++);
105 CUDie.addAttribute(Attr_DW_FORM_block2, DW_FORM_block2, BlockData, BlockSize);
106
107 const auto Attr_DW_FORM_block4 = static_cast<dwarf::Attribute>(Attr++);
108 CUDie.addAttribute(Attr_DW_FORM_block4, DW_FORM_block4, BlockData, BlockSize);
109
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000110 // We handle data16 as a block form.
111 const auto Attr_DW_FORM_data16 = static_cast<dwarf::Attribute>(Attr++);
112 if (Version >= 5)
113 CUDie.addAttribute(Attr_DW_FORM_data16, DW_FORM_data16, Data16, 16);
114
Greg Clayton3462a422016-12-08 01:03:48 +0000115 //----------------------------------------------------------------------
116 // Test data forms
117 //----------------------------------------------------------------------
118 const auto Attr_DW_FORM_data1 = static_cast<dwarf::Attribute>(Attr++);
119 CUDie.addAttribute(Attr_DW_FORM_data1, DW_FORM_data1, Data1);
120
121 const auto Attr_DW_FORM_data2 = static_cast<dwarf::Attribute>(Attr++);
122 CUDie.addAttribute(Attr_DW_FORM_data2, DW_FORM_data2, Data2);
123
124 const auto Attr_DW_FORM_data4 = static_cast<dwarf::Attribute>(Attr++);
125 CUDie.addAttribute(Attr_DW_FORM_data4, DW_FORM_data4, Data4);
126
127 const auto Attr_DW_FORM_data8 = static_cast<dwarf::Attribute>(Attr++);
128 CUDie.addAttribute(Attr_DW_FORM_data8, DW_FORM_data8, Data8);
129
130 //----------------------------------------------------------------------
131 // Test string forms
132 //----------------------------------------------------------------------
133 const auto Attr_DW_FORM_string = static_cast<dwarf::Attribute>(Attr++);
134 CUDie.addAttribute(Attr_DW_FORM_string, DW_FORM_string, StringValue);
135
Pavel Labath78ab6592018-07-25 15:33:32 +0000136 const auto Attr_DW_FORM_strx = static_cast<dwarf::Attribute>(Attr++);
137 const auto Attr_DW_FORM_strx1 = static_cast<dwarf::Attribute>(Attr++);
138 const auto Attr_DW_FORM_strx2 = static_cast<dwarf::Attribute>(Attr++);
139 const auto Attr_DW_FORM_strx3 = static_cast<dwarf::Attribute>(Attr++);
140 const auto Attr_DW_FORM_strx4 = static_cast<dwarf::Attribute>(Attr++);
141 if (Version >= 5) {
142 CUDie.addAttribute(Attr_DW_FORM_strx, DW_FORM_strx, StrxValue);
143 CUDie.addAttribute(Attr_DW_FORM_strx1, DW_FORM_strx1, Strx1Value);
144 CUDie.addAttribute(Attr_DW_FORM_strx2, DW_FORM_strx2, Strx2Value);
145 CUDie.addAttribute(Attr_DW_FORM_strx3, DW_FORM_strx3, Strx3Value);
146 CUDie.addAttribute(Attr_DW_FORM_strx4, DW_FORM_strx4, Strx4Value);
147 }
148
Greg Clayton3462a422016-12-08 01:03:48 +0000149 const auto Attr_DW_FORM_strp = static_cast<dwarf::Attribute>(Attr++);
150 CUDie.addAttribute(Attr_DW_FORM_strp, DW_FORM_strp, StrpValue);
151
152 //----------------------------------------------------------------------
153 // Test reference forms
154 //----------------------------------------------------------------------
155 const auto Attr_DW_FORM_ref_addr = static_cast<dwarf::Attribute>(Attr++);
156 CUDie.addAttribute(Attr_DW_FORM_ref_addr, DW_FORM_ref_addr, RefAddr);
157
158 const auto Attr_DW_FORM_ref1 = static_cast<dwarf::Attribute>(Attr++);
159 CUDie.addAttribute(Attr_DW_FORM_ref1, DW_FORM_ref1, Data1);
160
161 const auto Attr_DW_FORM_ref2 = static_cast<dwarf::Attribute>(Attr++);
162 CUDie.addAttribute(Attr_DW_FORM_ref2, DW_FORM_ref2, Data2);
163
164 const auto Attr_DW_FORM_ref4 = static_cast<dwarf::Attribute>(Attr++);
165 CUDie.addAttribute(Attr_DW_FORM_ref4, DW_FORM_ref4, Data4);
166
167 const auto Attr_DW_FORM_ref8 = static_cast<dwarf::Attribute>(Attr++);
168 CUDie.addAttribute(Attr_DW_FORM_ref8, DW_FORM_ref8, Data8);
169
170 const auto Attr_DW_FORM_ref_sig8 = static_cast<dwarf::Attribute>(Attr++);
Paul Robinson70b34532017-04-20 19:16:51 +0000171 if (Version >= 4)
172 CUDie.addAttribute(Attr_DW_FORM_ref_sig8, DW_FORM_ref_sig8, Data8_2);
Greg Clayton3462a422016-12-08 01:03:48 +0000173
174 const auto Attr_DW_FORM_ref_udata = static_cast<dwarf::Attribute>(Attr++);
175 CUDie.addAttribute(Attr_DW_FORM_ref_udata, DW_FORM_ref_udata, UData[0]);
176
177 //----------------------------------------------------------------------
178 // Test flag forms
179 //----------------------------------------------------------------------
180 const auto Attr_DW_FORM_flag_true = static_cast<dwarf::Attribute>(Attr++);
181 CUDie.addAttribute(Attr_DW_FORM_flag_true, DW_FORM_flag, true);
182
183 const auto Attr_DW_FORM_flag_false = static_cast<dwarf::Attribute>(Attr++);
184 CUDie.addAttribute(Attr_DW_FORM_flag_false, DW_FORM_flag, false);
185
186 const auto Attr_DW_FORM_flag_present = static_cast<dwarf::Attribute>(Attr++);
Paul Robinson70b34532017-04-20 19:16:51 +0000187 if (Version >= 4)
188 CUDie.addAttribute(Attr_DW_FORM_flag_present, DW_FORM_flag_present);
Greg Clayton3462a422016-12-08 01:03:48 +0000189
190 //----------------------------------------------------------------------
191 // Test SLEB128 based forms
192 //----------------------------------------------------------------------
193 const auto Attr_DW_FORM_sdata = static_cast<dwarf::Attribute>(Attr++);
194 CUDie.addAttribute(Attr_DW_FORM_sdata, DW_FORM_sdata, SData);
195
Victor Leschukcbddae72017-01-10 21:18:26 +0000196 const auto Attr_DW_FORM_implicit_const =
197 static_cast<dwarf::Attribute>(Attr++);
198 if (Version >= 5)
199 CUDie.addAttribute(Attr_DW_FORM_implicit_const, DW_FORM_implicit_const,
200 ICSData);
201
Greg Clayton3462a422016-12-08 01:03:48 +0000202 //----------------------------------------------------------------------
203 // Test ULEB128 based forms
204 //----------------------------------------------------------------------
205 const auto Attr_DW_FORM_udata = static_cast<dwarf::Attribute>(Attr++);
206 CUDie.addAttribute(Attr_DW_FORM_udata, DW_FORM_udata, UData[0]);
207
208 //----------------------------------------------------------------------
209 // Test DWARF32/DWARF64 forms
210 //----------------------------------------------------------------------
211 const auto Attr_DW_FORM_GNU_ref_alt = static_cast<dwarf::Attribute>(Attr++);
212 CUDie.addAttribute(Attr_DW_FORM_GNU_ref_alt, DW_FORM_GNU_ref_alt,
213 Dwarf32Values[0]);
214
215 const auto Attr_DW_FORM_sec_offset = static_cast<dwarf::Attribute>(Attr++);
Paul Robinson70b34532017-04-20 19:16:51 +0000216 if (Version >= 4)
217 CUDie.addAttribute(Attr_DW_FORM_sec_offset, DW_FORM_sec_offset,
218 Dwarf32Values[1]);
Greg Clayton3462a422016-12-08 01:03:48 +0000219
220 //----------------------------------------------------------------------
221 // Add an address at the end to make sure we can decode this value
222 //----------------------------------------------------------------------
223 const auto Attr_Last = static_cast<dwarf::Attribute>(Attr++);
224 CUDie.addAttribute(Attr_Last, DW_FORM_addr, AddrValue);
225
226 //----------------------------------------------------------------------
227 // Generate the DWARF
228 //----------------------------------------------------------------------
229 StringRef FileBytes = DG->generate();
230 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
231 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
232 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +0000233 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
234 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton3462a422016-12-08 01:03:48 +0000235 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +0000236 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
Greg Claytonc8c10322016-12-13 18:25:19 +0000237 auto DieDG = U->getUnitDIE(false);
238 EXPECT_TRUE(DieDG.isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000239
240 //----------------------------------------------------------------------
241 // Test address forms
242 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000243 EXPECT_EQ(AddrValue, toAddress(DieDG.find(Attr_DW_FORM_addr), 0));
Greg Clayton3462a422016-12-08 01:03:48 +0000244
245 //----------------------------------------------------------------------
246 // Test block forms
247 //----------------------------------------------------------------------
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000248 Optional<DWARFFormValue> FormValue;
Greg Clayton3462a422016-12-08 01:03:48 +0000249 ArrayRef<uint8_t> ExtractedBlockData;
250 Optional<ArrayRef<uint8_t>> BlockDataOpt;
251
Greg Clayton97d22182017-01-13 21:08:18 +0000252 FormValue = DieDG.find(Attr_DW_FORM_block);
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
Greg Clayton97d22182017-01-13 21:08:18 +0000260 FormValue = DieDG.find(Attr_DW_FORM_block1);
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000261 EXPECT_TRUE((bool)FormValue);
262 BlockDataOpt = FormValue->getAsBlock();
Greg Clayton3462a422016-12-08 01:03:48 +0000263 EXPECT_TRUE(BlockDataOpt.hasValue());
264 ExtractedBlockData = BlockDataOpt.getValue();
265 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
266 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
267
Greg Clayton97d22182017-01-13 21:08:18 +0000268 FormValue = DieDG.find(Attr_DW_FORM_block2);
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000269 EXPECT_TRUE((bool)FormValue);
270 BlockDataOpt = FormValue->getAsBlock();
Greg Clayton3462a422016-12-08 01:03:48 +0000271 EXPECT_TRUE(BlockDataOpt.hasValue());
272 ExtractedBlockData = BlockDataOpt.getValue();
273 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
274 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
275
Greg Clayton97d22182017-01-13 21:08:18 +0000276 FormValue = DieDG.find(Attr_DW_FORM_block4);
Greg Clayton1cbf3fa2016-12-13 23:20:56 +0000277 EXPECT_TRUE((bool)FormValue);
278 BlockDataOpt = FormValue->getAsBlock();
Greg Clayton3462a422016-12-08 01:03:48 +0000279 EXPECT_TRUE(BlockDataOpt.hasValue());
280 ExtractedBlockData = BlockDataOpt.getValue();
281 EXPECT_EQ(ExtractedBlockData.size(), BlockSize);
282 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), BlockData, BlockSize) == 0);
283
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000284 // Data16 is handled like a block.
285 if (Version >= 5) {
286 FormValue = DieDG.find(Attr_DW_FORM_data16);
287 EXPECT_TRUE((bool)FormValue);
288 BlockDataOpt = FormValue->getAsBlock();
289 EXPECT_TRUE(BlockDataOpt.hasValue());
290 ExtractedBlockData = BlockDataOpt.getValue();
291 EXPECT_EQ(ExtractedBlockData.size(), 16u);
292 EXPECT_TRUE(memcmp(ExtractedBlockData.data(), Data16, 16) == 0);
293 }
294
Greg Clayton3462a422016-12-08 01:03:48 +0000295 //----------------------------------------------------------------------
296 // Test data forms
297 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000298 EXPECT_EQ(Data1, toUnsigned(DieDG.find(Attr_DW_FORM_data1), 0));
299 EXPECT_EQ(Data2, toUnsigned(DieDG.find(Attr_DW_FORM_data2), 0));
300 EXPECT_EQ(Data4, toUnsigned(DieDG.find(Attr_DW_FORM_data4), 0));
301 EXPECT_EQ(Data8, toUnsigned(DieDG.find(Attr_DW_FORM_data8), 0));
Greg Clayton3462a422016-12-08 01:03:48 +0000302
303 //----------------------------------------------------------------------
304 // Test string forms
305 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000306 auto ExtractedStringValue = toString(DieDG.find(Attr_DW_FORM_string));
307 EXPECT_TRUE((bool)ExtractedStringValue);
Pavel Labath78ab6592018-07-25 15:33:32 +0000308 EXPECT_STREQ(StringValue, *ExtractedStringValue);
309
310 if (Version >= 5) {
311 auto ExtractedStrxValue = toString(DieDG.find(Attr_DW_FORM_strx));
312 EXPECT_TRUE((bool)ExtractedStrxValue);
313 EXPECT_STREQ(StrxValue, *ExtractedStrxValue);
314
315 auto ExtractedStrx1Value = toString(DieDG.find(Attr_DW_FORM_strx1));
316 EXPECT_TRUE((bool)ExtractedStrx1Value);
317 EXPECT_STREQ(Strx1Value, *ExtractedStrx1Value);
318
319 auto ExtractedStrx2Value = toString(DieDG.find(Attr_DW_FORM_strx2));
320 EXPECT_TRUE((bool)ExtractedStrx2Value);
321 EXPECT_STREQ(Strx2Value, *ExtractedStrx2Value);
322
323 auto ExtractedStrx3Value = toString(DieDG.find(Attr_DW_FORM_strx3));
324 EXPECT_TRUE((bool)ExtractedStrx3Value);
325 EXPECT_STREQ(Strx3Value, *ExtractedStrx3Value);
326
327 auto ExtractedStrx4Value = toString(DieDG.find(Attr_DW_FORM_strx4));
328 EXPECT_TRUE((bool)ExtractedStrx4Value);
329 EXPECT_STREQ(Strx4Value, *ExtractedStrx4Value);
330 }
Greg Clayton3462a422016-12-08 01:03:48 +0000331
Greg Clayton97d22182017-01-13 21:08:18 +0000332 auto ExtractedStrpValue = toString(DieDG.find(Attr_DW_FORM_strp));
333 EXPECT_TRUE((bool)ExtractedStrpValue);
Pavel Labath78ab6592018-07-25 15:33:32 +0000334 EXPECT_STREQ(StrpValue, *ExtractedStrpValue);
Greg Clayton3462a422016-12-08 01:03:48 +0000335
336 //----------------------------------------------------------------------
337 // Test reference forms
338 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000339 EXPECT_EQ(RefAddr, toReference(DieDG.find(Attr_DW_FORM_ref_addr), 0));
340 EXPECT_EQ(Data1, toReference(DieDG.find(Attr_DW_FORM_ref1), 0));
341 EXPECT_EQ(Data2, toReference(DieDG.find(Attr_DW_FORM_ref2), 0));
342 EXPECT_EQ(Data4, toReference(DieDG.find(Attr_DW_FORM_ref4), 0));
343 EXPECT_EQ(Data8, toReference(DieDG.find(Attr_DW_FORM_ref8), 0));
Galina Kistanovafcae62d2017-06-15 21:00:40 +0000344 if (Version >= 4) {
Paul Robinson70b34532017-04-20 19:16:51 +0000345 EXPECT_EQ(Data8_2, toReference(DieDG.find(Attr_DW_FORM_ref_sig8), 0));
Galina Kistanovafcae62d2017-06-15 21:00:40 +0000346 }
Greg Clayton97d22182017-01-13 21:08:18 +0000347 EXPECT_EQ(UData[0], toReference(DieDG.find(Attr_DW_FORM_ref_udata), 0));
Greg Clayton3462a422016-12-08 01:03:48 +0000348
349 //----------------------------------------------------------------------
350 // Test flag forms
351 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000352 EXPECT_EQ(1ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_true), 0));
353 EXPECT_EQ(0ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_false), 1));
Galina Kistanovafcae62d2017-06-15 21:00:40 +0000354 if (Version >= 4) {
Paul Robinson70b34532017-04-20 19:16:51 +0000355 EXPECT_EQ(1ULL, toUnsigned(DieDG.find(Attr_DW_FORM_flag_present), 0));
Galina Kistanovafcae62d2017-06-15 21:00:40 +0000356 }
Greg Clayton3462a422016-12-08 01:03:48 +0000357
Greg Clayton3462a422016-12-08 01:03:48 +0000358 //----------------------------------------------------------------------
359 // Test SLEB128 based forms
360 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000361 EXPECT_EQ(SData, toSigned(DieDG.find(Attr_DW_FORM_sdata), 0));
Galina Kistanovafcae62d2017-06-15 21:00:40 +0000362 if (Version >= 5) {
Greg Clayton97d22182017-01-13 21:08:18 +0000363 EXPECT_EQ(ICSData, toSigned(DieDG.find(Attr_DW_FORM_implicit_const), 0));
Galina Kistanovafcae62d2017-06-15 21:00:40 +0000364 }
Greg Clayton3462a422016-12-08 01:03:48 +0000365
366 //----------------------------------------------------------------------
367 // Test ULEB128 based forms
368 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000369 EXPECT_EQ(UData[0], toUnsigned(DieDG.find(Attr_DW_FORM_udata), 0));
Greg Clayton3462a422016-12-08 01:03:48 +0000370
371 //----------------------------------------------------------------------
372 // Test DWARF32/DWARF64 forms
373 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000374 EXPECT_EQ(Dwarf32Values[0],
375 toReference(DieDG.find(Attr_DW_FORM_GNU_ref_alt), 0));
Galina Kistanovafcae62d2017-06-15 21:00:40 +0000376 if (Version >= 4) {
Paul Robinson70b34532017-04-20 19:16:51 +0000377 EXPECT_EQ(Dwarf32Values[1],
378 toSectionOffset(DieDG.find(Attr_DW_FORM_sec_offset), 0));
Galina Kistanovafcae62d2017-06-15 21:00:40 +0000379 }
Greg Clayton3462a422016-12-08 01:03:48 +0000380
381 //----------------------------------------------------------------------
382 // Add an address at the end to make sure we can decode this value
383 //----------------------------------------------------------------------
Greg Clayton97d22182017-01-13 21:08:18 +0000384 EXPECT_EQ(AddrValue, toAddress(DieDG.find(Attr_Last), 0));
Greg Clayton3462a422016-12-08 01:03:48 +0000385}
386
387TEST(DWARFDebugInfo, TestDWARF32Version2Addr4AllForms) {
388 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
389 // addresses.
390 typedef uint32_t AddrType;
391 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2.
392 typedef AddrType RefAddrType;
393 TestAllForms<2, AddrType, RefAddrType>();
394}
395
396TEST(DWARFDebugInfo, TestDWARF32Version2Addr8AllForms) {
397 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
398 // addresses.
399 typedef uint64_t AddrType;
400 // DW_FORM_ref_addr are the same as the address type in DWARF32 version 2.
401 typedef AddrType RefAddrType;
402 TestAllForms<2, AddrType, RefAddrType>();
403}
404
405TEST(DWARFDebugInfo, TestDWARF32Version3Addr4AllForms) {
406 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
407 // addresses.
408 typedef uint32_t AddrType;
409 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later.
410 typedef uint32_t RefAddrType;
411 TestAllForms<3, AddrType, RefAddrType>();
412}
413
414TEST(DWARFDebugInfo, TestDWARF32Version3Addr8AllForms) {
415 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
416 // addresses.
417 typedef uint64_t AddrType;
418 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
419 typedef uint32_t RefAddrType;
420 TestAllForms<3, AddrType, RefAddrType>();
421}
422
423TEST(DWARFDebugInfo, TestDWARF32Version4Addr4AllForms) {
424 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
425 // addresses.
426 typedef uint32_t AddrType;
427 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
428 typedef uint32_t RefAddrType;
429 TestAllForms<4, AddrType, RefAddrType>();
430}
431
432TEST(DWARFDebugInfo, TestDWARF32Version4Addr8AllForms) {
433 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
434 // addresses.
435 typedef uint64_t AddrType;
436 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
437 typedef uint32_t RefAddrType;
438 TestAllForms<4, AddrType, RefAddrType>();
439}
440
Victor Leschukcbddae72017-01-10 21:18:26 +0000441TEST(DWARFDebugInfo, TestDWARF32Version5Addr4AllForms) {
442 // Test that we can decode all forms for DWARF32, version 5, with 4 byte
443 // addresses.
444 typedef uint32_t AddrType;
445 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
446 typedef uint32_t RefAddrType;
447 TestAllForms<5, AddrType, RefAddrType>();
448}
449
450TEST(DWARFDebugInfo, TestDWARF32Version5Addr8AllForms) {
451 // Test that we can decode all forms for DWARF32, version 5, with 8 byte
452 // addresses.
453 typedef uint64_t AddrType;
454 // DW_FORM_ref_addr are 4 bytes in DWARF32 for version 3 and later
455 typedef uint32_t RefAddrType;
456 TestAllForms<5, AddrType, RefAddrType>();
457}
458
Greg Clayton3462a422016-12-08 01:03:48 +0000459template <uint16_t Version, class AddrType> void TestChildren() {
George Rimard8508b02017-06-30 10:09:01 +0000460 Triple Triple = getHostTripleForAddrSize(sizeof(AddrType));
461 if (!isConfigurationSupported(Triple))
462 return;
463
Greg Clayton3462a422016-12-08 01:03:48 +0000464 // Test that we can decode DW_FORM_ref_addr values correctly in DWARF 2 with
465 // 4 byte addresses. DW_FORM_ref_addr values should be 4 bytes when using
466 // 8 byte addresses.
467
Greg Clayton3462a422016-12-08 01:03:48 +0000468 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
George Rimard8508b02017-06-30 10:09:01 +0000469 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Clayton3462a422016-12-08 01:03:48 +0000470 dwarfgen::Generator *DG = ExpectedDG.get().get();
471 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
472 dwarfgen::DIE CUDie = CU.getUnitDIE();
473
474 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
475 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
476
477 dwarfgen::DIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram);
478 SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main");
479 SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U);
480 SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U);
481
482 dwarfgen::DIE IntDie = CUDie.addChild(DW_TAG_base_type);
483 IntDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");
484 IntDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
485 IntDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
486
487 dwarfgen::DIE ArgcDie = SubprogramDie.addChild(DW_TAG_formal_parameter);
488 ArgcDie.addAttribute(DW_AT_name, DW_FORM_strp, "argc");
489 // ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref4, IntDie);
490 ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, IntDie);
491
492 StringRef FileBytes = DG->generate();
493 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
494 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
495 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +0000496 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
Greg Clayton3462a422016-12-08 01:03:48 +0000497
498 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +0000499 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton3462a422016-12-08 01:03:48 +0000500 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +0000501 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
Greg Clayton3462a422016-12-08 01:03:48 +0000502
503 // Get the compile unit DIE is valid.
Greg Claytonc8c10322016-12-13 18:25:19 +0000504 auto DieDG = U->getUnitDIE(false);
505 EXPECT_TRUE(DieDG.isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000506
507 // Verify the first child of the compile unit DIE is our subprogram.
Greg Claytonc8c10322016-12-13 18:25:19 +0000508 auto SubprogramDieDG = DieDG.getFirstChild();
509 EXPECT_TRUE(SubprogramDieDG.isValid());
510 EXPECT_EQ(SubprogramDieDG.getTag(), DW_TAG_subprogram);
Greg Clayton3462a422016-12-08 01:03:48 +0000511
512 // Verify the first child of the subprogram is our formal parameter.
Greg Claytonc8c10322016-12-13 18:25:19 +0000513 auto ArgcDieDG = SubprogramDieDG.getFirstChild();
514 EXPECT_TRUE(ArgcDieDG.isValid());
515 EXPECT_EQ(ArgcDieDG.getTag(), DW_TAG_formal_parameter);
Greg Clayton3462a422016-12-08 01:03:48 +0000516
517 // Verify our formal parameter has a NULL tag sibling.
Greg Claytonc8c10322016-12-13 18:25:19 +0000518 auto NullDieDG = ArgcDieDG.getSibling();
519 EXPECT_TRUE(NullDieDG.isValid());
520 if (NullDieDG) {
521 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null);
522 EXPECT_TRUE(!NullDieDG.getSibling().isValid());
523 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000524 }
525
526 // Verify the sibling of our subprogram is our integer base type.
Greg Claytonc8c10322016-12-13 18:25:19 +0000527 auto IntDieDG = SubprogramDieDG.getSibling();
528 EXPECT_TRUE(IntDieDG.isValid());
529 EXPECT_EQ(IntDieDG.getTag(), DW_TAG_base_type);
Greg Clayton3462a422016-12-08 01:03:48 +0000530
531 // Verify the sibling of our subprogram is our integer base is a NULL tag.
Greg Claytonc8c10322016-12-13 18:25:19 +0000532 NullDieDG = IntDieDG.getSibling();
533 EXPECT_TRUE(NullDieDG.isValid());
534 if (NullDieDG) {
535 EXPECT_EQ(NullDieDG.getTag(), DW_TAG_null);
536 EXPECT_TRUE(!NullDieDG.getSibling().isValid());
537 EXPECT_TRUE(!NullDieDG.getFirstChild().isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000538 }
Jonas Devlieghere3f27e572018-07-11 17:11:11 +0000539
540 // Verify the previous sibling of our subprogram is our integer base type.
541 IntDieDG = NullDieDG.getPreviousSibling();
542 EXPECT_TRUE(IntDieDG.isValid());
543 EXPECT_EQ(IntDieDG.getTag(), DW_TAG_base_type);
Greg Clayton3462a422016-12-08 01:03:48 +0000544}
545
546TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Children) {
547 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
548 // addresses.
549 typedef uint32_t AddrType;
550 TestChildren<2, AddrType>();
551}
552
553TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Children) {
554 // Test that we can decode all forms for DWARF32, version 2, with 8 byte
555 // addresses.
556 typedef uint64_t AddrType;
557 TestChildren<2, AddrType>();
558}
559
560TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Children) {
561 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
562 // addresses.
563 typedef uint32_t AddrType;
564 TestChildren<3, AddrType>();
565}
566
567TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Children) {
568 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
569 // addresses.
570 typedef uint64_t AddrType;
571 TestChildren<3, AddrType>();
572}
573
574TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Children) {
575 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
576 // addresses.
577 typedef uint32_t AddrType;
578 TestChildren<4, AddrType>();
579}
580
581TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Children) {
582 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
583 // addresses.
584 typedef uint64_t AddrType;
585 TestChildren<4, AddrType>();
586}
587
588template <uint16_t Version, class AddrType> void TestReferences() {
George Rimard8508b02017-06-30 10:09:01 +0000589 Triple Triple = getHostTripleForAddrSize(sizeof(AddrType));
590 if (!isConfigurationSupported(Triple))
Greg Clayton3462a422016-12-08 01:03:48 +0000591 return;
George Rimard8508b02017-06-30 10:09:01 +0000592
593 // Test that we can decode DW_FORM_refXXX values correctly in DWARF.
594 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
595 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Clayton3462a422016-12-08 01:03:48 +0000596 dwarfgen::Generator *DG = ExpectedDG.get().get();
597 dwarfgen::CompileUnit &CU1 = DG->addCompileUnit();
598 dwarfgen::CompileUnit &CU2 = DG->addCompileUnit();
599
600 dwarfgen::DIE CU1Die = CU1.getUnitDIE();
601 CU1Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
602 CU1Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
603
604 dwarfgen::DIE CU1TypeDie = CU1Die.addChild(DW_TAG_base_type);
605 CU1TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "int");
606 CU1TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed);
607 CU1TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
608
609 dwarfgen::DIE CU1Ref1Die = CU1Die.addChild(DW_TAG_variable);
610 CU1Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref1");
611 CU1Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU1TypeDie);
612
613 dwarfgen::DIE CU1Ref2Die = CU1Die.addChild(DW_TAG_variable);
614 CU1Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref2");
615 CU1Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU1TypeDie);
616
617 dwarfgen::DIE CU1Ref4Die = CU1Die.addChild(DW_TAG_variable);
618 CU1Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref4");
619 CU1Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU1TypeDie);
620
621 dwarfgen::DIE CU1Ref8Die = CU1Die.addChild(DW_TAG_variable);
622 CU1Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU1Ref8");
623 CU1Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU1TypeDie);
624
625 dwarfgen::DIE CU1RefAddrDie = CU1Die.addChild(DW_TAG_variable);
626 CU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1RefAddr");
627 CU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie);
628
629 dwarfgen::DIE CU2Die = CU2.getUnitDIE();
630 CU2Die.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/foo.c");
631 CU2Die.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
632
633 dwarfgen::DIE CU2TypeDie = CU2Die.addChild(DW_TAG_base_type);
634 CU2TypeDie.addAttribute(DW_AT_name, DW_FORM_strp, "float");
635 CU2TypeDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_float);
636 CU2TypeDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4);
637
638 dwarfgen::DIE CU2Ref1Die = CU2Die.addChild(DW_TAG_variable);
639 CU2Ref1Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref1");
640 CU2Ref1Die.addAttribute(DW_AT_type, DW_FORM_ref1, CU2TypeDie);
641
642 dwarfgen::DIE CU2Ref2Die = CU2Die.addChild(DW_TAG_variable);
643 CU2Ref2Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref2");
644 CU2Ref2Die.addAttribute(DW_AT_type, DW_FORM_ref2, CU2TypeDie);
645
646 dwarfgen::DIE CU2Ref4Die = CU2Die.addChild(DW_TAG_variable);
647 CU2Ref4Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref4");
648 CU2Ref4Die.addAttribute(DW_AT_type, DW_FORM_ref4, CU2TypeDie);
649
650 dwarfgen::DIE CU2Ref8Die = CU2Die.addChild(DW_TAG_variable);
651 CU2Ref8Die.addAttribute(DW_AT_name, DW_FORM_strp, "CU2Ref8");
652 CU2Ref8Die.addAttribute(DW_AT_type, DW_FORM_ref8, CU2TypeDie);
653
654 dwarfgen::DIE CU2RefAddrDie = CU2Die.addChild(DW_TAG_variable);
655 CU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2RefAddr");
656 CU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie);
657
658 // Refer to a type in CU1 from CU2
659 dwarfgen::DIE CU2ToCU1RefAddrDie = CU2Die.addChild(DW_TAG_variable);
660 CU2ToCU1RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU2ToCU1RefAddr");
661 CU2ToCU1RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU1TypeDie);
662
663 // Refer to a type in CU2 from CU1
664 dwarfgen::DIE CU1ToCU2RefAddrDie = CU1Die.addChild(DW_TAG_variable);
665 CU1ToCU2RefAddrDie.addAttribute(DW_AT_name, DW_FORM_strp, "CU1ToCU2RefAddr");
666 CU1ToCU2RefAddrDie.addAttribute(DW_AT_type, DW_FORM_ref_addr, CU2TypeDie);
667
668 StringRef FileBytes = DG->generate();
669 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
670 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
671 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +0000672 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
Greg Clayton3462a422016-12-08 01:03:48 +0000673
674 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +0000675 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton3462a422016-12-08 01:03:48 +0000676 EXPECT_EQ(NumCUs, 2u);
Rafael Espindolac398e672017-07-19 22:27:28 +0000677 DWARFCompileUnit *U1 = DwarfContext->getCompileUnitAtIndex(0);
678 DWARFCompileUnit *U2 = DwarfContext->getCompileUnitAtIndex(1);
Greg Clayton3462a422016-12-08 01:03:48 +0000679
680 // Get the compile unit DIE is valid.
Greg Claytonc8c10322016-12-13 18:25:19 +0000681 auto Unit1DieDG = U1->getUnitDIE(false);
682 EXPECT_TRUE(Unit1DieDG.isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000683
Greg Claytonc8c10322016-12-13 18:25:19 +0000684 auto Unit2DieDG = U2->getUnitDIE(false);
685 EXPECT_TRUE(Unit2DieDG.isValid());
Greg Clayton3462a422016-12-08 01:03:48 +0000686
687 // Verify the first child of the compile unit 1 DIE is our int base type.
Greg Claytonc8c10322016-12-13 18:25:19 +0000688 auto CU1TypeDieDG = Unit1DieDG.getFirstChild();
689 EXPECT_TRUE(CU1TypeDieDG.isValid());
690 EXPECT_EQ(CU1TypeDieDG.getTag(), DW_TAG_base_type);
Greg Clayton97d22182017-01-13 21:08:18 +0000691 EXPECT_EQ(DW_ATE_signed, toUnsigned(CU1TypeDieDG.find(DW_AT_encoding), 0));
Greg Clayton3462a422016-12-08 01:03:48 +0000692
693 // Verify the first child of the compile unit 2 DIE is our float base type.
Greg Claytonc8c10322016-12-13 18:25:19 +0000694 auto CU2TypeDieDG = Unit2DieDG.getFirstChild();
695 EXPECT_TRUE(CU2TypeDieDG.isValid());
696 EXPECT_EQ(CU2TypeDieDG.getTag(), DW_TAG_base_type);
Greg Clayton97d22182017-01-13 21:08:18 +0000697 EXPECT_EQ(DW_ATE_float, toUnsigned(CU2TypeDieDG.find(DW_AT_encoding), 0));
Greg Clayton3462a422016-12-08 01:03:48 +0000698
699 // Verify the sibling of the base type DIE is our Ref1 DIE and that its
700 // DW_AT_type points to our base type DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000701 auto CU1Ref1DieDG = CU1TypeDieDG.getSibling();
702 EXPECT_TRUE(CU1Ref1DieDG.isValid());
703 EXPECT_EQ(CU1Ref1DieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000704 EXPECT_EQ(CU1TypeDieDG.getOffset(),
705 toReference(CU1Ref1DieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000706 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our
707 // base type DIE in CU1.
Greg Claytonc8c10322016-12-13 18:25:19 +0000708 auto CU1Ref2DieDG = CU1Ref1DieDG.getSibling();
709 EXPECT_TRUE(CU1Ref2DieDG.isValid());
710 EXPECT_EQ(CU1Ref2DieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000711 EXPECT_EQ(CU1TypeDieDG.getOffset(),
712 toReference(CU1Ref2DieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000713
714 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our
715 // base type DIE in CU1.
Greg Claytonc8c10322016-12-13 18:25:19 +0000716 auto CU1Ref4DieDG = CU1Ref2DieDG.getSibling();
717 EXPECT_TRUE(CU1Ref4DieDG.isValid());
718 EXPECT_EQ(CU1Ref4DieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000719 EXPECT_EQ(CU1TypeDieDG.getOffset(),
720 toReference(CU1Ref4DieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000721
722 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our
723 // base type DIE in CU1.
Greg Claytonc8c10322016-12-13 18:25:19 +0000724 auto CU1Ref8DieDG = CU1Ref4DieDG.getSibling();
725 EXPECT_TRUE(CU1Ref8DieDG.isValid());
726 EXPECT_EQ(CU1Ref8DieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000727 EXPECT_EQ(CU1TypeDieDG.getOffset(),
728 toReference(CU1Ref8DieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000729
730 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our
731 // base type DIE in CU1.
Greg Claytonc8c10322016-12-13 18:25:19 +0000732 auto CU1RefAddrDieDG = CU1Ref8DieDG.getSibling();
733 EXPECT_TRUE(CU1RefAddrDieDG.isValid());
734 EXPECT_EQ(CU1RefAddrDieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000735 EXPECT_EQ(CU1TypeDieDG.getOffset(),
736 toReference(CU1RefAddrDieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000737
738 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its
739 // DW_AT_type points to our base type DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000740 auto CU1ToCU2RefAddrDieDG = CU1RefAddrDieDG.getSibling();
741 EXPECT_TRUE(CU1ToCU2RefAddrDieDG.isValid());
742 EXPECT_EQ(CU1ToCU2RefAddrDieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000743 EXPECT_EQ(CU2TypeDieDG.getOffset(),
744 toReference(CU1ToCU2RefAddrDieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000745
746 // Verify the sibling of the base type DIE is our Ref1 DIE and that its
747 // DW_AT_type points to our base type DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000748 auto CU2Ref1DieDG = CU2TypeDieDG.getSibling();
749 EXPECT_TRUE(CU2Ref1DieDG.isValid());
750 EXPECT_EQ(CU2Ref1DieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000751 EXPECT_EQ(CU2TypeDieDG.getOffset(),
752 toReference(CU2Ref1DieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000753 // Verify the sibling is our Ref2 DIE and that its DW_AT_type points to our
754 // base type DIE in CU2.
Greg Claytonc8c10322016-12-13 18:25:19 +0000755 auto CU2Ref2DieDG = CU2Ref1DieDG.getSibling();
756 EXPECT_TRUE(CU2Ref2DieDG.isValid());
757 EXPECT_EQ(CU2Ref2DieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000758 EXPECT_EQ(CU2TypeDieDG.getOffset(),
759 toReference(CU2Ref2DieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000760
761 // Verify the sibling is our Ref4 DIE and that its DW_AT_type points to our
762 // base type DIE in CU2.
Greg Claytonc8c10322016-12-13 18:25:19 +0000763 auto CU2Ref4DieDG = CU2Ref2DieDG.getSibling();
764 EXPECT_TRUE(CU2Ref4DieDG.isValid());
765 EXPECT_EQ(CU2Ref4DieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000766 EXPECT_EQ(CU2TypeDieDG.getOffset(),
767 toReference(CU2Ref4DieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000768
769 // Verify the sibling is our Ref8 DIE and that its DW_AT_type points to our
770 // base type DIE in CU2.
Greg Claytonc8c10322016-12-13 18:25:19 +0000771 auto CU2Ref8DieDG = CU2Ref4DieDG.getSibling();
772 EXPECT_TRUE(CU2Ref8DieDG.isValid());
773 EXPECT_EQ(CU2Ref8DieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000774 EXPECT_EQ(CU2TypeDieDG.getOffset(),
775 toReference(CU2Ref8DieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000776
777 // Verify the sibling is our RefAddr DIE and that its DW_AT_type points to our
778 // base type DIE in CU2.
Greg Claytonc8c10322016-12-13 18:25:19 +0000779 auto CU2RefAddrDieDG = CU2Ref8DieDG.getSibling();
780 EXPECT_TRUE(CU2RefAddrDieDG.isValid());
781 EXPECT_EQ(CU2RefAddrDieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000782 EXPECT_EQ(CU2TypeDieDG.getOffset(),
783 toReference(CU2RefAddrDieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000784
785 // Verify the sibling of the Ref4 DIE is our RefAddr DIE and that its
786 // DW_AT_type points to our base type DIE.
Greg Claytonc8c10322016-12-13 18:25:19 +0000787 auto CU2ToCU1RefAddrDieDG = CU2RefAddrDieDG.getSibling();
788 EXPECT_TRUE(CU2ToCU1RefAddrDieDG.isValid());
789 EXPECT_EQ(CU2ToCU1RefAddrDieDG.getTag(), DW_TAG_variable);
Greg Clayton97d22182017-01-13 21:08:18 +0000790 EXPECT_EQ(CU1TypeDieDG.getOffset(),
791 toReference(CU2ToCU1RefAddrDieDG.find(DW_AT_type), -1ULL));
Greg Clayton3462a422016-12-08 01:03:48 +0000792}
793
794TEST(DWARFDebugInfo, TestDWARF32Version2Addr4References) {
795 // Test that we can decode all forms for DWARF32, version 2, with 4 byte
796 // addresses.
797 typedef uint32_t AddrType;
798 TestReferences<2, AddrType>();
799}
800
801TEST(DWARFDebugInfo, TestDWARF32Version2Addr8References) {
802 // Test that we can decode all forms for DWARF32, version 2, with 8 byte
803 // addresses.
804 typedef uint64_t AddrType;
805 TestReferences<2, AddrType>();
806}
807
808TEST(DWARFDebugInfo, TestDWARF32Version3Addr4References) {
809 // Test that we can decode all forms for DWARF32, version 3, with 4 byte
810 // addresses.
811 typedef uint32_t AddrType;
812 TestReferences<3, AddrType>();
813}
814
815TEST(DWARFDebugInfo, TestDWARF32Version3Addr8References) {
816 // Test that we can decode all forms for DWARF32, version 3, with 8 byte
817 // addresses.
818 typedef uint64_t AddrType;
819 TestReferences<3, AddrType>();
820}
821
822TEST(DWARFDebugInfo, TestDWARF32Version4Addr4References) {
823 // Test that we can decode all forms for DWARF32, version 4, with 4 byte
824 // addresses.
825 typedef uint32_t AddrType;
826 TestReferences<4, AddrType>();
827}
828
829TEST(DWARFDebugInfo, TestDWARF32Version4Addr8References) {
830 // Test that we can decode all forms for DWARF32, version 4, with 8 byte
831 // addresses.
832 typedef uint64_t AddrType;
833 TestReferences<4, AddrType>();
834}
835
Greg Clayton2520c9e2016-12-19 20:36:41 +0000836template <uint16_t Version, class AddrType> void TestAddresses() {
George Rimard8508b02017-06-30 10:09:01 +0000837 Triple Triple = getHostTripleForAddrSize(sizeof(AddrType));
838 if (!isConfigurationSupported(Triple))
839 return;
840
Greg Clayton2520c9e2016-12-19 20:36:41 +0000841 // Test the DWARF APIs related to accessing the DW_AT_low_pc and
842 // DW_AT_high_pc.
Greg Clayton2520c9e2016-12-19 20:36:41 +0000843 const bool SupportsHighPCAsOffset = Version >= 4;
Greg Clayton2520c9e2016-12-19 20:36:41 +0000844 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
George Rimard8508b02017-06-30 10:09:01 +0000845 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Clayton2520c9e2016-12-19 20:36:41 +0000846 dwarfgen::Generator *DG = ExpectedDG.get().get();
847 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
848 dwarfgen::DIE CUDie = CU.getUnitDIE();
George Rimar002655d2017-06-28 08:26:57 +0000849
Greg Clayton2520c9e2016-12-19 20:36:41 +0000850 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c");
851 CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C);
George Rimar002655d2017-06-28 08:26:57 +0000852
Greg Clayton2520c9e2016-12-19 20:36:41 +0000853 // Create a subprogram DIE with no low or high PC.
854 dwarfgen::DIE SubprogramNoPC = CUDie.addChild(DW_TAG_subprogram);
855 SubprogramNoPC.addAttribute(DW_AT_name, DW_FORM_strp, "no_pc");
856
857 // Create a subprogram DIE with a low PC only.
858 dwarfgen::DIE SubprogramLowPC = CUDie.addChild(DW_TAG_subprogram);
859 SubprogramLowPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_pc");
860 const uint64_t ActualLowPC = 0x1000;
861 const uint64_t ActualHighPC = 0x2000;
862 const uint64_t ActualHighPCOffset = ActualHighPC - ActualLowPC;
863 SubprogramLowPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC);
864
865 // Create a subprogram DIE with a low and high PC.
866 dwarfgen::DIE SubprogramLowHighPC = CUDie.addChild(DW_TAG_subprogram);
867 SubprogramLowHighPC.addAttribute(DW_AT_name, DW_FORM_strp, "low_high_pc");
868 SubprogramLowHighPC.addAttribute(DW_AT_low_pc, DW_FORM_addr, ActualLowPC);
869 // Encode the high PC as an offset from the low PC if supported.
870 if (SupportsHighPCAsOffset)
871 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_data4,
872 ActualHighPCOffset);
873 else
874 SubprogramLowHighPC.addAttribute(DW_AT_high_pc, DW_FORM_addr, ActualHighPC);
George Rimar002655d2017-06-28 08:26:57 +0000875
Greg Clayton2520c9e2016-12-19 20:36:41 +0000876 StringRef FileBytes = DG->generate();
877 MemoryBufferRef FileBuffer(FileBytes, "dwarf");
878 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
879 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +0000880 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
George Rimar002655d2017-06-28 08:26:57 +0000881
Greg Clayton2520c9e2016-12-19 20:36:41 +0000882 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +0000883 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton2520c9e2016-12-19 20:36:41 +0000884 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +0000885 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
George Rimar002655d2017-06-28 08:26:57 +0000886
Greg Clayton2520c9e2016-12-19 20:36:41 +0000887 // Get the compile unit DIE is valid.
888 auto DieDG = U->getUnitDIE(false);
889 EXPECT_TRUE(DieDG.isValid());
George Rimara25d3292017-05-27 18:10:23 +0000890
891 uint64_t LowPC, HighPC, SectionIndex;
Greg Clayton2520c9e2016-12-19 20:36:41 +0000892 Optional<uint64_t> OptU64;
893 // Verify the that our subprogram with no PC value fails appropriately when
894 // asked for any PC values.
895 auto SubprogramDieNoPC = DieDG.getFirstChild();
896 EXPECT_TRUE(SubprogramDieNoPC.isValid());
897 EXPECT_EQ(SubprogramDieNoPC.getTag(), DW_TAG_subprogram);
Greg Clayton97d22182017-01-13 21:08:18 +0000898 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_low_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000899 EXPECT_FALSE((bool)OptU64);
Greg Clayton97d22182017-01-13 21:08:18 +0000900 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_high_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000901 EXPECT_FALSE((bool)OptU64);
George Rimara25d3292017-05-27 18:10:23 +0000902 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC, SectionIndex));
Greg Clayton97d22182017-01-13 21:08:18 +0000903 OptU64 = toAddress(SubprogramDieNoPC.find(DW_AT_high_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000904 EXPECT_FALSE((bool)OptU64);
Greg Clayton97d22182017-01-13 21:08:18 +0000905 OptU64 = toUnsigned(SubprogramDieNoPC.find(DW_AT_high_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000906 EXPECT_FALSE((bool)OptU64);
907 OptU64 = SubprogramDieNoPC.getHighPC(ActualLowPC);
908 EXPECT_FALSE((bool)OptU64);
George Rimara25d3292017-05-27 18:10:23 +0000909 EXPECT_FALSE(SubprogramDieNoPC.getLowAndHighPC(LowPC, HighPC, SectionIndex));
George Rimar002655d2017-06-28 08:26:57 +0000910
Greg Clayton2520c9e2016-12-19 20:36:41 +0000911 // Verify the that our subprogram with only a low PC value succeeds when
912 // we ask for the Low PC, but fails appropriately when asked for the high PC
913 // or both low and high PC values.
914 auto SubprogramDieLowPC = SubprogramDieNoPC.getSibling();
915 EXPECT_TRUE(SubprogramDieLowPC.isValid());
916 EXPECT_EQ(SubprogramDieLowPC.getTag(), DW_TAG_subprogram);
Greg Clayton97d22182017-01-13 21:08:18 +0000917 OptU64 = toAddress(SubprogramDieLowPC.find(DW_AT_low_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000918 EXPECT_TRUE((bool)OptU64);
919 EXPECT_EQ(OptU64.getValue(), ActualLowPC);
Greg Clayton97d22182017-01-13 21:08:18 +0000920 OptU64 = toAddress(SubprogramDieLowPC.find(DW_AT_high_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000921 EXPECT_FALSE((bool)OptU64);
Greg Clayton97d22182017-01-13 21:08:18 +0000922 OptU64 = toUnsigned(SubprogramDieLowPC.find(DW_AT_high_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000923 EXPECT_FALSE((bool)OptU64);
924 OptU64 = SubprogramDieLowPC.getHighPC(ActualLowPC);
925 EXPECT_FALSE((bool)OptU64);
George Rimara25d3292017-05-27 18:10:23 +0000926 EXPECT_FALSE(SubprogramDieLowPC.getLowAndHighPC(LowPC, HighPC, SectionIndex));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000927
Greg Clayton2520c9e2016-12-19 20:36:41 +0000928 // Verify the that our subprogram with only a low PC value succeeds when
929 // we ask for the Low PC, but fails appropriately when asked for the high PC
930 // or both low and high PC values.
931 auto SubprogramDieLowHighPC = SubprogramDieLowPC.getSibling();
932 EXPECT_TRUE(SubprogramDieLowHighPC.isValid());
933 EXPECT_EQ(SubprogramDieLowHighPC.getTag(), DW_TAG_subprogram);
Greg Clayton97d22182017-01-13 21:08:18 +0000934 OptU64 = toAddress(SubprogramDieLowHighPC.find(DW_AT_low_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000935 EXPECT_TRUE((bool)OptU64);
936 EXPECT_EQ(OptU64.getValue(), ActualLowPC);
937 // Get the high PC as an address. This should succeed if the high PC was
938 // encoded as an address and fail if the high PC was encoded as an offset.
Greg Clayton97d22182017-01-13 21:08:18 +0000939 OptU64 = toAddress(SubprogramDieLowHighPC.find(DW_AT_high_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000940 if (SupportsHighPCAsOffset) {
941 EXPECT_FALSE((bool)OptU64);
942 } else {
943 EXPECT_TRUE((bool)OptU64);
944 EXPECT_EQ(OptU64.getValue(), ActualHighPC);
945 }
946 // Get the high PC as an unsigned constant. This should succeed if the high PC
947 // was encoded as an offset and fail if the high PC was encoded as an address.
Greg Clayton97d22182017-01-13 21:08:18 +0000948 OptU64 = toUnsigned(SubprogramDieLowHighPC.find(DW_AT_high_pc));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000949 if (SupportsHighPCAsOffset) {
950 EXPECT_TRUE((bool)OptU64);
951 EXPECT_EQ(OptU64.getValue(), ActualHighPCOffset);
952 } else {
953 EXPECT_FALSE((bool)OptU64);
954 }
955
956 OptU64 = SubprogramDieLowHighPC.getHighPC(ActualLowPC);
957 EXPECT_TRUE((bool)OptU64);
958 EXPECT_EQ(OptU64.getValue(), ActualHighPC);
959
George Rimara25d3292017-05-27 18:10:23 +0000960 EXPECT_TRUE(SubprogramDieLowHighPC.getLowAndHighPC(LowPC, HighPC, SectionIndex));
Greg Clayton2520c9e2016-12-19 20:36:41 +0000961 EXPECT_EQ(LowPC, ActualLowPC);
962 EXPECT_EQ(HighPC, ActualHighPC);
963}
964
965TEST(DWARFDebugInfo, TestDWARF32Version2Addr4Addresses) {
966 // Test that we can decode address values in DWARF32, version 2, with 4 byte
967 // addresses.
968 typedef uint32_t AddrType;
969 TestAddresses<2, AddrType>();
970}
971
972TEST(DWARFDebugInfo, TestDWARF32Version2Addr8Addresses) {
973 // Test that we can decode address values in DWARF32, version 2, with 8 byte
974 // addresses.
975 typedef uint64_t AddrType;
976 TestAddresses<2, AddrType>();
977}
978
979TEST(DWARFDebugInfo, TestDWARF32Version3Addr4Addresses) {
980 // Test that we can decode address values in DWARF32, version 3, with 4 byte
981 // addresses.
982 typedef uint32_t AddrType;
983 TestAddresses<3, AddrType>();
984}
985
986TEST(DWARFDebugInfo, TestDWARF32Version3Addr8Addresses) {
987 // Test that we can decode address values in DWARF32, version 3, with 8 byte
988 // addresses.
989 typedef uint64_t AddrType;
990 TestAddresses<3, AddrType>();
991}
992
993TEST(DWARFDebugInfo, TestDWARF32Version4Addr4Addresses) {
994 // Test that we can decode address values in DWARF32, version 4, with 4 byte
995 // addresses.
996 typedef uint32_t AddrType;
997 TestAddresses<4, AddrType>();
998}
999
1000TEST(DWARFDebugInfo, TestDWARF32Version4Addr8Addresses) {
1001 // Test that we can decode address values in DWARF32, version 4, with 8 byte
1002 // addresses.
1003 typedef uint64_t AddrType;
1004 TestAddresses<4, AddrType>();
1005}
1006
Greg Clayton78a07bf2016-12-21 21:37:06 +00001007TEST(DWARFDebugInfo, TestRelations) {
George Rimard8508b02017-06-30 10:09:01 +00001008 Triple Triple = getHostTripleForAddrSize(sizeof(void *));
1009 if (!isConfigurationSupported(Triple))
1010 return;
1011
Greg Clayton78a07bf2016-12-21 21:37:06 +00001012 // Test the DWARF APIs related to accessing the DW_AT_low_pc and
1013 // DW_AT_high_pc.
1014 uint16_t Version = 4;
Greg Clayton78a07bf2016-12-21 21:37:06 +00001015 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
George Rimard8508b02017-06-30 10:09:01 +00001016 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Clayton78a07bf2016-12-21 21:37:06 +00001017 dwarfgen::Generator *DG = ExpectedDG.get().get();
1018 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
George Rimar002655d2017-06-28 08:26:57 +00001019
Greg Clayton78a07bf2016-12-21 21:37:06 +00001020 enum class Tag: uint16_t {
1021 A = dwarf::DW_TAG_lo_user,
1022 B,
Greg Clayton78a07bf2016-12-21 21:37:06 +00001023 C,
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001024 C1,
1025 C2,
1026 D,
1027 D1
Greg Clayton78a07bf2016-12-21 21:37:06 +00001028 };
1029
1030 // Scope to allow us to re-use the same DIE names
1031 {
1032 // Create DWARF tree that looks like:
1033 //
1034 // CU
1035 // A
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001036 // B
1037 // C
1038 // C1
1039 // C2
1040 // D
1041 // D1
Greg Clayton78a07bf2016-12-21 21:37:06 +00001042 dwarfgen::DIE CUDie = CU.getUnitDIE();
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001043 dwarfgen::DIE A = CUDie.addChild((dwarf::Tag)Tag::A);
1044 A.addChild((dwarf::Tag)Tag::B);
1045 dwarfgen::DIE C = A.addChild((dwarf::Tag)Tag::C);
1046 dwarfgen::DIE D = A.addChild((dwarf::Tag)Tag::D);
Greg Clayton78a07bf2016-12-21 21:37:06 +00001047 C.addChild((dwarf::Tag)Tag::C1);
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001048 C.addChild((dwarf::Tag)Tag::C2);
1049 D.addChild((dwarf::Tag)Tag::D1);
Greg Clayton78a07bf2016-12-21 21:37:06 +00001050 }
1051
1052 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1053 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1054 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +00001055 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
George Rimar002655d2017-06-28 08:26:57 +00001056
Greg Clayton78a07bf2016-12-21 21:37:06 +00001057 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +00001058 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton78a07bf2016-12-21 21:37:06 +00001059 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +00001060 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
George Rimar002655d2017-06-28 08:26:57 +00001061
Greg Clayton78a07bf2016-12-21 21:37:06 +00001062 // Get the compile unit DIE is valid.
1063 auto CUDie = U->getUnitDIE(false);
1064 EXPECT_TRUE(CUDie.isValid());
George Rimar002655d2017-06-28 08:26:57 +00001065
Greg Clayton78a07bf2016-12-21 21:37:06 +00001066 // The compile unit doesn't have a parent or a sibling.
1067 auto ParentDie = CUDie.getParent();
1068 EXPECT_FALSE(ParentDie.isValid());
1069 auto SiblingDie = CUDie.getSibling();
1070 EXPECT_FALSE(SiblingDie.isValid());
George Rimar002655d2017-06-28 08:26:57 +00001071
Greg Clayton78a07bf2016-12-21 21:37:06 +00001072 // Get the children of the compile unit
1073 auto A = CUDie.getFirstChild();
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001074 auto B = A.getFirstChild();
Greg Clayton78a07bf2016-12-21 21:37:06 +00001075 auto C = B.getSibling();
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001076 auto D = C.getSibling();
1077 auto Null = D.getSibling();
George Rimar002655d2017-06-28 08:26:57 +00001078
Greg Clayton78a07bf2016-12-21 21:37:06 +00001079 // Verify NULL Die is NULL and has no children or siblings
1080 EXPECT_TRUE(Null.isNULL());
1081 EXPECT_FALSE(Null.getSibling().isValid());
1082 EXPECT_FALSE(Null.getFirstChild().isValid());
George Rimar002655d2017-06-28 08:26:57 +00001083
Greg Clayton78a07bf2016-12-21 21:37:06 +00001084 // Verify all children of the compile unit DIE are correct.
1085 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A);
1086 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B);
1087 EXPECT_EQ(C.getTag(), (dwarf::Tag)Tag::C);
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001088 EXPECT_EQ(D.getTag(), (dwarf::Tag)Tag::D);
Greg Clayton78a07bf2016-12-21 21:37:06 +00001089
1090 // Verify who has children
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001091 EXPECT_TRUE(A.hasChildren());
1092 EXPECT_FALSE(B.hasChildren());
1093 EXPECT_TRUE(C.hasChildren());
1094 EXPECT_TRUE(D.hasChildren());
Greg Clayton78a07bf2016-12-21 21:37:06 +00001095
1096 // Make sure the parent of all the children of the compile unit are the
1097 // compile unit.
1098 EXPECT_EQ(A.getParent(), CUDie);
George Rimar002655d2017-06-28 08:26:57 +00001099
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001100 // Make sure the parent of all the children of A are the A.
1101 // B is the first child in A, so we need to verify we can get the previous
1102 // DIE as the parent.
1103 EXPECT_EQ(B.getParent(), A);
1104 // C is the second child in A, so we need to make sure we can backup across
1105 // other DIE (B) at the same level to get the correct parent.
1106 EXPECT_EQ(C.getParent(), A);
1107 // D is the third child of A. We need to verify we can backup across other DIE
1108 // (B and C) including DIE that have children (D) to get the correct parent.
1109 EXPECT_EQ(D.getParent(), A);
Greg Clayton78a07bf2016-12-21 21:37:06 +00001110
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001111 // Verify that a DIE with no children returns an invalid DWARFDie.
1112 EXPECT_FALSE(B.getFirstChild().isValid());
Greg Clayton78a07bf2016-12-21 21:37:06 +00001113
1114 // Verify the children of the B DIE
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001115 auto C1 = C.getFirstChild();
1116 auto C2 = C1.getSibling();
1117 EXPECT_TRUE(C2.getSibling().isNULL());
George Rimar002655d2017-06-28 08:26:57 +00001118
Greg Clayton78a07bf2016-12-21 21:37:06 +00001119 // Verify all children of the B DIE correctly valid or invalid.
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001120 EXPECT_EQ(C1.getTag(), (dwarf::Tag)Tag::C1);
1121 EXPECT_EQ(C2.getTag(), (dwarf::Tag)Tag::C2);
Greg Clayton78a07bf2016-12-21 21:37:06 +00001122
1123 // Make sure the parent of all the children of the B are the B.
Greg Claytona9ef7ee2017-01-04 00:10:50 +00001124 EXPECT_EQ(C1.getParent(), C);
1125 EXPECT_EQ(C2.getParent(), C);
Jonas Devlieghere3f27e572018-07-11 17:11:11 +00001126
1127 // Make sure bidirectional iterator works as expected.
1128 auto Begin = A.begin();
1129 auto End = A.end();
1130 auto It = A.begin();
1131
1132 EXPECT_EQ(It, Begin);
1133 EXPECT_EQ(*It, B);
1134 ++It;
1135 EXPECT_EQ(*It, C);
1136 ++It;
1137 EXPECT_EQ(*It, D);
1138 ++It;
1139 EXPECT_EQ(It, End);
1140 --It;
1141 EXPECT_EQ(*It, D);
1142 --It;
1143 EXPECT_EQ(*It, C);
1144 --It;
1145 EXPECT_EQ(*It, B);
1146 EXPECT_EQ(It, Begin);
Greg Clayton78a07bf2016-12-21 21:37:06 +00001147}
1148
1149TEST(DWARFDebugInfo, TestDWARFDie) {
Greg Clayton78a07bf2016-12-21 21:37:06 +00001150 // Make sure a default constructed DWARFDie doesn't have any parent, sibling
1151 // or child;
1152 DWARFDie DefaultDie;
1153 EXPECT_FALSE(DefaultDie.getParent().isValid());
1154 EXPECT_FALSE(DefaultDie.getFirstChild().isValid());
1155 EXPECT_FALSE(DefaultDie.getSibling().isValid());
1156}
1157
Greg Clayton93e4fe82017-01-05 23:47:37 +00001158TEST(DWARFDebugInfo, TestChildIterators) {
George Rimard8508b02017-06-30 10:09:01 +00001159 Triple Triple = getHostTripleForAddrSize(sizeof(void *));
1160 if (!isConfigurationSupported(Triple))
1161 return;
1162
Greg Clayton93e4fe82017-01-05 23:47:37 +00001163 // Test the DWARF APIs related to iterating across the children of a DIE using
1164 // the DWARFDie::iterator class.
1165 uint16_t Version = 4;
Greg Clayton93e4fe82017-01-05 23:47:37 +00001166 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
George Rimard8508b02017-06-30 10:09:01 +00001167 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Clayton93e4fe82017-01-05 23:47:37 +00001168 dwarfgen::Generator *DG = ExpectedDG.get().get();
1169 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
George Rimar002655d2017-06-28 08:26:57 +00001170
Greg Clayton93e4fe82017-01-05 23:47:37 +00001171 enum class Tag: uint16_t {
1172 A = dwarf::DW_TAG_lo_user,
1173 B,
1174 };
George Rimar002655d2017-06-28 08:26:57 +00001175
Greg Clayton93e4fe82017-01-05 23:47:37 +00001176 // Scope to allow us to re-use the same DIE names
1177 {
1178 // Create DWARF tree that looks like:
1179 //
1180 // CU
1181 // A
1182 // B
1183 auto CUDie = CU.getUnitDIE();
1184 CUDie.addChild((dwarf::Tag)Tag::A);
1185 CUDie.addChild((dwarf::Tag)Tag::B);
1186 }
George Rimar002655d2017-06-28 08:26:57 +00001187
Greg Clayton93e4fe82017-01-05 23:47:37 +00001188 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1189 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1190 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +00001191 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
George Rimar002655d2017-06-28 08:26:57 +00001192
Greg Clayton93e4fe82017-01-05 23:47:37 +00001193 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +00001194 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton93e4fe82017-01-05 23:47:37 +00001195 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +00001196 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
George Rimar002655d2017-06-28 08:26:57 +00001197
Greg Clayton93e4fe82017-01-05 23:47:37 +00001198 // Get the compile unit DIE is valid.
1199 auto CUDie = U->getUnitDIE(false);
1200 EXPECT_TRUE(CUDie.isValid());
Greg Clayton93e4fe82017-01-05 23:47:37 +00001201 uint32_t Index;
1202 DWARFDie A;
1203 DWARFDie B;
George Rimar002655d2017-06-28 08:26:57 +00001204
Greg Clayton93e4fe82017-01-05 23:47:37 +00001205 // Verify the compile unit DIE's children.
1206 Index = 0;
1207 for (auto Die : CUDie.children()) {
1208 switch (Index++) {
1209 case 0: A = Die; break;
1210 case 1: B = Die; break;
1211 }
1212 }
George Rimar002655d2017-06-28 08:26:57 +00001213
Greg Clayton93e4fe82017-01-05 23:47:37 +00001214 EXPECT_EQ(A.getTag(), (dwarf::Tag)Tag::A);
1215 EXPECT_EQ(B.getTag(), (dwarf::Tag)Tag::B);
1216
1217 // Verify that A has no children by verifying that the begin and end contain
1218 // invalid DIEs and also that the iterators are equal.
1219 EXPECT_EQ(A.begin(), A.end());
1220}
1221
1222TEST(DWARFDebugInfo, TestChildIteratorsOnInvalidDie) {
1223 // Verify that an invalid DIE has no children.
1224 DWARFDie Invalid;
1225 auto begin = Invalid.begin();
1226 auto end = Invalid.end();
1227 EXPECT_FALSE(begin->isValid());
1228 EXPECT_FALSE(end->isValid());
1229 EXPECT_EQ(begin, end);
1230}
1231
Greg Clayton93e4fe82017-01-05 23:47:37 +00001232TEST(DWARFDebugInfo, TestEmptyChildren) {
Chris Bieneman2e752db2017-01-20 19:03:14 +00001233 const char *yamldata = "debug_abbrev:\n"
1234 " - Code: 0x00000001\n"
1235 " Tag: DW_TAG_compile_unit\n"
1236 " Children: DW_CHILDREN_yes\n"
1237 " Attributes:\n"
1238 "debug_info:\n"
Chris Bienemanfaf1feb2017-03-03 21:11:55 +00001239 " - Length:\n"
Jonas Devlieghere5c709ed2018-04-20 12:33:49 +00001240 " TotalLength: 0\n"
Chris Bieneman2e752db2017-01-20 19:03:14 +00001241 " Version: 4\n"
1242 " AbbrOffset: 0\n"
1243 " AddrSize: 8\n"
1244 " Entries:\n"
1245 " - AbbrCode: 0x00000001\n"
1246 " Values:\n"
1247 " - AbbrCode: 0x00000000\n"
1248 " Values:\n";
1249
Jonas Devlieghere5c709ed2018-04-20 12:33:49 +00001250 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata), true);
Chris Bieneman977db092017-01-23 16:49:34 +00001251 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00001252 std::unique_ptr<DWARFContext> DwarfContext =
1253 DWARFContext::create(*ErrOrSections, 8);
Chris Bieneman2e752db2017-01-20 19:03:14 +00001254
Greg Clayton93e4fe82017-01-05 23:47:37 +00001255 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +00001256 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton93e4fe82017-01-05 23:47:37 +00001257 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +00001258 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
Chris Bieneman2e752db2017-01-20 19:03:14 +00001259
Greg Clayton93e4fe82017-01-05 23:47:37 +00001260 // Get the compile unit DIE is valid.
1261 auto CUDie = U->getUnitDIE(false);
1262 EXPECT_TRUE(CUDie.isValid());
George Rimar002655d2017-06-28 08:26:57 +00001263
Greg Clayton93e4fe82017-01-05 23:47:37 +00001264 // Verify that the CU Die that says it has children, but doesn't, actually
1265 // has begin and end iterators that are equal. We want to make sure we don't
1266 // see the Null DIEs during iteration.
1267 EXPECT_EQ(CUDie.begin(), CUDie.end());
1268}
Greg Clayton2520c9e2016-12-19 20:36:41 +00001269
Greg Clayton0e62ee72017-01-13 00:13:42 +00001270TEST(DWARFDebugInfo, TestAttributeIterators) {
George Rimard8508b02017-06-30 10:09:01 +00001271 Triple Triple = getHostTripleForAddrSize(sizeof(void *));
1272 if (!isConfigurationSupported(Triple))
1273 return;
1274
Greg Clayton0e62ee72017-01-13 00:13:42 +00001275 // Test the DWARF APIs related to iterating across all attribute values in a
1276 // a DWARFDie.
1277 uint16_t Version = 4;
Greg Clayton0e62ee72017-01-13 00:13:42 +00001278 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
George Rimard8508b02017-06-30 10:09:01 +00001279 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Clayton0e62ee72017-01-13 00:13:42 +00001280 dwarfgen::Generator *DG = ExpectedDG.get().get();
1281 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1282 const uint64_t CULowPC = 0x1000;
1283 StringRef CUPath("/tmp/main.c");
George Rimar002655d2017-06-28 08:26:57 +00001284
Greg Clayton0e62ee72017-01-13 00:13:42 +00001285 // Scope to allow us to re-use the same DIE names
1286 {
1287 auto CUDie = CU.getUnitDIE();
1288 // Encode an attribute value before an attribute with no data.
1289 CUDie.addAttribute(DW_AT_name, DW_FORM_strp, CUPath.data());
1290 // Encode an attribute value with no data in .debug_info/types to ensure
1291 // the iteration works correctly.
1292 CUDie.addAttribute(DW_AT_declaration, DW_FORM_flag_present);
1293 // Encode an attribute value after an attribute with no data.
1294 CUDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, CULowPC);
1295 }
George Rimar002655d2017-06-28 08:26:57 +00001296
Greg Clayton0e62ee72017-01-13 00:13:42 +00001297 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1298 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1299 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +00001300 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
George Rimar002655d2017-06-28 08:26:57 +00001301
Greg Clayton0e62ee72017-01-13 00:13:42 +00001302 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +00001303 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton0e62ee72017-01-13 00:13:42 +00001304 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +00001305 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
George Rimar002655d2017-06-28 08:26:57 +00001306
Greg Clayton0e62ee72017-01-13 00:13:42 +00001307 // Get the compile unit DIE is valid.
1308 auto CUDie = U->getUnitDIE(false);
1309 EXPECT_TRUE(CUDie.isValid());
George Rimar002655d2017-06-28 08:26:57 +00001310
Greg Clayton0e62ee72017-01-13 00:13:42 +00001311 auto R = CUDie.attributes();
1312 auto I = R.begin();
1313 auto E = R.end();
George Rimar002655d2017-06-28 08:26:57 +00001314
Greg Clayton0e62ee72017-01-13 00:13:42 +00001315 ASSERT_NE(E, I);
1316 EXPECT_EQ(I->Attr, DW_AT_name);
1317 auto ActualCUPath = I->Value.getAsCString();
1318 EXPECT_EQ(CUPath, *ActualCUPath);
George Rimar002655d2017-06-28 08:26:57 +00001319
Greg Clayton0e62ee72017-01-13 00:13:42 +00001320 ASSERT_NE(E, ++I);
1321 EXPECT_EQ(I->Attr, DW_AT_declaration);
Greg Clayton71503432017-01-13 00:23:59 +00001322 EXPECT_EQ(1ull, *I->Value.getAsUnsignedConstant());
George Rimar002655d2017-06-28 08:26:57 +00001323
Greg Clayton0e62ee72017-01-13 00:13:42 +00001324 ASSERT_NE(E, ++I);
1325 EXPECT_EQ(I->Attr, DW_AT_low_pc);
Greg Clayton71503432017-01-13 00:23:59 +00001326 EXPECT_EQ(CULowPC, *I->Value.getAsAddress());
George Rimar002655d2017-06-28 08:26:57 +00001327
Greg Clayton0e62ee72017-01-13 00:13:42 +00001328 EXPECT_EQ(E, ++I);
1329}
1330
Greg Clayton97d22182017-01-13 21:08:18 +00001331TEST(DWARFDebugInfo, TestFindRecurse) {
George Rimard8508b02017-06-30 10:09:01 +00001332 Triple Triple = getHostTripleForAddrSize(sizeof(void *));
1333 if (!isConfigurationSupported(Triple))
Greg Clayton97d22182017-01-13 21:08:18 +00001334 return;
George Rimard8508b02017-06-30 10:09:01 +00001335
1336 uint16_t Version = 4;
1337 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1338 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Clayton97d22182017-01-13 21:08:18 +00001339 dwarfgen::Generator *DG = ExpectedDG.get().get();
1340 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
George Rimar002655d2017-06-28 08:26:57 +00001341
David Blaikie1914c822017-03-13 21:46:37 +00001342 StringRef SpecDieName = "spec";
1343 StringRef SpecLinkageName = "spec_linkage";
1344 StringRef AbsDieName = "abs";
Greg Clayton97d22182017-01-13 21:08:18 +00001345 // Scope to allow us to re-use the same DIE names
1346 {
Greg Clayton97d22182017-01-13 21:08:18 +00001347 auto CUDie = CU.getUnitDIE();
1348 auto FuncSpecDie = CUDie.addChild(DW_TAG_subprogram);
David Blaikie1914c822017-03-13 21:46:37 +00001349 auto FuncAbsDie = CUDie.addChild(DW_TAG_subprogram);
Greg Claytond6b67eb2017-11-27 22:12:44 +00001350 // Put the linkage name in a second abstract origin DIE to ensure we
1351 // recurse through more than just one DIE when looking for attributes.
1352 auto FuncAbsDie2 = CUDie.addChild(DW_TAG_subprogram);
Greg Clayton97d22182017-01-13 21:08:18 +00001353 auto FuncDie = CUDie.addChild(DW_TAG_subprogram);
1354 auto VarAbsDie = CUDie.addChild(DW_TAG_variable);
1355 auto VarDie = CUDie.addChild(DW_TAG_variable);
1356 FuncSpecDie.addAttribute(DW_AT_name, DW_FORM_strp, SpecDieName);
Greg Claytond6b67eb2017-11-27 22:12:44 +00001357 FuncAbsDie2.addAttribute(DW_AT_linkage_name, DW_FORM_strp, SpecLinkageName);
David Blaikie1914c822017-03-13 21:46:37 +00001358 FuncAbsDie.addAttribute(DW_AT_specification, DW_FORM_ref4, FuncSpecDie);
Greg Claytond6b67eb2017-11-27 22:12:44 +00001359 FuncAbsDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, FuncAbsDie2);
David Blaikie1914c822017-03-13 21:46:37 +00001360 FuncDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, FuncAbsDie);
Greg Clayton97d22182017-01-13 21:08:18 +00001361 VarAbsDie.addAttribute(DW_AT_name, DW_FORM_strp, AbsDieName);
1362 VarDie.addAttribute(DW_AT_abstract_origin, DW_FORM_ref4, VarAbsDie);
1363 }
George Rimar002655d2017-06-28 08:26:57 +00001364
Greg Clayton97d22182017-01-13 21:08:18 +00001365 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1366 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1367 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +00001368 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
George Rimar002655d2017-06-28 08:26:57 +00001369
Greg Clayton97d22182017-01-13 21:08:18 +00001370 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +00001371 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Clayton97d22182017-01-13 21:08:18 +00001372 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +00001373 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
George Rimar002655d2017-06-28 08:26:57 +00001374
Greg Clayton97d22182017-01-13 21:08:18 +00001375 // Get the compile unit DIE is valid.
1376 auto CUDie = U->getUnitDIE(false);
1377 EXPECT_TRUE(CUDie.isValid());
George Rimar002655d2017-06-28 08:26:57 +00001378
Greg Clayton97d22182017-01-13 21:08:18 +00001379 auto FuncSpecDie = CUDie.getFirstChild();
David Blaikie1914c822017-03-13 21:46:37 +00001380 auto FuncAbsDie = FuncSpecDie.getSibling();
Greg Claytond6b67eb2017-11-27 22:12:44 +00001381 auto FuncAbsDie2 = FuncAbsDie.getSibling();
1382 auto FuncDie = FuncAbsDie2.getSibling();
Greg Clayton97d22182017-01-13 21:08:18 +00001383 auto VarAbsDie = FuncDie.getSibling();
1384 auto VarDie = VarAbsDie.getSibling();
1385
1386 // Make sure we can't extract the name from the specification die when using
1387 // DWARFDie::find() since it won't check the DW_AT_specification DIE.
David Blaikie1914c822017-03-13 21:46:37 +00001388 EXPECT_FALSE(FuncDie.find(DW_AT_name));
Greg Clayton97d22182017-01-13 21:08:18 +00001389
1390 // Make sure we can extract the name from the specification die when using
1391 // DWARFDie::findRecursively() since it should recurse through the
1392 // DW_AT_specification DIE.
1393 auto NameOpt = FuncDie.findRecursively(DW_AT_name);
David Blaikie1914c822017-03-13 21:46:37 +00001394 EXPECT_TRUE(NameOpt);
Greg Clayton97d22182017-01-13 21:08:18 +00001395 // Test the dwarf::toString() helper function.
1396 auto StringOpt = toString(NameOpt);
David Blaikie1914c822017-03-13 21:46:37 +00001397 EXPECT_TRUE(StringOpt);
Greg Clayton97d22182017-01-13 21:08:18 +00001398 EXPECT_EQ(SpecDieName, StringOpt.getValueOr(nullptr));
1399 // Test the dwarf::toString() helper function with a default value specified.
1400 EXPECT_EQ(SpecDieName, toString(NameOpt, nullptr));
David Blaikie1914c822017-03-13 21:46:37 +00001401
1402 auto LinkageNameOpt = FuncDie.findRecursively(DW_AT_linkage_name);
1403 EXPECT_EQ(SpecLinkageName, toString(LinkageNameOpt).getValueOr(nullptr));
George Rimar002655d2017-06-28 08:26:57 +00001404
Greg Clayton97d22182017-01-13 21:08:18 +00001405 // Make sure we can't extract the name from the abstract origin die when using
1406 // DWARFDie::find() since it won't check the DW_AT_abstract_origin DIE.
David Blaikie1914c822017-03-13 21:46:37 +00001407 EXPECT_FALSE(VarDie.find(DW_AT_name));
George Rimar002655d2017-06-28 08:26:57 +00001408
Greg Clayton97d22182017-01-13 21:08:18 +00001409 // Make sure we can extract the name from the abstract origin die when using
1410 // DWARFDie::findRecursively() since it should recurse through the
1411 // DW_AT_abstract_origin DIE.
1412 NameOpt = VarDie.findRecursively(DW_AT_name);
David Blaikie1914c822017-03-13 21:46:37 +00001413 EXPECT_TRUE(NameOpt);
Greg Clayton97d22182017-01-13 21:08:18 +00001414 // Test the dwarf::toString() helper function.
1415 StringOpt = toString(NameOpt);
David Blaikie1914c822017-03-13 21:46:37 +00001416 EXPECT_TRUE(StringOpt);
Greg Clayton97d22182017-01-13 21:08:18 +00001417 EXPECT_EQ(AbsDieName, StringOpt.getValueOr(nullptr));
Greg Clayton97d22182017-01-13 21:08:18 +00001418}
1419
1420TEST(DWARFDebugInfo, TestDwarfToFunctions) {
1421 // Test all of the dwarf::toXXX functions that take a
1422 // Optional<DWARFFormValue> and extract the values from it.
1423 DWARFFormValue FormVal;
1424 uint64_t InvalidU64 = 0xBADBADBADBADBADB;
1425 int64_t InvalidS64 = 0xBADBADBADBADBADB;
1426 // First test that we don't get valid values back when using an optional with
1427 // no value.
1428 Optional<DWARFFormValue> FormValOpt;
1429 EXPECT_FALSE(toString(FormValOpt).hasValue());
1430 EXPECT_FALSE(toUnsigned(FormValOpt).hasValue());
1431 EXPECT_FALSE(toReference(FormValOpt).hasValue());
1432 EXPECT_FALSE(toSigned(FormValOpt).hasValue());
1433 EXPECT_FALSE(toAddress(FormValOpt).hasValue());
1434 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue());
1435 EXPECT_FALSE(toBlock(FormValOpt).hasValue());
1436 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr));
1437 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt, InvalidU64));
1438 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64));
1439 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64));
1440 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64));
1441 EXPECT_EQ(InvalidS64, toSigned(FormValOpt, InvalidS64));
1442
Greg Clayton97d22182017-01-13 21:08:18 +00001443 // Test successful and unsuccessful address decoding.
1444 uint64_t Address = 0x100000000ULL;
1445 FormVal.setForm(DW_FORM_addr);
1446 FormVal.setUValue(Address);
1447 FormValOpt = FormVal;
1448
1449 EXPECT_FALSE(toString(FormValOpt).hasValue());
1450 EXPECT_FALSE(toUnsigned(FormValOpt).hasValue());
1451 EXPECT_FALSE(toReference(FormValOpt).hasValue());
1452 EXPECT_FALSE(toSigned(FormValOpt).hasValue());
1453 EXPECT_TRUE(toAddress(FormValOpt).hasValue());
1454 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue());
1455 EXPECT_FALSE(toBlock(FormValOpt).hasValue());
1456 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr));
1457 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt, InvalidU64));
1458 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64));
1459 EXPECT_EQ(Address, toAddress(FormValOpt, InvalidU64));
1460 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64));
1461 EXPECT_EQ(InvalidS64, toSigned(FormValOpt, InvalidU64));
1462
1463 // Test successful and unsuccessful unsigned constant decoding.
1464 uint64_t UData8 = 0x1020304050607080ULL;
1465 FormVal.setForm(DW_FORM_udata);
1466 FormVal.setUValue(UData8);
1467 FormValOpt = FormVal;
George Rimar002655d2017-06-28 08:26:57 +00001468
Greg Clayton97d22182017-01-13 21:08:18 +00001469 EXPECT_FALSE(toString(FormValOpt).hasValue());
1470 EXPECT_TRUE(toUnsigned(FormValOpt).hasValue());
1471 EXPECT_FALSE(toReference(FormValOpt).hasValue());
1472 EXPECT_TRUE(toSigned(FormValOpt).hasValue());
1473 EXPECT_FALSE(toAddress(FormValOpt).hasValue());
1474 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue());
1475 EXPECT_FALSE(toBlock(FormValOpt).hasValue());
1476 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr));
1477 EXPECT_EQ(UData8, toUnsigned(FormValOpt, InvalidU64));
1478 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64));
1479 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64));
1480 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64));
1481 EXPECT_EQ((int64_t)UData8, toSigned(FormValOpt, InvalidU64));
1482
1483 // Test successful and unsuccessful reference decoding.
1484 uint32_t RefData = 0x11223344U;
1485 FormVal.setForm(DW_FORM_ref_addr);
1486 FormVal.setUValue(RefData);
1487 FormValOpt = FormVal;
George Rimar002655d2017-06-28 08:26:57 +00001488
Greg Clayton97d22182017-01-13 21:08:18 +00001489 EXPECT_FALSE(toString(FormValOpt).hasValue());
1490 EXPECT_FALSE(toUnsigned(FormValOpt).hasValue());
1491 EXPECT_TRUE(toReference(FormValOpt).hasValue());
1492 EXPECT_FALSE(toSigned(FormValOpt).hasValue());
1493 EXPECT_FALSE(toAddress(FormValOpt).hasValue());
1494 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue());
1495 EXPECT_FALSE(toBlock(FormValOpt).hasValue());
1496 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr));
1497 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt, InvalidU64));
1498 EXPECT_EQ(RefData, toReference(FormValOpt, InvalidU64));
1499 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64));
1500 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64));
1501 EXPECT_EQ(InvalidS64, toSigned(FormValOpt, InvalidU64));
1502
1503 // Test successful and unsuccessful signed constant decoding.
1504 int64_t SData8 = 0x1020304050607080ULL;
1505 FormVal.setForm(DW_FORM_udata);
1506 FormVal.setSValue(SData8);
1507 FormValOpt = FormVal;
George Rimar002655d2017-06-28 08:26:57 +00001508
Greg Clayton97d22182017-01-13 21:08:18 +00001509 EXPECT_FALSE(toString(FormValOpt).hasValue());
1510 EXPECT_TRUE(toUnsigned(FormValOpt).hasValue());
1511 EXPECT_FALSE(toReference(FormValOpt).hasValue());
1512 EXPECT_TRUE(toSigned(FormValOpt).hasValue());
1513 EXPECT_FALSE(toAddress(FormValOpt).hasValue());
1514 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue());
1515 EXPECT_FALSE(toBlock(FormValOpt).hasValue());
1516 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr));
1517 EXPECT_EQ((uint64_t)SData8, toUnsigned(FormValOpt, InvalidU64));
1518 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64));
1519 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64));
1520 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64));
1521 EXPECT_EQ(SData8, toSigned(FormValOpt, InvalidU64));
1522
1523 // Test successful and unsuccessful block decoding.
1524 uint8_t Data[] = { 2, 3, 4 };
1525 ArrayRef<uint8_t> Array(Data);
1526 FormVal.setForm(DW_FORM_block1);
1527 FormVal.setBlockValue(Array);
1528 FormValOpt = FormVal;
George Rimar002655d2017-06-28 08:26:57 +00001529
Greg Clayton97d22182017-01-13 21:08:18 +00001530 EXPECT_FALSE(toString(FormValOpt).hasValue());
1531 EXPECT_FALSE(toUnsigned(FormValOpt).hasValue());
1532 EXPECT_FALSE(toReference(FormValOpt).hasValue());
1533 EXPECT_FALSE(toSigned(FormValOpt).hasValue());
1534 EXPECT_FALSE(toAddress(FormValOpt).hasValue());
1535 EXPECT_FALSE(toSectionOffset(FormValOpt).hasValue());
1536 auto BlockOpt = toBlock(FormValOpt);
1537 EXPECT_TRUE(BlockOpt.hasValue());
1538 EXPECT_EQ(*BlockOpt, Array);
1539 EXPECT_EQ(nullptr, toString(FormValOpt, nullptr));
1540 EXPECT_EQ(InvalidU64, toUnsigned(FormValOpt, InvalidU64));
1541 EXPECT_EQ(InvalidU64, toReference(FormValOpt, InvalidU64));
1542 EXPECT_EQ(InvalidU64, toAddress(FormValOpt, InvalidU64));
1543 EXPECT_EQ(InvalidU64, toSectionOffset(FormValOpt, InvalidU64));
1544 EXPECT_EQ(InvalidS64, toSigned(FormValOpt, InvalidU64));
1545
1546 // Test
1547}
1548
Greg Claytonc109bbe2017-01-13 22:32:12 +00001549TEST(DWARFDebugInfo, TestFindAttrs) {
George Rimard8508b02017-06-30 10:09:01 +00001550 Triple Triple = getHostTripleForAddrSize(sizeof(void *));
1551 if (!isConfigurationSupported(Triple))
1552 return;
1553
Greg Claytonc109bbe2017-01-13 22:32:12 +00001554 // Test the DWARFDie::find() and DWARFDie::findRecursively() that take an
1555 // ArrayRef<dwarf::Attribute> value to make sure they work correctly.
1556 uint16_t Version = 4;
Greg Claytonc109bbe2017-01-13 22:32:12 +00001557 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
George Rimard8508b02017-06-30 10:09:01 +00001558 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Greg Claytonc109bbe2017-01-13 22:32:12 +00001559 dwarfgen::Generator *DG = ExpectedDG.get().get();
1560 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
George Rimar002655d2017-06-28 08:26:57 +00001561
Greg Claytonc109bbe2017-01-13 22:32:12 +00001562 StringRef DieMangled("_Z3fooi");
1563 // Scope to allow us to re-use the same DIE names
1564 {
Greg Claytonc109bbe2017-01-13 22:32:12 +00001565 auto CUDie = CU.getUnitDIE();
1566 auto FuncSpecDie = CUDie.addChild(DW_TAG_subprogram);
1567 auto FuncDie = CUDie.addChild(DW_TAG_subprogram);
1568 FuncSpecDie.addAttribute(DW_AT_MIPS_linkage_name, DW_FORM_strp, DieMangled);
1569 FuncDie.addAttribute(DW_AT_specification, DW_FORM_ref4, FuncSpecDie);
1570 }
George Rimar002655d2017-06-28 08:26:57 +00001571
Greg Claytonc109bbe2017-01-13 22:32:12 +00001572 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1573 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1574 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +00001575 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
George Rimar002655d2017-06-28 08:26:57 +00001576
Greg Claytonc109bbe2017-01-13 22:32:12 +00001577 // Verify the number of compile units is correct.
Rafael Espindolac398e672017-07-19 22:27:28 +00001578 uint32_t NumCUs = DwarfContext->getNumCompileUnits();
Greg Claytonc109bbe2017-01-13 22:32:12 +00001579 EXPECT_EQ(NumCUs, 1u);
Rafael Espindolac398e672017-07-19 22:27:28 +00001580 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
George Rimar002655d2017-06-28 08:26:57 +00001581
Greg Claytonc109bbe2017-01-13 22:32:12 +00001582 // Get the compile unit DIE is valid.
1583 auto CUDie = U->getUnitDIE(false);
1584 EXPECT_TRUE(CUDie.isValid());
George Rimar002655d2017-06-28 08:26:57 +00001585
Greg Claytonc109bbe2017-01-13 22:32:12 +00001586 auto FuncSpecDie = CUDie.getFirstChild();
1587 auto FuncDie = FuncSpecDie.getSibling();
1588
1589 // Make sure that passing in an empty attribute list behave correctly.
1590 EXPECT_FALSE(FuncDie.find(ArrayRef<dwarf::Attribute>()).hasValue());
1591
1592 // Make sure that passing in a list of attribute that are not contained
1593 // in the DIE returns nothing.
1594 EXPECT_FALSE(FuncDie.find({DW_AT_low_pc, DW_AT_entry_pc}).hasValue());
1595
NAKAMURA Takumif2b135a2017-01-16 14:33:37 +00001596 const dwarf::Attribute Attrs[] = {DW_AT_linkage_name,
1597 DW_AT_MIPS_linkage_name};
1598
Greg Claytonc109bbe2017-01-13 22:32:12 +00001599 // Make sure we can't extract the linkage name attributes when using
1600 // DWARFDie::find() since it won't check the DW_AT_specification DIE.
1601 EXPECT_FALSE(FuncDie.find(Attrs).hasValue());
George Rimar002655d2017-06-28 08:26:57 +00001602
Greg Claytonc109bbe2017-01-13 22:32:12 +00001603 // Make sure we can extract the name from the specification die when using
1604 // DWARFDie::findRecursively() since it should recurse through the
1605 // DW_AT_specification DIE.
1606 auto NameOpt = FuncDie.findRecursively(Attrs);
1607 EXPECT_TRUE(NameOpt.hasValue());
1608 EXPECT_EQ(DieMangled, toString(NameOpt, ""));
Greg Claytonc109bbe2017-01-13 22:32:12 +00001609}
1610
Victor Leschukd7bfa402017-03-01 22:13:42 +00001611TEST(DWARFDebugInfo, TestImplicitConstAbbrevs) {
George Rimard8508b02017-06-30 10:09:01 +00001612 Triple Triple = getHostTripleForAddrSize(sizeof(void *));
1613 if (!isConfigurationSupported(Triple))
Victor Leschukd7bfa402017-03-01 22:13:42 +00001614 return;
George Rimard8508b02017-06-30 10:09:01 +00001615
1616 uint16_t Version = 5;
1617 auto ExpectedDG = dwarfgen::Generator::create(Triple, Version);
1618 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
Victor Leschukd7bfa402017-03-01 22:13:42 +00001619 dwarfgen::Generator *DG = ExpectedDG.get().get();
1620 dwarfgen::CompileUnit &CU = DG->addCompileUnit();
1621 dwarfgen::DIE CUDie = CU.getUnitDIE();
1622 const dwarf::Attribute Attr = DW_AT_lo_user;
1623 const int64_t Val1 = 42;
1624 const int64_t Val2 = 43;
1625
1626 auto FirstVal1DIE = CUDie.addChild(DW_TAG_class_type);
1627 FirstVal1DIE.addAttribute(Attr, DW_FORM_implicit_const, Val1);
1628
1629 auto SecondVal1DIE = CUDie.addChild(DW_TAG_class_type);
1630 SecondVal1DIE.addAttribute(Attr, DW_FORM_implicit_const, Val1);
1631
1632 auto Val2DIE = CUDie.addChild(DW_TAG_class_type);
1633 Val2DIE.addAttribute(Attr, DW_FORM_implicit_const, Val2);
1634
1635 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
1636 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
1637 EXPECT_TRUE((bool)Obj);
Rafael Espindolac398e672017-07-19 22:27:28 +00001638 std::unique_ptr<DWARFContext> DwarfContext = DWARFContext::create(**Obj);
1639 DWARFCompileUnit *U = DwarfContext->getCompileUnitAtIndex(0);
Victor Leschukd7bfa402017-03-01 22:13:42 +00001640 EXPECT_TRUE((bool)U);
1641
1642 const auto *Abbrevs = U->getAbbreviations();
1643 EXPECT_TRUE((bool)Abbrevs);
1644
1645 // Let's find implicit_const abbrevs and verify,
1646 // that there are exactly two of them and both of them
1647 // can be dumped correctly.
1648 typedef decltype(Abbrevs->begin()) AbbrevIt;
1649 AbbrevIt Val1Abbrev = Abbrevs->end();
1650 AbbrevIt Val2Abbrev = Abbrevs->end();
1651 for(auto it = Abbrevs->begin(); it != Abbrevs->end(); ++it) {
1652 if (it->getNumAttributes() == 0)
1653 continue; // root abbrev for DW_TAG_compile_unit
1654
1655 auto A = it->getAttrByIndex(0);
1656 EXPECT_EQ(A, Attr);
1657
1658 auto FormValue = it->getAttributeValue(/* offset */ 0, A, *U);
1659 EXPECT_TRUE((bool)FormValue);
1660 EXPECT_EQ(FormValue->getForm(), dwarf::DW_FORM_implicit_const);
1661
1662 const auto V = FormValue->getAsSignedConstant();
1663 EXPECT_TRUE((bool)V);
1664
1665 auto VerifyAbbrevDump = [&V](AbbrevIt it) {
1666 std::string S;
1667 llvm::raw_string_ostream OS(S);
1668 it->dump(OS);
1669 auto FormPos = OS.str().find("DW_FORM_implicit_const");
1670 EXPECT_NE(FormPos, std::string::npos);
1671 auto ValPos = S.find_first_of("-0123456789", FormPos);
1672 EXPECT_NE(ValPos, std::string::npos);
1673 int64_t Val = std::atoll(S.substr(ValPos).c_str());
1674 EXPECT_EQ(Val, *V);
1675 };
1676
1677 switch(*V) {
1678 case Val1:
1679 EXPECT_EQ(Val1Abbrev, Abbrevs->end());
1680 Val1Abbrev = it;
1681 VerifyAbbrevDump(it);
1682 break;
1683 case Val2:
1684 EXPECT_EQ(Val2Abbrev, Abbrevs->end());
1685 Val2Abbrev = it;
1686 VerifyAbbrevDump(it);
1687 break;
1688 default:
1689 FAIL() << "Unexpected attribute value: " << *V;
1690 }
1691 }
1692
1693 // Now let's make sure that two Val1-DIEs refer to the same abbrev,
1694 // and Val2-DIE refers to another one.
1695 auto DieDG = U->getUnitDIE(false);
1696 auto it = DieDG.begin();
1697 std::multimap<int64_t, decltype(it->getAbbreviationDeclarationPtr())> DIEs;
1698 const DWARFAbbreviationDeclaration *AbbrevPtrVal1 = nullptr;
1699 const DWARFAbbreviationDeclaration *AbbrevPtrVal2 = nullptr;
1700 for (; it != DieDG.end(); ++it) {
1701 const auto *AbbrevPtr = it->getAbbreviationDeclarationPtr();
1702 EXPECT_TRUE((bool)AbbrevPtr);
1703 auto FormValue = it->find(Attr);
1704 EXPECT_TRUE((bool)FormValue);
1705 const auto V = FormValue->getAsSignedConstant();
1706 EXPECT_TRUE((bool)V);
1707 switch(*V) {
1708 case Val1:
1709 AbbrevPtrVal1 = AbbrevPtr;
1710 break;
1711 case Val2:
1712 AbbrevPtrVal2 = AbbrevPtr;
1713 break;
1714 default:
1715 FAIL() << "Unexpected attribute value: " << *V;
1716 }
1717 DIEs.insert(std::make_pair(*V, AbbrevPtr));
1718 }
1719 EXPECT_EQ(DIEs.count(Val1), 2u);
1720 EXPECT_EQ(DIEs.count(Val2), 1u);
1721 auto Val1Range = DIEs.equal_range(Val1);
1722 for (auto it = Val1Range.first; it != Val1Range.second; ++it)
1723 EXPECT_EQ(it->second, AbbrevPtrVal1);
1724 EXPECT_EQ(DIEs.find(Val2)->second, AbbrevPtrVal2);
1725}
1726
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +00001727void VerifyWarning(DWARFContext &DwarfContext, StringRef Error) {
1728 SmallString<1024> Str;
1729 raw_svector_ostream Strm(Str);
Jonas Devliegherec0a758d2017-09-18 14:15:57 +00001730 EXPECT_TRUE(DwarfContext.verify(Strm));
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +00001731 EXPECT_TRUE(Str.str().contains(Error));
1732}
1733
Greg Clayton67070462017-05-02 22:48:52 +00001734void VerifyError(DWARFContext &DwarfContext, StringRef Error) {
1735 SmallString<1024> Str;
1736 raw_svector_ostream Strm(Str);
Jonas Devliegherec0a758d2017-09-18 14:15:57 +00001737 EXPECT_FALSE(DwarfContext.verify(Strm));
Greg Clayton67070462017-05-02 22:48:52 +00001738 EXPECT_TRUE(Str.str().contains(Error));
1739}
1740
Jonas Devlieghere58910602017-09-14 11:33:42 +00001741void VerifySuccess(DWARFContext &DwarfContext) {
1742 SmallString<1024> Str;
1743 raw_svector_ostream Strm(Str);
Jonas Devliegherec0a758d2017-09-18 14:15:57 +00001744 EXPECT_TRUE(DwarfContext.verify(Strm));
Jonas Devlieghere58910602017-09-14 11:33:42 +00001745}
1746
Greg Clayton48432cf2017-05-01 22:07:02 +00001747TEST(DWARFDebugInfo, TestDwarfVerifyInvalidCURef) {
1748 // Create a single compile unit with a single function that has a DW_AT_type
Jonas Devlieghere58910602017-09-14 11:33:42 +00001749 // that is CU relative. The CU offset is not valid because it is larger than
Greg Clayton48432cf2017-05-01 22:07:02 +00001750 // the compile unit itself.
1751
1752 const char *yamldata = R"(
1753 debug_str:
1754 - ''
1755 - /tmp/main.c
1756 - main
1757 debug_abbrev:
1758 - Code: 0x00000001
1759 Tag: DW_TAG_compile_unit
1760 Children: DW_CHILDREN_yes
1761 Attributes:
1762 - Attribute: DW_AT_name
1763 Form: DW_FORM_strp
1764 - Code: 0x00000002
1765 Tag: DW_TAG_subprogram
1766 Children: DW_CHILDREN_no
1767 Attributes:
1768 - Attribute: DW_AT_name
1769 Form: DW_FORM_strp
1770 - Attribute: DW_AT_type
1771 Form: DW_FORM_ref4
1772 debug_info:
1773 - Length:
1774 TotalLength: 22
1775 Version: 4
1776 AbbrOffset: 0
1777 AddrSize: 8
1778 Entries:
1779 - AbbrCode: 0x00000001
1780 Values:
1781 - Value: 0x0000000000000001
1782 - AbbrCode: 0x00000002
1783 Values:
1784 - Value: 0x000000000000000D
1785 - Value: 0x0000000000001234
1786 - AbbrCode: 0x00000000
1787 Values:
1788 )";
1789 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
1790 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00001791 std::unique_ptr<DWARFContext> DwarfContext =
1792 DWARFContext::create(*ErrOrSections, 8);
1793 VerifyError(*DwarfContext, "error: DW_FORM_ref4 CU offset 0x00001234 is "
1794 "invalid (must be less than CU size of "
1795 "0x0000001a):");
Greg Clayton48432cf2017-05-01 22:07:02 +00001796}
1797
1798TEST(DWARFDebugInfo, TestDwarfVerifyInvalidRefAddr) {
1799 // Create a single compile unit with a single function that has an invalid
1800 // DW_AT_type with an invalid .debug_info offset in its DW_FORM_ref_addr.
1801 const char *yamldata = R"(
1802 debug_str:
1803 - ''
1804 - /tmp/main.c
1805 - main
1806 debug_abbrev:
1807 - Code: 0x00000001
1808 Tag: DW_TAG_compile_unit
1809 Children: DW_CHILDREN_yes
1810 Attributes:
1811 - Attribute: DW_AT_name
1812 Form: DW_FORM_strp
1813 - Code: 0x00000002
1814 Tag: DW_TAG_subprogram
1815 Children: DW_CHILDREN_no
1816 Attributes:
1817 - Attribute: DW_AT_name
1818 Form: DW_FORM_strp
1819 - Attribute: DW_AT_type
1820 Form: DW_FORM_ref_addr
1821 debug_info:
1822 - Length:
1823 TotalLength: 22
1824 Version: 4
1825 AbbrOffset: 0
1826 AddrSize: 8
1827 Entries:
1828 - AbbrCode: 0x00000001
1829 Values:
1830 - Value: 0x0000000000000001
1831 - AbbrCode: 0x00000002
1832 Values:
1833 - Value: 0x000000000000000D
1834 - Value: 0x0000000000001234
1835 - AbbrCode: 0x00000000
1836 Values:
1837 )";
1838 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
1839 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00001840 std::unique_ptr<DWARFContext> DwarfContext =
1841 DWARFContext::create(*ErrOrSections, 8);
1842 VerifyError(*DwarfContext,
Greg Clayton67070462017-05-02 22:48:52 +00001843 "error: DW_FORM_ref_addr offset beyond .debug_info bounds:");
Greg Clayton48432cf2017-05-01 22:07:02 +00001844}
1845
1846TEST(DWARFDebugInfo, TestDwarfVerifyInvalidRanges) {
1847 // Create a single compile unit with a DW_AT_ranges whose section offset
1848 // isn't valid.
1849 const char *yamldata = R"(
1850 debug_str:
1851 - ''
1852 - /tmp/main.c
1853 debug_abbrev:
1854 - Code: 0x00000001
1855 Tag: DW_TAG_compile_unit
1856 Children: DW_CHILDREN_no
1857 Attributes:
1858 - Attribute: DW_AT_name
1859 Form: DW_FORM_strp
1860 - Attribute: DW_AT_ranges
1861 Form: DW_FORM_sec_offset
1862 debug_info:
1863 - Length:
1864 TotalLength: 16
1865 Version: 4
1866 AbbrOffset: 0
1867 AddrSize: 8
1868 Entries:
1869 - AbbrCode: 0x00000001
1870 Values:
1871 - Value: 0x0000000000000001
1872 - Value: 0x0000000000001000
1873
1874 )";
1875 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
1876 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00001877 std::unique_ptr<DWARFContext> DwarfContext =
1878 DWARFContext::create(*ErrOrSections, 8);
1879 VerifyError(*DwarfContext,
Greg Clayton67070462017-05-02 22:48:52 +00001880 "error: DW_AT_ranges offset is beyond .debug_ranges bounds:");
Greg Clayton48432cf2017-05-01 22:07:02 +00001881}
1882
1883TEST(DWARFDebugInfo, TestDwarfVerifyInvalidStmtList) {
1884 // Create a single compile unit with a DW_AT_stmt_list whose section offset
1885 // isn't valid.
1886 const char *yamldata = R"(
1887 debug_str:
1888 - ''
1889 - /tmp/main.c
1890 debug_abbrev:
1891 - Code: 0x00000001
1892 Tag: DW_TAG_compile_unit
1893 Children: DW_CHILDREN_no
1894 Attributes:
1895 - Attribute: DW_AT_name
1896 Form: DW_FORM_strp
1897 - Attribute: DW_AT_stmt_list
1898 Form: DW_FORM_sec_offset
1899 debug_info:
1900 - Length:
1901 TotalLength: 16
1902 Version: 4
1903 AbbrOffset: 0
1904 AddrSize: 8
1905 Entries:
1906 - AbbrCode: 0x00000001
1907 Values:
1908 - Value: 0x0000000000000001
1909 - Value: 0x0000000000001000
1910
1911 )";
1912 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
1913 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00001914 std::unique_ptr<DWARFContext> DwarfContext =
1915 DWARFContext::create(*ErrOrSections, 8);
Greg Clayton67070462017-05-02 22:48:52 +00001916 VerifyError(
Rafael Espindolac398e672017-07-19 22:27:28 +00001917 *DwarfContext,
Greg Clayton67070462017-05-02 22:48:52 +00001918 "error: DW_AT_stmt_list offset is beyond .debug_line bounds: 0x00001000");
Greg Clayton48432cf2017-05-01 22:07:02 +00001919}
1920
1921TEST(DWARFDebugInfo, TestDwarfVerifyInvalidStrp) {
1922 // Create a single compile unit with a single function that has an invalid
1923 // DW_FORM_strp for the DW_AT_name.
1924 const char *yamldata = R"(
1925 debug_str:
1926 - ''
1927 debug_abbrev:
1928 - Code: 0x00000001
1929 Tag: DW_TAG_compile_unit
1930 Children: DW_CHILDREN_no
1931 Attributes:
1932 - Attribute: DW_AT_name
1933 Form: DW_FORM_strp
1934 debug_info:
1935 - Length:
1936 TotalLength: 12
1937 Version: 4
1938 AbbrOffset: 0
1939 AddrSize: 8
1940 Entries:
1941 - AbbrCode: 0x00000001
1942 Values:
1943 - Value: 0x0000000000001234
1944 )";
1945 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
1946 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00001947 std::unique_ptr<DWARFContext> DwarfContext =
1948 DWARFContext::create(*ErrOrSections, 8);
1949 VerifyError(*DwarfContext,
Greg Clayton67070462017-05-02 22:48:52 +00001950 "error: DW_FORM_strp offset beyond .debug_str bounds:");
Greg Clayton48432cf2017-05-01 22:07:02 +00001951}
1952
Greg Claytonc7695a82017-05-02 20:28:33 +00001953TEST(DWARFDebugInfo, TestDwarfVerifyInvalidRefAddrBetween) {
1954 // Create a single compile unit with a single function that has a DW_AT_type
1955 // with a valid .debug_info offset, but the offset is between two DIEs.
1956 const char *yamldata = R"(
1957 debug_str:
1958 - ''
1959 - /tmp/main.c
1960 - main
1961 debug_abbrev:
1962 - Code: 0x00000001
1963 Tag: DW_TAG_compile_unit
1964 Children: DW_CHILDREN_yes
1965 Attributes:
1966 - Attribute: DW_AT_name
1967 Form: DW_FORM_strp
1968 - Code: 0x00000002
1969 Tag: DW_TAG_subprogram
1970 Children: DW_CHILDREN_no
1971 Attributes:
1972 - Attribute: DW_AT_name
1973 Form: DW_FORM_strp
1974 - Attribute: DW_AT_type
1975 Form: DW_FORM_ref_addr
1976 debug_info:
1977 - Length:
1978 TotalLength: 22
1979 Version: 4
1980 AbbrOffset: 0
1981 AddrSize: 8
1982 Entries:
1983 - AbbrCode: 0x00000001
1984 Values:
1985 - Value: 0x0000000000000001
1986 - AbbrCode: 0x00000002
1987 Values:
1988 - Value: 0x000000000000000D
1989 - Value: 0x0000000000000011
1990 - AbbrCode: 0x00000000
1991 Values:
1992 )";
1993 auto ErrOrSections = DWARFYAML::EmitDebugSections(StringRef(yamldata));
1994 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00001995 std::unique_ptr<DWARFContext> DwarfContext =
1996 DWARFContext::create(*ErrOrSections, 8);
Greg Clayton67070462017-05-02 22:48:52 +00001997 VerifyError(
Rafael Espindolac398e672017-07-19 22:27:28 +00001998 *DwarfContext,
Greg Clayton67070462017-05-02 22:48:52 +00001999 "error: invalid DIE reference 0x00000011. Offset is in between DIEs:");
Greg Claytonc7695a82017-05-02 20:28:33 +00002000}
Greg Clayton67070462017-05-02 22:48:52 +00002001
2002TEST(DWARFDebugInfo, TestDwarfVerifyInvalidLineSequence) {
2003 // Create a single compile unit whose line table has a sequence in it where
2004 // the address decreases.
2005 StringRef yamldata = R"(
2006 debug_str:
2007 - ''
2008 - /tmp/main.c
2009 debug_abbrev:
2010 - Code: 0x00000001
2011 Tag: DW_TAG_compile_unit
2012 Children: DW_CHILDREN_no
2013 Attributes:
2014 - Attribute: DW_AT_name
2015 Form: DW_FORM_strp
2016 - Attribute: DW_AT_stmt_list
2017 Form: DW_FORM_sec_offset
2018 debug_info:
2019 - Length:
2020 TotalLength: 16
2021 Version: 4
2022 AbbrOffset: 0
2023 AddrSize: 8
2024 Entries:
2025 - AbbrCode: 0x00000001
2026 Values:
2027 - Value: 0x0000000000000001
2028 - Value: 0x0000000000000000
2029 debug_line:
2030 - Length:
2031 TotalLength: 68
2032 Version: 2
2033 PrologueLength: 34
2034 MinInstLength: 1
2035 DefaultIsStmt: 1
2036 LineBase: 251
2037 LineRange: 14
2038 OpcodeBase: 13
2039 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
2040 IncludeDirs:
2041 - /tmp
2042 Files:
2043 - Name: main.c
2044 DirIdx: 1
2045 ModTime: 0
2046 Length: 0
2047 Opcodes:
2048 - Opcode: DW_LNS_extended_op
2049 ExtLen: 9
2050 SubOpcode: DW_LNE_set_address
2051 Data: 4112
2052 - Opcode: DW_LNS_advance_line
2053 SData: 9
2054 Data: 4112
2055 - Opcode: DW_LNS_copy
2056 Data: 4112
2057 - Opcode: DW_LNS_advance_pc
2058 Data: 18446744073709551600
2059 - Opcode: DW_LNS_extended_op
2060 ExtLen: 1
2061 SubOpcode: DW_LNE_end_sequence
2062 Data: 18446744073709551600
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] row[1] decreases "
2069 "in address from previous row:");
Greg Clayton67070462017-05-02 22:48:52 +00002070}
2071
2072TEST(DWARFDebugInfo, TestDwarfVerifyInvalidLineFileIndex) {
2073 // Create a single compile unit whose line table has a line table row with
2074 // an invalid file 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: 1
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: 5
2131 - Opcode: DW_LNS_extended_op
2132 ExtLen: 1
2133 SubOpcode: DW_LNE_end_sequence
2134 Data: 5
2135 )";
2136 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2137 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00002138 std::unique_ptr<DWARFContext> DwarfContext =
2139 DWARFContext::create(*ErrOrSections, 8);
2140 VerifyError(*DwarfContext, "error: .debug_line[0x00000000][1] has invalid "
2141 "file index 5 (valid values are [1,1]):");
Greg Clayton67070462017-05-02 22:48:52 +00002142}
2143
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +00002144TEST(DWARFDebugInfo, TestDwarfVerifyInvalidLineTablePorlogueDirIndex) {
2145 // Create a single compile unit whose line table has a prologue with an
2146 // invalid dir index.
2147 StringRef yamldata = R"(
2148 debug_str:
2149 - ''
2150 - /tmp/main.c
2151 debug_abbrev:
2152 - Code: 0x00000001
2153 Tag: DW_TAG_compile_unit
2154 Children: DW_CHILDREN_no
2155 Attributes:
2156 - Attribute: DW_AT_name
2157 Form: DW_FORM_strp
2158 - Attribute: DW_AT_stmt_list
2159 Form: DW_FORM_sec_offset
2160 debug_info:
2161 - Length:
2162 TotalLength: 16
2163 Version: 4
2164 AbbrOffset: 0
2165 AddrSize: 8
2166 Entries:
2167 - AbbrCode: 0x00000001
2168 Values:
2169 - Value: 0x0000000000000001
2170 - Value: 0x0000000000000000
2171 debug_line:
2172 - Length:
2173 TotalLength: 61
2174 Version: 2
2175 PrologueLength: 34
2176 MinInstLength: 1
2177 DefaultIsStmt: 1
2178 LineBase: 251
2179 LineRange: 14
2180 OpcodeBase: 13
2181 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
2182 IncludeDirs:
2183 - /tmp
2184 Files:
2185 - Name: main.c
2186 DirIdx: 2
2187 ModTime: 0
2188 Length: 0
2189 Opcodes:
2190 - Opcode: DW_LNS_extended_op
2191 ExtLen: 9
2192 SubOpcode: DW_LNE_set_address
2193 Data: 4096
2194 - Opcode: DW_LNS_advance_line
2195 SData: 9
2196 Data: 4096
2197 - Opcode: DW_LNS_copy
2198 Data: 4096
2199 - Opcode: DW_LNS_advance_pc
2200 Data: 16
2201 - Opcode: DW_LNS_set_file
2202 Data: 1
2203 - Opcode: DW_LNS_extended_op
2204 ExtLen: 1
2205 SubOpcode: DW_LNE_end_sequence
2206 Data: 1
2207 )";
2208 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2209 ASSERT_TRUE((bool)ErrOrSections);
2210 std::unique_ptr<DWARFContext> DwarfContext =
2211 DWARFContext::create(*ErrOrSections, 8);
2212 VerifyError(*DwarfContext,
2213 "error: .debug_line[0x00000000].prologue."
2214 "file_names[1].dir_idx contains an invalid index: 2");
2215}
2216
2217TEST(DWARFDebugInfo, TestDwarfVerifyDuplicateFileWarning) {
2218 // Create a single compile unit whose line table has a prologue with an
2219 // invalid dir index.
2220 StringRef yamldata = R"(
2221 debug_str:
2222 - ''
2223 - /tmp/main.c
2224 debug_abbrev:
2225 - Code: 0x00000001
2226 Tag: DW_TAG_compile_unit
2227 Children: DW_CHILDREN_no
2228 Attributes:
2229 - Attribute: DW_AT_name
2230 Form: DW_FORM_strp
2231 - Attribute: DW_AT_stmt_list
2232 Form: DW_FORM_sec_offset
2233 debug_info:
2234 - Length:
2235 TotalLength: 16
2236 Version: 4
2237 AbbrOffset: 0
2238 AddrSize: 8
2239 Entries:
2240 - AbbrCode: 0x00000001
2241 Values:
2242 - Value: 0x0000000000000001
2243 - Value: 0x0000000000000000
2244 debug_line:
2245 - Length:
2246 TotalLength: 71
2247 Version: 2
2248 PrologueLength: 44
2249 MinInstLength: 1
2250 DefaultIsStmt: 1
2251 LineBase: 251
2252 LineRange: 14
2253 OpcodeBase: 13
2254 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
2255 IncludeDirs:
2256 - /tmp
2257 Files:
2258 - Name: main.c
2259 DirIdx: 1
2260 ModTime: 0
2261 Length: 0
2262 - Name: main.c
2263 DirIdx: 1
2264 ModTime: 0
2265 Length: 0
2266 Opcodes:
2267 - Opcode: DW_LNS_extended_op
2268 ExtLen: 9
2269 SubOpcode: DW_LNE_set_address
2270 Data: 4096
2271 - Opcode: DW_LNS_advance_line
2272 SData: 9
2273 Data: 4096
2274 - Opcode: DW_LNS_copy
2275 Data: 4096
2276 - Opcode: DW_LNS_advance_pc
2277 Data: 16
2278 - Opcode: DW_LNS_set_file
2279 Data: 1
2280 - Opcode: DW_LNS_extended_op
2281 ExtLen: 1
2282 SubOpcode: DW_LNE_end_sequence
2283 Data: 2
2284 )";
2285 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2286 ASSERT_TRUE((bool)ErrOrSections);
2287 std::unique_ptr<DWARFContext> DwarfContext =
2288 DWARFContext::create(*ErrOrSections, 8);
2289 VerifyWarning(*DwarfContext,
2290 "warning: .debug_line[0x00000000].prologue.file_names[2] is "
2291 "a duplicate of file_names[1]");
2292}
2293
Greg Clayton8df55b42017-05-03 15:45:31 +00002294TEST(DWARFDebugInfo, TestDwarfVerifyCUDontShareLineTable) {
2295 // Create a two compile units where both compile units share the same
2296 // DW_AT_stmt_list value and verify we report the error correctly.
2297 StringRef yamldata = R"(
2298 debug_str:
2299 - ''
2300 - /tmp/main.c
2301 - /tmp/foo.c
George Rimar002655d2017-06-28 08:26:57 +00002302 debug_abbrev:
Greg Clayton8df55b42017-05-03 15:45:31 +00002303 - Code: 0x00000001
2304 Tag: DW_TAG_compile_unit
2305 Children: DW_CHILDREN_no
George Rimar002655d2017-06-28 08:26:57 +00002306 Attributes:
Greg Clayton8df55b42017-05-03 15:45:31 +00002307 - Attribute: DW_AT_name
2308 Form: DW_FORM_strp
2309 - Attribute: DW_AT_stmt_list
2310 Form: DW_FORM_sec_offset
George Rimar002655d2017-06-28 08:26:57 +00002311 debug_info:
2312 - Length:
Greg Clayton8df55b42017-05-03 15:45:31 +00002313 TotalLength: 16
2314 Version: 4
2315 AbbrOffset: 0
2316 AddrSize: 8
George Rimar002655d2017-06-28 08:26:57 +00002317 Entries:
Greg Clayton8df55b42017-05-03 15:45:31 +00002318 - AbbrCode: 0x00000001
George Rimar002655d2017-06-28 08:26:57 +00002319 Values:
Greg Clayton8df55b42017-05-03 15:45:31 +00002320 - Value: 0x0000000000000001
2321 - Value: 0x0000000000000000
George Rimar002655d2017-06-28 08:26:57 +00002322 - Length:
Greg Clayton8df55b42017-05-03 15:45:31 +00002323 TotalLength: 16
2324 Version: 4
2325 AbbrOffset: 0
2326 AddrSize: 8
George Rimar002655d2017-06-28 08:26:57 +00002327 Entries:
Greg Clayton8df55b42017-05-03 15:45:31 +00002328 - AbbrCode: 0x00000001
George Rimar002655d2017-06-28 08:26:57 +00002329 Values:
Greg Clayton8df55b42017-05-03 15:45:31 +00002330 - Value: 0x000000000000000D
2331 - Value: 0x0000000000000000
George Rimar002655d2017-06-28 08:26:57 +00002332 debug_line:
2333 - Length:
Greg Clayton8df55b42017-05-03 15:45:31 +00002334 TotalLength: 60
2335 Version: 2
2336 PrologueLength: 34
2337 MinInstLength: 1
2338 DefaultIsStmt: 1
2339 LineBase: 251
2340 LineRange: 14
2341 OpcodeBase: 13
2342 StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
George Rimar002655d2017-06-28 08:26:57 +00002343 IncludeDirs:
Greg Clayton8df55b42017-05-03 15:45:31 +00002344 - /tmp
George Rimar002655d2017-06-28 08:26:57 +00002345 Files:
Greg Clayton8df55b42017-05-03 15:45:31 +00002346 - Name: main.c
2347 DirIdx: 1
2348 ModTime: 0
2349 Length: 0
George Rimar002655d2017-06-28 08:26:57 +00002350 Opcodes:
Greg Clayton8df55b42017-05-03 15:45:31 +00002351 - Opcode: DW_LNS_extended_op
2352 ExtLen: 9
2353 SubOpcode: DW_LNE_set_address
2354 Data: 4096
2355 - Opcode: DW_LNS_advance_line
2356 SData: 9
2357 Data: 4096
2358 - Opcode: DW_LNS_copy
2359 Data: 4096
2360 - Opcode: DW_LNS_advance_pc
2361 Data: 256
2362 - Opcode: DW_LNS_extended_op
2363 ExtLen: 1
2364 SubOpcode: DW_LNE_end_sequence
2365 Data: 256
2366 )";
2367 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2368 ASSERT_TRUE((bool)ErrOrSections);
Rafael Espindolac398e672017-07-19 22:27:28 +00002369 std::unique_ptr<DWARFContext> DwarfContext =
2370 DWARFContext::create(*ErrOrSections, 8);
2371 VerifyError(*DwarfContext,
2372 "error: two compile unit DIEs, 0x0000000b and "
2373 "0x0000001f, have the same DW_AT_stmt_list section "
2374 "offset:");
Greg Clayton8df55b42017-05-03 15:45:31 +00002375}
2376
George Rimar1af3cb22017-06-28 08:21:19 +00002377TEST(DWARFDebugInfo, TestErrorReportingPolicy) {
George Rimard8508b02017-06-30 10:09:01 +00002378 Triple Triple("x86_64-pc-linux");
2379 if (!isConfigurationSupported(Triple))
2380 return;
2381
2382 auto ExpectedDG = dwarfgen::Generator::create(Triple, 4 /*DwarfVersion*/);
2383 ASSERT_THAT_EXPECTED(ExpectedDG, Succeeded());
George Rimar1af3cb22017-06-28 08:21:19 +00002384 dwarfgen::Generator *DG = ExpectedDG.get().get();
2385 AsmPrinter *AP = DG->getAsmPrinter();
2386 MCContext *MC = DG->getMCContext();
2387
2388 // Emit two compressed sections with broken headers.
2389 AP->OutStreamer->SwitchSection(
2390 MC->getELFSection(".zdebug_foo", 0 /*Type*/, 0 /*Flags*/));
2391 AP->OutStreamer->EmitBytes("0");
2392 AP->OutStreamer->SwitchSection(
2393 MC->getELFSection(".zdebug_bar", 0 /*Type*/, 0 /*Flags*/));
2394 AP->OutStreamer->EmitBytes("0");
2395
2396 MemoryBufferRef FileBuffer(DG->generate(), "dwarf");
2397 auto Obj = object::ObjectFile::createObjectFile(FileBuffer);
2398 EXPECT_TRUE((bool)Obj);
2399
2400 // Case 1: error handler handles all errors. That allows
Rafael Espindola3ee9e112017-07-19 23:34:59 +00002401 // DWARFContext to parse whole file and find both two errors we know about.
George Rimar1af3cb22017-06-28 08:21:19 +00002402 int Errors = 0;
Rafael Espindolac398e672017-07-19 22:27:28 +00002403 std::unique_ptr<DWARFContext> Ctx1 =
2404 DWARFContext::create(**Obj, nullptr, [&](Error E) {
2405 ++Errors;
2406 consumeError(std::move(E));
2407 return ErrorPolicy::Continue;
2408 });
George Rimar1af3cb22017-06-28 08:21:19 +00002409 EXPECT_TRUE(Errors == 2);
2410
2411 // Case 2: error handler stops parsing of object after first error.
2412 Errors = 0;
Rafael Espindolac398e672017-07-19 22:27:28 +00002413 std::unique_ptr<DWARFContext> Ctx2 =
2414 DWARFContext::create(**Obj, nullptr, [&](Error E) {
2415 ++Errors;
2416 consumeError(std::move(E));
2417 return ErrorPolicy::Halt;
2418 });
George Rimar1af3cb22017-06-28 08:21:19 +00002419 EXPECT_TRUE(Errors == 1);
2420}
2421
Jonas Devlieghere58910602017-09-14 11:33:42 +00002422TEST(DWARFDebugInfo, TestDwarfVerifyCURangesIncomplete) {
2423 // Create a single compile unit with a single function. The compile
2424 // unit has a DW_AT_ranges attribute that doesn't fully contain the
2425 // address range of the function. The verification should fail due to
2426 // the CU ranges not containing all of the address ranges of all of the
2427 // functions.
2428 StringRef yamldata = R"(
2429 debug_str:
2430 - ''
2431 - /tmp/main.c
2432 debug_abbrev:
2433 - Code: 0x00000001
2434 Tag: DW_TAG_compile_unit
2435 Children: DW_CHILDREN_yes
2436 Attributes:
2437 - Attribute: DW_AT_low_pc
2438 Form: DW_FORM_addr
2439 - Attribute: DW_AT_high_pc
2440 Form: DW_FORM_addr
2441 - Attribute: DW_AT_name
2442 Form: DW_FORM_strp
2443 - Code: 0x00000002
2444 Tag: DW_TAG_subprogram
2445 Children: DW_CHILDREN_no
2446 Attributes:
2447 - Attribute: DW_AT_low_pc
2448 Form: DW_FORM_addr
2449 - Attribute: DW_AT_high_pc
2450 Form: DW_FORM_addr
2451 debug_info:
2452 - Length:
2453 TotalLength: 46
2454 Version: 4
2455 AbbrOffset: 0
2456 AddrSize: 8
2457 Entries:
2458 - AbbrCode: 0x00000001
2459 Values:
2460 - Value: 0x0000000000001000
2461 - Value: 0x0000000000001500
2462 - Value: 0x0000000000000001
2463 - AbbrCode: 0x00000002
2464 Values:
2465 - Value: 0x0000000000001000
2466 - Value: 0x0000000000002000
2467 - AbbrCode: 0x00000000
2468 Values:
2469 )";
2470 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2471 ASSERT_TRUE((bool)ErrOrSections);
2472 std::unique_ptr<DWARFContext> DwarfContext =
2473 DWARFContext::create(*ErrOrSections, 8);
2474 VerifyError(*DwarfContext, "error: DIE address ranges are not "
2475 "contained in its parent's ranges:");
2476}
2477
2478TEST(DWARFDebugInfo, TestDwarfVerifyLexicalBlockRanges) {
2479 // Create a single compile unit with a single function that has a lexical
2480 // block whose address range is not contained in the function address range.
2481 StringRef yamldata = R"(
2482 debug_str:
2483 - ''
2484 - /tmp/main.c
2485 - main
2486 debug_abbrev:
2487 - Code: 0x00000001
2488 Tag: DW_TAG_compile_unit
2489 Children: DW_CHILDREN_yes
2490 Attributes:
2491 - Attribute: DW_AT_name
2492 Form: DW_FORM_strp
2493 - Code: 0x00000002
2494 Tag: DW_TAG_subprogram
2495 Children: DW_CHILDREN_yes
2496 Attributes:
2497 - Attribute: DW_AT_name
2498 Form: DW_FORM_strp
2499 - Attribute: DW_AT_low_pc
2500 Form: DW_FORM_addr
2501 - Attribute: DW_AT_high_pc
2502 Form: DW_FORM_addr
2503 - Code: 0x00000003
2504 Tag: DW_TAG_lexical_block
2505 Children: DW_CHILDREN_no
2506 Attributes:
2507 - Attribute: DW_AT_low_pc
2508 Form: DW_FORM_addr
2509 - Attribute: DW_AT_high_pc
2510 Form: DW_FORM_addr
2511 debug_info:
2512 - Length:
2513 TotalLength: 52
2514 Version: 4
2515 AbbrOffset: 0
2516 AddrSize: 8
2517 Entries:
2518 - AbbrCode: 0x00000001
2519 Values:
2520 - Value: 0x0000000000000001
2521 - AbbrCode: 0x00000002
2522 Values:
2523 - Value: 0x000000000000000D
2524 - Value: 0x0000000000001000
2525 - Value: 0x0000000000002000
2526 - AbbrCode: 0x00000003
2527 Values:
2528 - Value: 0x0000000000001000
2529 - Value: 0x0000000000002001
2530 - AbbrCode: 0x00000000
2531 Values:
2532 - AbbrCode: 0x00000000
2533 Values:
2534 )";
2535 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2536 ASSERT_TRUE((bool)ErrOrSections);
2537 std::unique_ptr<DWARFContext> DwarfContext =
2538 DWARFContext::create(*ErrOrSections, 8);
2539 VerifyError(*DwarfContext, "error: DIE address ranges are not "
2540 "contained in its parent's ranges:");
2541}
2542
2543TEST(DWARFDebugInfo, TestDwarfVerifyOverlappingFunctionRanges) {
2544 // Create a single compile unit with a two functions that have overlapping
2545 // address ranges.
2546 StringRef yamldata = R"(
2547 debug_str:
2548 - ''
2549 - /tmp/main.c
2550 - main
2551 - foo
2552 debug_abbrev:
2553 - Code: 0x00000001
2554 Tag: DW_TAG_compile_unit
2555 Children: DW_CHILDREN_yes
2556 Attributes:
2557 - Attribute: DW_AT_name
2558 Form: DW_FORM_strp
2559 - Code: 0x00000002
2560 Tag: DW_TAG_subprogram
2561 Children: DW_CHILDREN_no
2562 Attributes:
2563 - Attribute: DW_AT_name
2564 Form: DW_FORM_strp
2565 - Attribute: DW_AT_low_pc
2566 Form: DW_FORM_addr
2567 - Attribute: DW_AT_high_pc
2568 Form: DW_FORM_addr
2569 debug_info:
2570 - Length:
2571 TotalLength: 55
2572 Version: 4
2573 AbbrOffset: 0
2574 AddrSize: 8
2575 Entries:
2576 - AbbrCode: 0x00000001
2577 Values:
2578 - Value: 0x0000000000000001
2579 - AbbrCode: 0x00000002
2580 Values:
2581 - Value: 0x000000000000000D
2582 - Value: 0x0000000000001000
2583 - Value: 0x0000000000002000
2584 - AbbrCode: 0x00000002
2585 Values:
2586 - Value: 0x0000000000000012
2587 - Value: 0x0000000000001FFF
2588 - Value: 0x0000000000002000
2589 - AbbrCode: 0x00000000
2590 Values:
2591 )";
2592 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2593 ASSERT_TRUE((bool)ErrOrSections);
2594 std::unique_ptr<DWARFContext> DwarfContext =
2595 DWARFContext::create(*ErrOrSections, 8);
2596 VerifyError(*DwarfContext, "error: DIEs have overlapping address ranges:");
2597}
2598
2599TEST(DWARFDebugInfo, TestDwarfVerifyOverlappingLexicalBlockRanges) {
2600 // Create a single compile unit with a one function that has two lexical
2601 // blocks with overlapping address ranges.
2602 StringRef yamldata = R"(
2603 debug_str:
2604 - ''
2605 - /tmp/main.c
2606 - main
2607 debug_abbrev:
2608 - Code: 0x00000001
2609 Tag: DW_TAG_compile_unit
2610 Children: DW_CHILDREN_yes
2611 Attributes:
2612 - Attribute: DW_AT_low_pc
2613 Form: DW_FORM_addr
2614 - Attribute: DW_AT_high_pc
2615 Form: DW_FORM_addr
2616 - Attribute: DW_AT_name
2617 Form: DW_FORM_strp
2618 - Code: 0x00000002
2619 Tag: DW_TAG_subprogram
2620 Children: DW_CHILDREN_yes
2621 Attributes:
2622 - Attribute: DW_AT_name
2623 Form: DW_FORM_strp
2624 - Attribute: DW_AT_low_pc
2625 Form: DW_FORM_addr
2626 - Attribute: DW_AT_high_pc
2627 Form: DW_FORM_addr
2628 - Code: 0x00000003
2629 Tag: DW_TAG_lexical_block
2630 Children: DW_CHILDREN_no
2631 Attributes:
2632 - Attribute: DW_AT_low_pc
2633 Form: DW_FORM_addr
2634 - Attribute: DW_AT_high_pc
2635 Form: DW_FORM_addr
2636 debug_info:
2637 - Length:
2638 TotalLength: 85
2639 Version: 4
2640 AbbrOffset: 0
2641 AddrSize: 8
2642 Entries:
2643 - AbbrCode: 0x00000001
2644 Values:
2645 - Value: 0x0000000000001000
2646 - Value: 0x0000000000002000
2647 - Value: 0x0000000000000001
2648 - AbbrCode: 0x00000002
2649 Values:
2650 - Value: 0x000000000000000D
2651 - Value: 0x0000000000001000
2652 - Value: 0x0000000000002000
2653 - AbbrCode: 0x00000003
2654 Values:
2655 - Value: 0x0000000000001100
2656 - Value: 0x0000000000001300
2657 - AbbrCode: 0x00000003
2658 Values:
2659 - Value: 0x00000000000012FF
2660 - Value: 0x0000000000001300
2661 - AbbrCode: 0x00000000
2662 Values:
2663 - AbbrCode: 0x00000000
2664 Values:
2665 )";
2666 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2667 ASSERT_TRUE((bool)ErrOrSections);
2668 std::unique_ptr<DWARFContext> DwarfContext =
2669 DWARFContext::create(*ErrOrSections, 8);
2670 VerifyError(*DwarfContext, "error: DIEs have overlapping address ranges:");
2671}
2672
2673TEST(DWARFDebugInfo, TestDwarfVerifyInvalidDIERange) {
2674 // Create a single compile unit with a single function that has an invalid
2675 // address range where the high PC is smaller than the low PC.
2676 StringRef yamldata = R"(
2677 debug_str:
2678 - ''
2679 - /tmp/main.c
2680 - main
2681 debug_abbrev:
2682 - Code: 0x00000001
2683 Tag: DW_TAG_compile_unit
2684 Children: DW_CHILDREN_yes
2685 Attributes:
2686 - Attribute: DW_AT_name
2687 Form: DW_FORM_strp
2688 - Code: 0x00000002
2689 Tag: DW_TAG_subprogram
2690 Children: DW_CHILDREN_no
2691 Attributes:
2692 - Attribute: DW_AT_name
2693 Form: DW_FORM_strp
2694 - Attribute: DW_AT_low_pc
2695 Form: DW_FORM_addr
2696 - Attribute: DW_AT_high_pc
2697 Form: DW_FORM_addr
2698 debug_info:
2699 - Length:
2700 TotalLength: 34
2701 Version: 4
2702 AbbrOffset: 0
2703 AddrSize: 8
2704 Entries:
2705 - AbbrCode: 0x00000001
2706 Values:
2707 - Value: 0x0000000000000001
2708 - AbbrCode: 0x00000002
2709 Values:
2710 - Value: 0x000000000000000D
2711 - Value: 0x0000000000001000
2712 - Value: 0x0000000000000900
2713 - AbbrCode: 0x00000000
2714 Values:
2715 )";
2716 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2717 ASSERT_TRUE((bool)ErrOrSections);
2718 std::unique_ptr<DWARFContext> DwarfContext =
2719 DWARFContext::create(*ErrOrSections, 8);
2720 VerifyError(*DwarfContext, "error: Invalid address range");
2721}
2722
2723TEST(DWARFDebugInfo, TestDwarfVerifyElidedDoesntFail) {
2724 // Create a single compile unit with two functions: one that has a valid range
2725 // and one whose low and high PC are the same. When the low and high PC are
2726 // the same, this indicates the function was dead code stripped. We want to
2727 // ensure that verification succeeds.
2728 StringRef yamldata = R"(
2729 debug_str:
2730 - ''
2731 - /tmp/main.c
2732 - main
2733 - elided
2734 debug_abbrev:
2735 - Code: 0x00000001
2736 Tag: DW_TAG_compile_unit
2737 Children: DW_CHILDREN_yes
2738 Attributes:
2739 - Attribute: DW_AT_low_pc
2740 Form: DW_FORM_addr
2741 - Attribute: DW_AT_high_pc
2742 Form: DW_FORM_addr
2743 - Attribute: DW_AT_name
2744 Form: DW_FORM_strp
2745 - Code: 0x00000002
2746 Tag: DW_TAG_subprogram
2747 Children: DW_CHILDREN_no
2748 Attributes:
2749 - Attribute: DW_AT_name
2750 Form: DW_FORM_strp
2751 - Attribute: DW_AT_low_pc
2752 Form: DW_FORM_addr
2753 - Attribute: DW_AT_high_pc
2754 Form: DW_FORM_addr
2755 debug_info:
2756 - Length:
2757 TotalLength: 71
2758 Version: 4
2759 AbbrOffset: 0
2760 AddrSize: 8
2761 Entries:
2762 - AbbrCode: 0x00000001
2763 Values:
2764 - Value: 0x0000000000001000
2765 - Value: 0x0000000000002000
2766 - Value: 0x0000000000000001
2767 - AbbrCode: 0x00000002
2768 Values:
2769 - Value: 0x000000000000000D
2770 - Value: 0x0000000000001000
2771 - Value: 0x0000000000002000
2772 - AbbrCode: 0x00000002
2773 Values:
2774 - Value: 0x0000000000000012
2775 - Value: 0x0000000000002000
2776 - Value: 0x0000000000002000
2777 - AbbrCode: 0x00000000
2778 Values:
2779 )";
2780 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2781 ASSERT_TRUE((bool)ErrOrSections);
2782 std::unique_ptr<DWARFContext> DwarfContext =
2783 DWARFContext::create(*ErrOrSections, 8);
2784 VerifySuccess(*DwarfContext);
2785}
2786
2787TEST(DWARFDebugInfo, TestDwarfVerifyNestedFunctions) {
2788 // Create a single compile unit with a nested function which is not contained
2789 // in its parent. Although LLVM doesn't generate this, it is valid accoridng
2790 // to the DWARF standard.
2791 StringRef yamldata = R"(
2792 debug_str:
2793 - ''
2794 - /tmp/main.c
2795 - main
2796 - nested
2797 debug_abbrev:
2798 - Code: 0x00000001
2799 Tag: DW_TAG_compile_unit
2800 Children: DW_CHILDREN_yes
2801 Attributes:
2802 - Attribute: DW_AT_low_pc
2803 Form: DW_FORM_addr
2804 - Attribute: DW_AT_high_pc
2805 Form: DW_FORM_addr
2806 - Attribute: DW_AT_name
2807 Form: DW_FORM_strp
2808 - Code: 0x00000002
2809 Tag: DW_TAG_subprogram
2810 Children: DW_CHILDREN_yes
2811 Attributes:
2812 - Attribute: DW_AT_name
2813 Form: DW_FORM_strp
2814 - Attribute: DW_AT_low_pc
2815 Form: DW_FORM_addr
2816 - Attribute: DW_AT_high_pc
2817 Form: DW_FORM_addr
2818 debug_info:
2819 - Length:
2820 TotalLength: 73
2821 Version: 4
2822 AbbrOffset: 0
2823 AddrSize: 8
2824 Entries:
2825 - AbbrCode: 0x00000001
2826 Values:
2827 - Value: 0x0000000000001000
2828 - Value: 0x0000000000002000
2829 - Value: 0x0000000000000001
2830 - AbbrCode: 0x00000002
2831 Values:
2832 - Value: 0x000000000000000D
2833 - Value: 0x0000000000001000
2834 - Value: 0x0000000000001500
2835 - AbbrCode: 0x00000002
2836 Values:
2837 - Value: 0x0000000000000012
2838 - Value: 0x0000000000001500
2839 - Value: 0x0000000000002000
2840 - AbbrCode: 0x00000000
2841 Values:
2842 - AbbrCode: 0x00000000
2843 Values:
2844 - AbbrCode: 0x00000000
2845 Values:
2846 )";
2847 auto ErrOrSections = DWARFYAML::EmitDebugSections(yamldata);
2848 ASSERT_TRUE((bool)ErrOrSections);
2849 std::unique_ptr<DWARFContext> DwarfContext =
2850 DWARFContext::create(*ErrOrSections, 8);
2851 VerifySuccess(*DwarfContext);
2852}
2853
2854TEST(DWARFDebugInfo, TestDwarfRangesContains) {
2855 DWARFAddressRange R(0x10, 0x20);
2856
2857 //----------------------------------------------------------------------
2858 // Test ranges that start before R...
2859 //----------------------------------------------------------------------
2860 // Other range ends before start of R
2861 ASSERT_FALSE(R.contains({0x0f, 0x10}));
2862 // Other range end address is start of a R
2863 ASSERT_FALSE(R.contains({0x0f, 0x11}));
2864 // Other range end address is at and of R
2865 ASSERT_FALSE(R.contains({0x0f, 0x20}));
2866 // Other range end address is past end of R
2867 ASSERT_FALSE(R.contains({0x0f, 0x40}));
2868
2869 //----------------------------------------------------------------------
2870 // Test ranges that start at R's start address
2871 //----------------------------------------------------------------------
2872 // Ensure empty ranges matches
2873 ASSERT_TRUE(R.contains({0x10, 0x10}));
2874 // 1 byte of Range
2875 ASSERT_TRUE(R.contains({0x10, 0x11}));
2876 // same as Range
2877 ASSERT_TRUE(R.contains({0x10, 0x20}));
2878 // 1 byte past Range
2879 ASSERT_FALSE(R.contains({0x10, 0x21}));
2880
2881 //----------------------------------------------------------------------
2882 // Test ranges that start inside Range
2883 //----------------------------------------------------------------------
2884 // empty in range
2885 ASSERT_TRUE(R.contains({0x11, 0x11}));
2886 // all in Range
2887 ASSERT_TRUE(R.contains({0x11, 0x1f}));
2888 // ends at end of Range
2889 ASSERT_TRUE(R.contains({0x11, 0x20}));
2890 // ends past Range
2891 ASSERT_FALSE(R.contains({0x11, 0x21}));
2892
2893 //----------------------------------------------------------------------
2894 // Test ranges that start at last bytes of Range
2895 //----------------------------------------------------------------------
2896 // ends at end of Range
2897 ASSERT_TRUE(R.contains({0x1f, 0x20}));
2898 // ends past Range
2899 ASSERT_FALSE(R.contains({0x1f, 0x21}));
2900
2901 //----------------------------------------------------------------------
2902 // Test ranges that start after Range
2903 //----------------------------------------------------------------------
2904 // empty considered in Range
2905 ASSERT_TRUE(R.contains({0x20, 0x20}));
2906 // valid past Range
2907 ASSERT_FALSE(R.contains({0x20, 0x21}));
2908}
2909
2910TEST(DWARFDebugInfo, TestDWARFDieRangeInfoContains) {
2911 DWARFVerifier::DieRangeInfo Ranges({{0x10, 0x20}, {0x30, 0x40}});
2912
2913 ASSERT_FALSE(Ranges.contains({{{0x0f, 0x10}}}));
2914 ASSERT_FALSE(Ranges.contains({{{0x20, 0x30}}}));
2915 ASSERT_FALSE(Ranges.contains({{{0x40, 0x41}}}));
2916 ASSERT_TRUE(Ranges.contains({{{0x10, 0x20}}}));
2917 ASSERT_TRUE(Ranges.contains({{{0x11, 0x12}}}));
2918 ASSERT_TRUE(Ranges.contains({{{0x1f, 0x20}}}));
2919 ASSERT_TRUE(Ranges.contains({{{0x30, 0x40}}}));
2920 ASSERT_TRUE(Ranges.contains({{{0x31, 0x32}}}));
2921 ASSERT_TRUE(Ranges.contains({{{0x3f, 0x40}}}));
2922 ASSERT_TRUE(Ranges.contains({{{0x10, 0x20}, {0x30, 0x40}}}));
2923 ASSERT_TRUE(Ranges.contains({{{0x11, 0x12}, {0x31, 0x32}}}));
2924 ASSERT_TRUE(Ranges.contains(
2925 {{{0x11, 0x12}, {0x12, 0x13}, {0x31, 0x32}, {0x32, 0x33}}}));
2926 ASSERT_FALSE(Ranges.contains({{{0x11, 0x12},
2927 {0x12, 0x13},
2928 {0x20, 0x21},
2929 {0x31, 0x32},
2930 {0x32, 0x33}}}));
2931 ASSERT_FALSE(Ranges.contains(
2932 {{{0x11, 0x12}, {0x12, 0x13}, {0x31, 0x32}, {0x32, 0x41}}}));
2933}
2934
2935namespace {
2936
2937void AssertRangesIntersect(const DWARFAddressRange &LHS,
2938 const DWARFAddressRange &RHS) {
2939 ASSERT_TRUE(LHS.intersects(RHS));
2940 ASSERT_TRUE(RHS.intersects(LHS));
2941}
2942void AssertRangesDontIntersect(const DWARFAddressRange &LHS,
2943 const DWARFAddressRange &RHS) {
2944 ASSERT_FALSE(LHS.intersects(RHS));
2945 ASSERT_FALSE(RHS.intersects(LHS));
2946}
2947
2948void AssertRangesIntersect(const DWARFVerifier::DieRangeInfo &LHS,
2949 const DWARFAddressRangesVector &Ranges) {
2950 DWARFVerifier::DieRangeInfo RHS(Ranges);
2951 ASSERT_TRUE(LHS.intersects(RHS));
2952 ASSERT_TRUE(RHS.intersects(LHS));
2953}
2954
2955void AssertRangesDontIntersect(const DWARFVerifier::DieRangeInfo &LHS,
2956 const DWARFAddressRangesVector &Ranges) {
2957 DWARFVerifier::DieRangeInfo RHS(Ranges);
2958 ASSERT_FALSE(LHS.intersects(RHS));
2959 ASSERT_FALSE(RHS.intersects(LHS));
2960}
2961
2962} // namespace
2963TEST(DWARFDebugInfo, TestDwarfRangesIntersect) {
2964 DWARFAddressRange R(0x10, 0x20);
2965
2966 //----------------------------------------------------------------------
2967 // Test ranges that start before R...
2968 //----------------------------------------------------------------------
2969 // Other range ends before start of R
2970 AssertRangesDontIntersect(R, {0x00, 0x10});
2971 // Other range end address is start of a R
2972 AssertRangesIntersect(R, {0x00, 0x11});
2973 // Other range end address is in R
2974 AssertRangesIntersect(R, {0x00, 0x15});
2975 // Other range end address is at and of R
2976 AssertRangesIntersect(R, {0x00, 0x20});
2977 // Other range end address is past end of R
2978 AssertRangesIntersect(R, {0x00, 0x40});
2979
2980 //----------------------------------------------------------------------
2981 // Test ranges that start at R's start address
2982 //----------------------------------------------------------------------
2983 // Ensure empty ranges doesn't match
2984 AssertRangesDontIntersect(R, {0x10, 0x10});
2985 // 1 byte of Range
2986 AssertRangesIntersect(R, {0x10, 0x11});
2987 // same as Range
2988 AssertRangesIntersect(R, {0x10, 0x20});
2989 // 1 byte past Range
2990 AssertRangesIntersect(R, {0x10, 0x21});
2991
2992 //----------------------------------------------------------------------
2993 // Test ranges that start inside Range
2994 //----------------------------------------------------------------------
2995 // empty in range
2996 AssertRangesDontIntersect(R, {0x11, 0x11});
2997 // all in Range
2998 AssertRangesIntersect(R, {0x11, 0x1f});
2999 // ends at end of Range
3000 AssertRangesIntersect(R, {0x11, 0x20});
3001 // ends past Range
3002 AssertRangesIntersect(R, {0x11, 0x21});
3003
3004 //----------------------------------------------------------------------
3005 // Test ranges that start at last bytes of Range
3006 //----------------------------------------------------------------------
3007 // ends at end of Range
3008 AssertRangesIntersect(R, {0x1f, 0x20});
3009 // ends past Range
3010 AssertRangesIntersect(R, {0x1f, 0x21});
3011
3012 //----------------------------------------------------------------------
3013 // Test ranges that start after Range
3014 //----------------------------------------------------------------------
3015 // empty just past in Range
3016 AssertRangesDontIntersect(R, {0x20, 0x20});
3017 // valid past Range
3018 AssertRangesDontIntersect(R, {0x20, 0x21});
3019}
3020
3021TEST(DWARFDebugInfo, TestDWARFDieRangeInfoIntersects) {
3022
3023 DWARFVerifier::DieRangeInfo Ranges({{0x10, 0x20}, {0x30, 0x40}});
3024
3025 // Test empty range
3026 AssertRangesDontIntersect(Ranges, {});
3027 // Test range that appears before all ranges in Ranges
3028 AssertRangesDontIntersect(Ranges, {{0x00, 0x10}});
3029 // Test range that appears between ranges in Ranges
3030 AssertRangesDontIntersect(Ranges, {{0x20, 0x30}});
3031 // Test range that appears after ranges in Ranges
3032 AssertRangesDontIntersect(Ranges, {{0x40, 0x50}});
3033
3034 // Test range that start before first range
3035 AssertRangesIntersect(Ranges, {{0x00, 0x11}});
3036 // Test range that start at first range
3037 AssertRangesIntersect(Ranges, {{0x10, 0x11}});
3038 // Test range that start in first range
3039 AssertRangesIntersect(Ranges, {{0x11, 0x12}});
3040 // Test range that start at end of first range
3041 AssertRangesIntersect(Ranges, {{0x1f, 0x20}});
3042 // Test range that starts at end of first range
3043 AssertRangesDontIntersect(Ranges, {{0x20, 0x21}});
3044 // Test range that starts at end of first range
3045 AssertRangesIntersect(Ranges, {{0x20, 0x31}});
3046
3047 // Test range that start before second range and ends before second
3048 AssertRangesDontIntersect(Ranges, {{0x2f, 0x30}});
3049 // Test range that start before second range and ends in second
3050 AssertRangesIntersect(Ranges, {{0x2f, 0x31}});
3051 // Test range that start at second range
3052 AssertRangesIntersect(Ranges, {{0x30, 0x31}});
3053 // Test range that start in second range
3054 AssertRangesIntersect(Ranges, {{0x31, 0x32}});
3055 // Test range that start at end of second range
3056 AssertRangesIntersect(Ranges, {{0x3f, 0x40}});
3057 // Test range that starts at end of second range
3058 AssertRangesDontIntersect(Ranges, {{0x40, 0x41}});
3059}
3060
Greg Clayton3462a422016-12-08 01:03:48 +00003061} // end anonymous namespace