blob: ad00537c2a4a6890ccee5fad636fd3e61fede3a2 [file] [log] [blame]
Pavel Labatha03435e2019-11-08 15:24:11 +01001//===- llvm/unittest/DebugInfo/DWARFDieTest.cpp ---------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/BinaryFormat/Dwarf.h"
10#include "llvm/DebugInfo/DWARF/DWARFContext.h"
11#include "llvm/ObjectYAML/DWARFEmitter.h"
12#include "llvm/Testing/Support/Error.h"
13#include "gtest/gtest.h"
14
15using namespace llvm;
16using namespace llvm::dwarf;
17using object::SectionedAddress;
18
19namespace {
20
21TEST(DWARFDie, getLocations) {
22 const char *yamldata = R"(
23 debug_abbrev:
24 - Code: 0x00000001
25 Tag: DW_TAG_compile_unit
26 Children: DW_CHILDREN_no
27 Attributes:
28 - Attribute: DW_AT_location
29 Form: DW_FORM_sec_offset
30 - Attribute: DW_AT_data_member_location
31 Form: DW_FORM_exprloc
32 - Attribute: DW_AT_vtable_elem_location
33 Form: DW_FORM_sec_offset
34 - Attribute: DW_AT_call_data_location
35 Form: DW_FORM_sec_offset
36 debug_info:
37 - Length:
38 TotalLength: 0
39 Version: 5
40 UnitType: DW_UT_compile
41 AbbrOffset: 0
42 AddrSize: 4
43 Entries:
44 - AbbrCode: 0x00000001
45 Values:
46 - Value: 12
47 - Value: 0x0000000000000001
48 BlockData: [ 0x47 ]
49 - Value: 20
50 - Value: 25
51 )";
52 Expected<StringMap<std::unique_ptr<MemoryBuffer>>> Sections =
53 DWARFYAML::EmitDebugSections(StringRef(yamldata), /*ApplyFixups=*/true,
54 /*IsLittleEndian=*/true);
55 ASSERT_THAT_EXPECTED(Sections, Succeeded());
56 std::vector<uint8_t> Loclists{
57 // Header
58 0, 0, 0, 0, // Length
59 5, 0, // Version
60 4, // Address size
61 0, // Segment selector size
62 0, 0, 0, 0, // Offset entry count
63 // First location list.
64 DW_LLE_start_length, // First entry
65 1, 0, 0, 0, // Start offset
66 2, // Length
67 0, // Expression length
68 DW_LLE_end_of_list,
69 // Second location list.
70 DW_LLE_startx_length, // First entry
71 1, // Start index
72 2, // Length
73 0, // Expression length
74 DW_LLE_end_of_list,
75 // Third location list.
76 DW_LLE_start_length, // First entry
77 1, 0, 0, 0, // Start offset
78 2, // Length
79 0, // Expression length
80 // end_of_list intentionally missing
81 };
82 Loclists[0] = Loclists.size() - 4;
83 Sections->try_emplace(
84 "debug_loclists",
85 MemoryBuffer::getMemBuffer(toStringRef(Loclists), "debug_loclists",
86 /*RequiresNullTerminator=*/false));
87 std::unique_ptr<DWARFContext> Ctx =
88 DWARFContext::create(*Sections, 4, /*isLittleEndian=*/true);
89 DWARFCompileUnit *CU = Ctx->getCompileUnitForOffset(0);
90 ASSERT_NE(nullptr, CU);
91 DWARFDie Die = CU->getUnitDIE();
92 ASSERT_TRUE(Die.isValid());
93
94 EXPECT_THAT_EXPECTED(Die.getLocations(DW_AT_location),
95 HasValue(testing::ElementsAre(DWARFLocationExpression{
96 DWARFAddressRange{1, 3}, {}})));
97
98 EXPECT_THAT_EXPECTED(
99 Die.getLocations(DW_AT_data_member_location),
100 HasValue(testing::ElementsAre(DWARFLocationExpression{None, {0x47}})));
101
102 EXPECT_THAT_EXPECTED(
103 Die.getLocations(DW_AT_vtable_elem_location),
104 Failed<ErrorInfoBase>(testing::Property(
105 &ErrorInfoBase::message,
106 "Unable to resolve indirect address 1 for: DW_LLE_startx_length")));
107
Pavel Labath5754a612020-02-27 16:22:12 +0100108 EXPECT_THAT_EXPECTED(
109 Die.getLocations(DW_AT_call_data_location),
Pavel Labath04aea762020-04-20 17:28:15 +0200110 FailedWithMessage(
111 "unexpected end of data at offset 0x20 while reading [0x20, 0x21)"));
Pavel Labatha03435e2019-11-08 15:24:11 +0100112
113 EXPECT_THAT_EXPECTED(
114 Die.getLocations(DW_AT_call_data_value),
115 Failed<ErrorInfoBase>(testing::Property(&ErrorInfoBase::message,
116 "No DW_AT_call_data_value")));
117}
118
119} // end anonymous namespace