1*da816ca0SGreg Clayton //===-- SymbolTest.cpp ----------------------------------------------------===//
2*da816ca0SGreg Clayton //
3*da816ca0SGreg Clayton // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*da816ca0SGreg Clayton // See https://llvm.org/LICENSE.txt for license information.
5*da816ca0SGreg Clayton // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*da816ca0SGreg Clayton //
7*da816ca0SGreg Clayton //===----------------------------------------------------------------------===//
8*da816ca0SGreg Clayton
9*da816ca0SGreg Clayton #include "lldb/Symbol/Symbol.h"
10*da816ca0SGreg Clayton #include "lldb/Core/DataFileCache.h"
11*da816ca0SGreg Clayton #include "lldb/Core/Section.h"
12*da816ca0SGreg Clayton #include "lldb/Utility/DataEncoder.h"
13*da816ca0SGreg Clayton #include "lldb/Utility/DataExtractor.h"
14*da816ca0SGreg Clayton
15*da816ca0SGreg Clayton #include "gtest/gtest.h"
16*da816ca0SGreg Clayton
17*da816ca0SGreg Clayton using namespace lldb;
18*da816ca0SGreg Clayton using namespace lldb_private;
19*da816ca0SGreg Clayton
EncodeDecode(const Symbol & object,const SectionList * sect_list,ByteOrder byte_order)20*da816ca0SGreg Clayton static void EncodeDecode(const Symbol &object, const SectionList *sect_list,
21*da816ca0SGreg Clayton ByteOrder byte_order) {
22*da816ca0SGreg Clayton const uint8_t addr_size = 8;
23*da816ca0SGreg Clayton DataEncoder file(byte_order, addr_size);
24*da816ca0SGreg Clayton DataEncoder strtab_encoder(byte_order, addr_size);
25*da816ca0SGreg Clayton ConstStringTable const_strtab;
26*da816ca0SGreg Clayton object.Encode(file, const_strtab);
27*da816ca0SGreg Clayton llvm::ArrayRef<uint8_t> bytes = file.GetData();
28*da816ca0SGreg Clayton DataExtractor data(bytes.data(), bytes.size(), byte_order, addr_size);
29*da816ca0SGreg Clayton
30*da816ca0SGreg Clayton const_strtab.Encode(strtab_encoder);
31*da816ca0SGreg Clayton llvm::ArrayRef<uint8_t> strtab_bytes = strtab_encoder.GetData();
32*da816ca0SGreg Clayton DataExtractor strtab_data(strtab_bytes.data(), strtab_bytes.size(),
33*da816ca0SGreg Clayton byte_order, addr_size);
34*da816ca0SGreg Clayton StringTableReader strtab_reader;
35*da816ca0SGreg Clayton offset_t strtab_data_offset = 0;
36*da816ca0SGreg Clayton ASSERT_EQ(strtab_reader.Decode(strtab_data, &strtab_data_offset), true);
37*da816ca0SGreg Clayton
38*da816ca0SGreg Clayton Symbol decoded_object;
39*da816ca0SGreg Clayton offset_t data_offset = 0;
40*da816ca0SGreg Clayton decoded_object.Decode(data, &data_offset, sect_list, strtab_reader);
41*da816ca0SGreg Clayton EXPECT_EQ(object, decoded_object);
42*da816ca0SGreg Clayton }
43*da816ca0SGreg Clayton
EncodeDecode(const Symbol & object,const SectionList * sect_list)44*da816ca0SGreg Clayton static void EncodeDecode(const Symbol &object, const SectionList *sect_list) {
45*da816ca0SGreg Clayton EncodeDecode(object, sect_list, eByteOrderLittle);
46*da816ca0SGreg Clayton EncodeDecode(object, sect_list, eByteOrderBig);
47*da816ca0SGreg Clayton }
48*da816ca0SGreg Clayton
TEST(SymbolTest,EncodeDecodeSymbol)49*da816ca0SGreg Clayton TEST(SymbolTest, EncodeDecodeSymbol) {
50*da816ca0SGreg Clayton
51*da816ca0SGreg Clayton SectionSP sect_sp(new Section(
52*da816ca0SGreg Clayton /*module_sp=*/ModuleSP(),
53*da816ca0SGreg Clayton /*obj_file=*/nullptr,
54*da816ca0SGreg Clayton /*sect_id=*/1,
55*da816ca0SGreg Clayton /*name=*/ConstString(".text"),
56*da816ca0SGreg Clayton /*sect_type=*/eSectionTypeCode,
57*da816ca0SGreg Clayton /*file_vm_addr=*/0x1000,
58*da816ca0SGreg Clayton /*vm_size=*/0x1000,
59*da816ca0SGreg Clayton /*file_offset=*/0,
60*da816ca0SGreg Clayton /*file_size=*/0,
61*da816ca0SGreg Clayton /*log2align=*/5,
62*da816ca0SGreg Clayton /*flags=*/0x10203040));
63*da816ca0SGreg Clayton
64*da816ca0SGreg Clayton SectionList sect_list;
65*da816ca0SGreg Clayton sect_list.AddSection(sect_sp);
66*da816ca0SGreg Clayton
67*da816ca0SGreg Clayton Symbol symbol(
68*da816ca0SGreg Clayton /*symID=*/0x10203040,
69*da816ca0SGreg Clayton /*name=*/"main",
70*da816ca0SGreg Clayton /*type=*/eSymbolTypeCode,
71*da816ca0SGreg Clayton /*bool external=*/false,
72*da816ca0SGreg Clayton /*bool is_debug=*/false,
73*da816ca0SGreg Clayton /*bool is_trampoline=*/false,
74*da816ca0SGreg Clayton /*bool is_artificial=*/false,
75*da816ca0SGreg Clayton /*section_sp=*/sect_sp,
76*da816ca0SGreg Clayton /*offset=*/0x0,
77*da816ca0SGreg Clayton /*size=*/0x100,
78*da816ca0SGreg Clayton /*size_is_valid=*/true,
79*da816ca0SGreg Clayton /*contains_linker_annotations=*/false,
80*da816ca0SGreg Clayton /*flags=*/0x11223344);
81*da816ca0SGreg Clayton
82*da816ca0SGreg Clayton // Test encoding a symbol with an address.
83*da816ca0SGreg Clayton EncodeDecode(symbol, §_list);
84*da816ca0SGreg Clayton
85*da816ca0SGreg Clayton // Test that encoding the bits in the bitfield works for all endianness
86*da816ca0SGreg Clayton // combos.
87*da816ca0SGreg Clayton
88*da816ca0SGreg Clayton // Test Symbol.m_is_synthetic
89*da816ca0SGreg Clayton symbol.SetIsSynthetic(true);
90*da816ca0SGreg Clayton EncodeDecode(symbol, §_list);
91*da816ca0SGreg Clayton symbol.SetIsSynthetic(false);
92*da816ca0SGreg Clayton
93*da816ca0SGreg Clayton // Test Symbol.m_is_debug
94*da816ca0SGreg Clayton symbol.SetDebug(true);
95*da816ca0SGreg Clayton EncodeDecode(symbol, §_list);
96*da816ca0SGreg Clayton symbol.SetDebug(false);
97*da816ca0SGreg Clayton
98*da816ca0SGreg Clayton // Test Symbol.m_is_external
99*da816ca0SGreg Clayton symbol.SetExternal(true);
100*da816ca0SGreg Clayton EncodeDecode(symbol, §_list);
101*da816ca0SGreg Clayton symbol.SetExternal(false);
102*da816ca0SGreg Clayton
103*da816ca0SGreg Clayton // Test Symbol.m_size_is_sibling
104*da816ca0SGreg Clayton symbol.SetSizeIsSibling(true);
105*da816ca0SGreg Clayton EncodeDecode(symbol, §_list);
106*da816ca0SGreg Clayton symbol.SetSizeIsSibling(false);
107*da816ca0SGreg Clayton
108*da816ca0SGreg Clayton // Test Symbol.m_size_is_synthesized
109*da816ca0SGreg Clayton symbol.SetSizeIsSynthesized(true);
110*da816ca0SGreg Clayton EncodeDecode(symbol, §_list);
111*da816ca0SGreg Clayton symbol.SetSizeIsSynthesized(false);
112*da816ca0SGreg Clayton
113*da816ca0SGreg Clayton // Test Symbol.m_size_is_synthesized
114*da816ca0SGreg Clayton symbol.SetByteSize(0);
115*da816ca0SGreg Clayton EncodeDecode(symbol, §_list);
116*da816ca0SGreg Clayton symbol.SetByteSize(0x100);
117*da816ca0SGreg Clayton
118*da816ca0SGreg Clayton // Test Symbol.m_demangled_is_synthesized
119*da816ca0SGreg Clayton symbol.SetDemangledNameIsSynthesized(true);
120*da816ca0SGreg Clayton EncodeDecode(symbol, §_list);
121*da816ca0SGreg Clayton symbol.SetDemangledNameIsSynthesized(false);
122*da816ca0SGreg Clayton
123*da816ca0SGreg Clayton // Test Symbol.m_contains_linker_annotations
124*da816ca0SGreg Clayton symbol.SetContainsLinkerAnnotations(true);
125*da816ca0SGreg Clayton EncodeDecode(symbol, §_list);
126*da816ca0SGreg Clayton symbol.SetContainsLinkerAnnotations(false);
127*da816ca0SGreg Clayton
128*da816ca0SGreg Clayton // Test Symbol.m_is_weak
129*da816ca0SGreg Clayton symbol.SetIsWeak(true);
130*da816ca0SGreg Clayton EncodeDecode(symbol, §_list);
131*da816ca0SGreg Clayton symbol.SetIsWeak(false);
132*da816ca0SGreg Clayton
133*da816ca0SGreg Clayton // Test encoding a symbol with no address.
134*da816ca0SGreg Clayton symbol.GetAddressRef().SetSection(SectionSP());
135*da816ca0SGreg Clayton EncodeDecode(symbol, §_list);
136*da816ca0SGreg Clayton }
137