blob: d87f68e2f7403a07231975ef8e1b76a7f1ed371f [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:
Xing GUO86329312020-07-22 12:14:16 +080037 - Version: 5
Pavel Labatha03435e2019-11-08 15:24:11 +010038 UnitType: DW_UT_compile
39 AbbrOffset: 0
40 AddrSize: 4
41 Entries:
42 - AbbrCode: 0x00000001
43 Values:
44 - Value: 12
45 - Value: 0x0000000000000001
46 BlockData: [ 0x47 ]
47 - Value: 20
48 - Value: 25
49 )";
50 Expected<StringMap<std::unique_ptr<MemoryBuffer>>> Sections =
Xing GUO67b4afc2020-06-08 17:30:23 +080051 DWARFYAML::emitDebugSections(StringRef(yamldata), /*ApplyFixups=*/true,
Pavel Labatha03435e2019-11-08 15:24:11 +010052 /*IsLittleEndian=*/true);
53 ASSERT_THAT_EXPECTED(Sections, Succeeded());
54 std::vector<uint8_t> Loclists{
55 // Header
56 0, 0, 0, 0, // Length
57 5, 0, // Version
58 4, // Address size
59 0, // Segment selector size
60 0, 0, 0, 0, // Offset entry count
61 // First location list.
62 DW_LLE_start_length, // First entry
63 1, 0, 0, 0, // Start offset
64 2, // Length
65 0, // Expression length
66 DW_LLE_end_of_list,
67 // Second location list.
68 DW_LLE_startx_length, // First entry
69 1, // Start index
70 2, // Length
71 0, // Expression length
72 DW_LLE_end_of_list,
73 // Third location list.
74 DW_LLE_start_length, // First entry
75 1, 0, 0, 0, // Start offset
76 2, // Length
77 0, // Expression length
78 // end_of_list intentionally missing
79 };
80 Loclists[0] = Loclists.size() - 4;
81 Sections->try_emplace(
82 "debug_loclists",
83 MemoryBuffer::getMemBuffer(toStringRef(Loclists), "debug_loclists",
84 /*RequiresNullTerminator=*/false));
85 std::unique_ptr<DWARFContext> Ctx =
86 DWARFContext::create(*Sections, 4, /*isLittleEndian=*/true);
87 DWARFCompileUnit *CU = Ctx->getCompileUnitForOffset(0);
88 ASSERT_NE(nullptr, CU);
89 DWARFDie Die = CU->getUnitDIE();
90 ASSERT_TRUE(Die.isValid());
91
92 EXPECT_THAT_EXPECTED(Die.getLocations(DW_AT_location),
93 HasValue(testing::ElementsAre(DWARFLocationExpression{
94 DWARFAddressRange{1, 3}, {}})));
95
96 EXPECT_THAT_EXPECTED(
97 Die.getLocations(DW_AT_data_member_location),
98 HasValue(testing::ElementsAre(DWARFLocationExpression{None, {0x47}})));
99
100 EXPECT_THAT_EXPECTED(
101 Die.getLocations(DW_AT_vtable_elem_location),
102 Failed<ErrorInfoBase>(testing::Property(
103 &ErrorInfoBase::message,
104 "Unable to resolve indirect address 1 for: DW_LLE_startx_length")));
105
Pavel Labath5754a612020-02-27 16:22:12 +0100106 EXPECT_THAT_EXPECTED(
107 Die.getLocations(DW_AT_call_data_location),
Pavel Labath04aea762020-04-20 17:28:15 +0200108 FailedWithMessage(
109 "unexpected end of data at offset 0x20 while reading [0x20, 0x21)"));
Pavel Labatha03435e2019-11-08 15:24:11 +0100110
111 EXPECT_THAT_EXPECTED(
112 Die.getLocations(DW_AT_call_data_value),
113 Failed<ErrorInfoBase>(testing::Property(&ErrorInfoBase::message,
114 "No DW_AT_call_data_value")));
115}
116
117} // end anonymous namespace