blob: 582b58062c4568942a2ddeb9006b2ef41675d67e [file] [log] [blame]
Alexey Samsonov045c6c52013-04-17 08:29:02 +00001//===- llvm/unittest/DebugInfo/DWARFFormValueTest.cpp ---------------------===//
2//
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
Zachary Turner82af9432015-01-30 18:07:45 +000010#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000011#include "llvm/ADT/ArrayRef.h"
Frederic Riss77f850e2015-03-04 22:07:41 +000012#include "llvm/ADT/SmallString.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000013#include "llvm/BinaryFormat/Dwarf.h"
Frederic Riss77f850e2015-03-04 22:07:41 +000014#include "llvm/Support/Host.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000015#include "llvm/Support/LEB128.h"
Alexey Samsonov045c6c52013-04-17 08:29:02 +000016#include "gtest/gtest.h"
Benjamin Kramer16132e62015-03-23 18:07:13 +000017#include <climits>
Alexey Samsonov045c6c52013-04-17 08:29:02 +000018using namespace llvm;
19using namespace dwarf;
20
21namespace {
22
Greg Clayton6c273762016-10-27 16:32:04 +000023bool isFormClass(dwarf::Form Form, DWARFFormValue::FormClass FC) {
Alexey Samsonov48cbda52013-10-28 23:01:48 +000024 return DWARFFormValue(Form).isFormClass(FC);
25}
26
27TEST(DWARFFormValue, FormClass) {
28 EXPECT_TRUE(isFormClass(DW_FORM_addr, DWARFFormValue::FC_Address));
29 EXPECT_FALSE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_Address));
30 EXPECT_TRUE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_Constant));
31 EXPECT_TRUE(isFormClass(DW_FORM_data8, DWARFFormValue::FC_SectionOffset));
32 EXPECT_TRUE(
33 isFormClass(DW_FORM_sec_offset, DWARFFormValue::FC_SectionOffset));
34 EXPECT_TRUE(isFormClass(DW_FORM_GNU_str_index, DWARFFormValue::FC_String));
35 EXPECT_TRUE(isFormClass(DW_FORM_GNU_addr_index, DWARFFormValue::FC_Address));
36 EXPECT_FALSE(isFormClass(DW_FORM_ref_addr, DWARFFormValue::FC_Address));
37 EXPECT_TRUE(isFormClass(DW_FORM_ref_addr, DWARFFormValue::FC_Reference));
Alexey Samsonovcbd806a2013-10-29 16:32:19 +000038 EXPECT_TRUE(isFormClass(DW_FORM_ref_sig8, DWARFFormValue::FC_Reference));
Alexey Samsonov48cbda52013-10-28 23:01:48 +000039}
40
Frederic Riss77f850e2015-03-04 22:07:41 +000041template<typename RawTypeT>
Greg Clayton6c273762016-10-27 16:32:04 +000042DWARFFormValue createDataXFormValue(dwarf::Form Form, RawTypeT Value) {
Frederic Riss77f850e2015-03-04 22:07:41 +000043 char Raw[sizeof(RawTypeT)];
44 memcpy(Raw, &Value, sizeof(RawTypeT));
45 uint32_t Offset = 0;
46 DWARFFormValue Result(Form);
Paul Robinson17536b92017-06-29 16:52:08 +000047 DWARFDataExtractor Data(StringRef(Raw, sizeof(RawTypeT)),
48 sys::IsLittleEndianHost, sizeof(void *));
Paul Robinsone5400f82017-11-07 19:57:12 +000049 Result.extractValue(Data, &Offset, {0, 0, dwarf::DwarfFormat::DWARF32});
Frederic Riss77f850e2015-03-04 22:07:41 +000050 return Result;
51}
52
53DWARFFormValue createULEBFormValue(uint64_t Value) {
54 SmallString<10> RawData;
55 raw_svector_ostream OS(RawData);
56 encodeULEB128(Value, OS);
57 uint32_t Offset = 0;
58 DWARFFormValue Result(DW_FORM_udata);
Paul Robinson17536b92017-06-29 16:52:08 +000059 DWARFDataExtractor Data(OS.str(), sys::IsLittleEndianHost, sizeof(void *));
Paul Robinsone5400f82017-11-07 19:57:12 +000060 Result.extractValue(Data, &Offset, {0, 0, dwarf::DwarfFormat::DWARF32});
Frederic Riss77f850e2015-03-04 22:07:41 +000061 return Result;
62}
63
64DWARFFormValue createSLEBFormValue(int64_t Value) {
65 SmallString<10> RawData;
66 raw_svector_ostream OS(RawData);
67 encodeSLEB128(Value, OS);
68 uint32_t Offset = 0;
69 DWARFFormValue Result(DW_FORM_sdata);
Paul Robinson17536b92017-06-29 16:52:08 +000070 DWARFDataExtractor Data(OS.str(), sys::IsLittleEndianHost, sizeof(void *));
Paul Robinsone5400f82017-11-07 19:57:12 +000071 Result.extractValue(Data, &Offset, {0, 0, dwarf::DwarfFormat::DWARF32});
Frederic Riss77f850e2015-03-04 22:07:41 +000072 return Result;
73}
74
75TEST(DWARFFormValue, SignedConstantForms) {
76 // Check that we correctly sign extend fixed size forms.
77 auto Sign1 = createDataXFormValue<uint8_t>(DW_FORM_data1, -123);
78 auto Sign2 = createDataXFormValue<uint16_t>(DW_FORM_data2, -12345);
79 auto Sign4 = createDataXFormValue<uint32_t>(DW_FORM_data4, -123456789);
80 auto Sign8 = createDataXFormValue<uint64_t>(DW_FORM_data8, -1);
81 EXPECT_EQ(Sign1.getAsSignedConstant().getValue(), -123);
82 EXPECT_EQ(Sign2.getAsSignedConstant().getValue(), -12345);
83 EXPECT_EQ(Sign4.getAsSignedConstant().getValue(), -123456789);
84 EXPECT_EQ(Sign8.getAsSignedConstant().getValue(), -1);
85
86 // Check that we can handle big positive values, but that we return
87 // an error just over the limit.
88 auto UMax = createULEBFormValue(LLONG_MAX);
Frederic Riss0d94ef92015-03-05 14:43:15 +000089 auto TooBig = createULEBFormValue(uint64_t(LLONG_MAX) + 1);
Frederic Riss77f850e2015-03-04 22:07:41 +000090 EXPECT_EQ(UMax.getAsSignedConstant().getValue(), LLONG_MAX);
91 EXPECT_EQ(TooBig.getAsSignedConstant().hasValue(), false);
92
93 // Sanity check some other forms.
94 auto Data1 = createDataXFormValue<uint8_t>(DW_FORM_data1, 120);
95 auto Data2 = createDataXFormValue<uint16_t>(DW_FORM_data2, 32000);
96 auto Data4 = createDataXFormValue<uint32_t>(DW_FORM_data4, 2000000000);
97 auto Data8 = createDataXFormValue<uint64_t>(DW_FORM_data8, 0x1234567812345678LL);
98 auto LEBMin = createSLEBFormValue(LLONG_MIN);
99 auto LEBMax = createSLEBFormValue(LLONG_MAX);
100 auto LEB1 = createSLEBFormValue(-42);
101 auto LEB2 = createSLEBFormValue(42);
102 EXPECT_EQ(Data1.getAsSignedConstant().getValue(), 120);
103 EXPECT_EQ(Data2.getAsSignedConstant().getValue(), 32000);
104 EXPECT_EQ(Data4.getAsSignedConstant().getValue(), 2000000000);
105 EXPECT_EQ(Data8.getAsSignedConstant().getValue(), 0x1234567812345678LL);
106 EXPECT_EQ(LEBMin.getAsSignedConstant().getValue(), LLONG_MIN);
107 EXPECT_EQ(LEBMax.getAsSignedConstant().getValue(), LLONG_MAX);
108 EXPECT_EQ(LEB1.getAsSignedConstant().getValue(), -42);
109 EXPECT_EQ(LEB2.getAsSignedConstant().getValue(), 42);
Paul Robinsona06f8dc2017-12-18 19:08:35 +0000110
111 // Data16 is a little tricky.
112 char Cksum[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
113 DWARFFormValue Data16(DW_FORM_data16);
114 DWARFDataExtractor DE16(StringRef(Cksum, 16), sys::IsLittleEndianHost,
115 sizeof(void *));
116 uint32_t Offset = 0;
117 Data16.extractValue(DE16, &Offset, {0, 0, dwarf::DwarfFormat::DWARF32});
118 SmallString<32> Str;
119 raw_svector_ostream Res(Str);
120 Data16.dump(Res, DIDumpOptions());
121 EXPECT_EQ(memcmp(Str.data(), "000102030405060708090a0b0c0d0e0f", 32), 0);
Frederic Riss77f850e2015-03-04 22:07:41 +0000122}
123
Alexey Samsonov045c6c52013-04-17 08:29:02 +0000124} // end anonymous namespace