1 //===- DWARFDataExtractorTest.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/DebugInfo/DWARF/DWARFDataExtractor.h"
10 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
11 #include "llvm/Object/ObjectFile.h"
12 #include "llvm/ObjectYAML/yaml2obj.h"
13 #include "llvm/Testing/Support/Error.h"
14 #include "gtest/gtest.h"
15 
16 using namespace llvm;
17 
18 namespace {
19 
20 TEST(DWARFDataExtractorTest, getRelocatedValue) {
21   StringRef Yaml = R"(
22 !ELF
23 FileHeader:
24   Class:    ELFCLASS32
25   Data:     ELFDATA2LSB
26   Type:     ET_REL
27   Machine:  EM_386
28 Sections:
29   - Name:     .text
30     Type:     SHT_PROGBITS
31     Size:     0x80
32   - Name:     .debug_line
33     Type:     SHT_PROGBITS
34     Content:  '000000000000'
35   - Name:     .rel.debug_line
36     Type:     SHT_REL
37     Info:     .debug_line
38     Relocations:
39       - Offset:   0
40         Symbol:   f
41         Type:     R_386_32
42       - Offset:   4
43         Symbol:   f
44         Type:     R_386_32
45 Symbols:
46   - Name:     f
47     Type:     STT_SECTION
48     Section:  .text
49     Value:    0x42
50 )";
51   SmallString<0> Storage;
52   std::unique_ptr<object::ObjectFile> Obj = yaml::yaml2ObjectFile(
53       Storage, Yaml, [](const Twine &Err) { errs() << Err; });
54   ASSERT_TRUE(Obj);
55   std::unique_ptr<DWARFContext> Ctx = DWARFContext::create(*Obj);
56   const DWARFObject &DObj = Ctx->getDWARFObj();
57   ASSERT_EQ(6u, DObj.getLineSection().Data.size());
58 
59   DWARFDataExtractor Data(DObj, DObj.getLineSection(), Obj->isLittleEndian(),
60                           Obj->getBytesInAddress());
61   DataExtractor::Cursor C(0);
62   EXPECT_EQ(0x42u, Data.getRelocatedAddress(C));
63   EXPECT_EQ(0u, Data.getRelocatedAddress(C));
64   EXPECT_THAT_ERROR(C.takeError(),
65                     FailedWithMessage("unexpected end of data at offset 0x4"));
66 }
67 
68 TEST(DWARFDataExtractorTest, getInitialLength) {
69   auto GetWithError = [](ArrayRef<uint8_t> Bytes)
70       -> Expected<std::tuple<uint64_t, dwarf::DwarfFormat, uint64_t>> {
71     DWARFDataExtractor Data(Bytes, /*IsLittleEndian=*/false, /*AddressSize=*/8);
72     DWARFDataExtractor::Cursor C(0);
73     uint64_t Length;
74     dwarf::DwarfFormat Format;
75     std::tie(Length, Format) = Data.getInitialLength(C);
76     if (C)
77       return std::make_tuple(Length, Format, C.tell());
78 
79     EXPECT_EQ(Length, 0u);
80     EXPECT_EQ(Format, dwarf::DWARF32);
81     EXPECT_EQ(C.tell(), 0u);
82     return C.takeError();
83   };
84   auto GetWithoutError = [](ArrayRef<uint8_t> Bytes) {
85     DWARFDataExtractor Data(Bytes, /*IsLittleEndian=*/false, /*AddressSize=*/8);
86     uint64_t Offset = 0;
87     uint64_t Length;
88     dwarf::DwarfFormat Format;
89     std::tie(Length, Format) = Data.getInitialLength(&Offset);
90     return std::make_tuple(Length, Format, Offset);
91   };
92   auto ErrorResult = std::make_tuple(0, dwarf::DWARF32, 0);
93 
94   // Empty data.
95   EXPECT_THAT_EXPECTED(
96       GetWithError({}),
97       FailedWithMessage("unexpected end of data at offset 0x0"));
98   EXPECT_EQ(GetWithoutError({}), ErrorResult);
99 
100   // Not long enough for the U32 field.
101   EXPECT_THAT_EXPECTED(
102       GetWithError({0x00, 0x01, 0x02}),
103       FailedWithMessage("unexpected end of data at offset 0x0"));
104   EXPECT_EQ(GetWithoutError({0x00, 0x01, 0x02}), ErrorResult);
105 
106   EXPECT_THAT_EXPECTED(
107       GetWithError({0x00, 0x01, 0x02, 0x03}),
108       HasValue(std::make_tuple(0x00010203, dwarf::DWARF32, 4)));
109   EXPECT_EQ(GetWithoutError({0x00, 0x01, 0x02, 0x03}),
110             std::make_tuple(0x00010203, dwarf::DWARF32, 4));
111 
112   // Zeroes are not an error, but without the Error object it is hard to tell
113   // them apart from a failed read.
114   EXPECT_THAT_EXPECTED(
115       GetWithError({0x00, 0x00, 0x00, 0x00}),
116       HasValue(std::make_tuple(0x00000000, dwarf::DWARF32, 4)));
117   EXPECT_EQ(GetWithoutError({0x00, 0x00, 0x00, 0x00}),
118             std::make_tuple(0x00000000, dwarf::DWARF32, 4));
119 
120   // Smallest invalid value.
121   EXPECT_THAT_EXPECTED(
122       GetWithError({0xff, 0xff, 0xff, 0xf0}),
123       FailedWithMessage(
124           "unsupported reserved unit length of value 0xfffffff0"));
125   EXPECT_EQ(GetWithoutError({0xff, 0xff, 0xff, 0xf0}), ErrorResult);
126 
127   // DWARF64 marker without the subsequent length field.
128   EXPECT_THAT_EXPECTED(
129       GetWithError({0xff, 0xff, 0xff, 0xff}),
130       FailedWithMessage("unexpected end of data at offset 0x4"));
131   EXPECT_EQ(GetWithoutError({0xff, 0xff, 0xff, 0xff}), ErrorResult);
132 
133   // Not enough data for the U64 length.
134   EXPECT_THAT_EXPECTED(
135       GetWithError({0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03}),
136       FailedWithMessage("unexpected end of data at offset 0x4"));
137   EXPECT_EQ(GetWithoutError({0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03}),
138             ErrorResult);
139 
140   EXPECT_THAT_EXPECTED(
141       GetWithError({0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
142                     0x06, 0x07}),
143       HasValue(std::make_tuple(0x0001020304050607, dwarf::DWARF64, 12)));
144   EXPECT_EQ(GetWithoutError({0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03,
145                              0x04, 0x05, 0x06, 0x07}),
146             std::make_tuple(0x0001020304050607, dwarf::DWARF64, 12));
147 }
148 
149 } // namespace
150