1 //===-- TestObjectFileELF.cpp -----------------------------------*- C++ -*-===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
11 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
12 #include "TestingSupport/SubsystemRAII.h"
13 #include "TestingSupport/TestUtilities.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/ModuleSpec.h"
16 #include "lldb/Core/Section.h"
17 #include "lldb/Host/FileSystem.h"
18 #include "lldb/Host/HostInfo.h"
19 #include "lldb/Utility/DataBufferHeap.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/Support/Compression.h"
22 #include "llvm/Support/FileUtilities.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/Program.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Testing/Support/Error.h"
27 #include "gtest/gtest.h"
29 using namespace lldb_private
;
32 class ObjectFileELFTest
: public testing::Test
{
33 SubsystemRAII
<FileSystem
, HostInfo
, ObjectFileELF
, SymbolFileSymtab
>
37 TEST_F(ObjectFileELFTest
, SectionsResolveConsistently
) {
38 auto ExpectedFile
= TestFile::fromYaml(R
"(
45 Entry: 0x0000000000400180
47 - Name: .note.gnu.build-id
50 Address: 0x0000000000400158
51 AddressAlign: 0x0000000000000004
52 Content: 040000001400000003000000474E55003F3EC29E3FD83E49D18C4D49CD8A730CC13117B6
55 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
56 Address: 0x0000000000400180
57 AddressAlign: 0x0000000000000010
58 Content: 554889E58B042500106000890425041060005DC3
61 Flags: [ SHF_WRITE, SHF_ALLOC ]
62 Address: 0x0000000000601000
63 AddressAlign: 0x0000000000000004
67 Flags: [ SHF_WRITE, SHF_ALLOC ]
68 Address: 0x0000000000601004
69 AddressAlign: 0x0000000000000004
70 Size: 0x0000000000000004
75 Value: 0x0000000000601000
76 Size: 0x0000000000000004
81 Value: 0x0000000000400180
82 Size: 0x0000000000000014
87 Value: 0x0000000000601004
88 Size: 0x0000000000000004
92 ASSERT_THAT_EXPECTED(ExpectedFile
, llvm::Succeeded());
94 ModuleSpec spec
{FileSpec(ExpectedFile
->name())};
95 spec
.GetSymbolFileSpec().SetFile(ExpectedFile
->name(),
96 FileSpec::Style::native
);
97 auto module_sp
= std::make_shared
<Module
>(spec
);
98 SectionList
*list
= module_sp
->GetSectionList();
99 ASSERT_NE(nullptr, list
);
101 auto bss_sp
= list
->FindSectionByName(ConstString(".bss"));
102 ASSERT_NE(nullptr, bss_sp
);
103 auto data_sp
= list
->FindSectionByName(ConstString(".data"));
104 ASSERT_NE(nullptr, data_sp
);
105 auto text_sp
= list
->FindSectionByName(ConstString(".text"));
106 ASSERT_NE(nullptr, text_sp
);
108 const Symbol
*X
= module_sp
->FindFirstSymbolWithNameAndType(ConstString("X"),
110 ASSERT_NE(nullptr, X
);
111 EXPECT_EQ(bss_sp
, X
->GetAddress().GetSection());
113 const Symbol
*Y
= module_sp
->FindFirstSymbolWithNameAndType(ConstString("Y"),
115 ASSERT_NE(nullptr, Y
);
116 EXPECT_EQ(data_sp
, Y
->GetAddress().GetSection());
118 const Symbol
*start
= module_sp
->FindFirstSymbolWithNameAndType(
119 ConstString("_start"), eSymbolTypeAny
);
120 ASSERT_NE(nullptr, start
);
121 EXPECT_EQ(text_sp
, start
->GetAddress().GetSection());
124 // Test that GetModuleSpecifications works on an "atypical" object file which
125 // has section headers right after the ELF header (instead of the more common
126 // layout where the section headers are at the very end of the object file).
128 // Test file generated with yaml2obj (@svn rev 324254) from the following input:
136 Entry: 0x00000000004003D0
138 - Name: .note.gnu.build-id
141 Address: 0x0000000000400274
142 AddressAlign: 0x0000000000000004
143 Content: 040000001400000003000000474E55001B8A73AC238390E32A7FF4AC8EBE4D6A41ECF5C9
146 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
147 Address: 0x00000000004003D0
148 AddressAlign: 0x0000000000000010
149 Content: DEADBEEFBAADF00D
152 TEST_F(ObjectFileELFTest
, GetModuleSpecifications_EarlySectionHeaders
) {
153 std::string SO
= GetInputFilePath("early-section-headers.so");
154 ModuleSpecList Specs
;
155 ASSERT_EQ(1u, ObjectFile::GetModuleSpecifications(FileSpec(SO
), 0, 0, Specs
));
157 ASSERT_TRUE(Specs
.GetModuleSpecAtIndex(0, Spec
)) ;
159 Uuid
.SetFromStringRef("1b8a73ac238390e32a7ff4ac8ebe4d6a41ecf5c9", 20);
160 EXPECT_EQ(Spec
.GetUUID(), Uuid
);
163 TEST_F(ObjectFileELFTest
, GetSymtab_NoSymEntryPointArmThumbAddressClass
) {
165 // nosym-entrypoint-arm-thumb.s
171 // arm-linux-androideabi-as nosym-entrypoint-arm-thumb.s
172 // -o nosym-entrypoint-arm-thumb.o
173 // arm-linux-androideabi-ld nosym-entrypoint-arm-thumb.o
174 // -o nosym-entrypoint-arm-thumb -e 0x8075 -s
176 auto ExpectedFile
= TestFile::fromYaml(R
"(
183 Flags: [ EF_ARM_SOFT_FLOAT, EF_ARM_EABI_VER5 ]
184 Entry: 0x0000000000008075
188 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
189 Address: 0x0000000000008074
190 AddressAlign: 0x0000000000000002
191 Content: 2A20012700DF
194 Flags: [ SHF_WRITE, SHF_ALLOC ]
195 Address: 0x0000000000009000
196 AddressAlign: 0x0000000000000001
200 Flags: [ SHF_WRITE, SHF_ALLOC ]
201 Address: 0x0000000000009000
202 AddressAlign: 0x0000000000000001
203 - Name: .note.gnu.gold-version
205 AddressAlign: 0x0000000000000004
206 Content: 040000000900000004000000474E5500676F6C6420312E3131000000
207 - Name: .ARM.attributes
208 Type: SHT_ARM_ATTRIBUTES
209 AddressAlign: 0x0000000000000001
210 Content: '4113000000616561626900010900000006020901'
213 ASSERT_THAT_EXPECTED(ExpectedFile
, llvm::Succeeded());
215 ModuleSpec spec
{FileSpec(ExpectedFile
->name())};
216 spec
.GetSymbolFileSpec().SetFile(ExpectedFile
->name(),
217 FileSpec::Style::native
);
218 auto module_sp
= std::make_shared
<Module
>(spec
);
220 auto entry_point_addr
= module_sp
->GetObjectFile()->GetEntryPointAddress();
221 ASSERT_TRUE(entry_point_addr
.GetOffset() & 1);
222 // Decrease the offsite by 1 to make it into a breakable address since this
224 entry_point_addr
.SetOffset(entry_point_addr
.GetOffset() - 1);
225 ASSERT_EQ(entry_point_addr
.GetAddressClass(),
226 AddressClass::eCodeAlternateISA
);
229 TEST_F(ObjectFileELFTest
, GetSymtab_NoSymEntryPointArmAddressClass
) {
231 // nosym-entrypoint-arm.s
236 // arm-linux-androideabi-as nosym-entrypoint-arm.s
237 // -o nosym-entrypoint-arm.o
238 // arm-linux-androideabi-ld nosym-entrypoint-arm.o
239 // -o nosym-entrypoint-arm -e 0x8074 -s
241 auto ExpectedFile
= TestFile::fromYaml(R
"(
248 Flags: [ EF_ARM_SOFT_FLOAT, EF_ARM_EABI_VER5 ]
249 Entry: 0x0000000000008074
253 Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
254 Address: 0x0000000000008074
255 AddressAlign: 0x0000000000000004
256 Content: 2A00A0E30170A0E3000000EF
259 Flags: [ SHF_WRITE, SHF_ALLOC ]
260 Address: 0x0000000000009000
261 AddressAlign: 0x0000000000000001
265 Flags: [ SHF_WRITE, SHF_ALLOC ]
266 Address: 0x0000000000009000
267 AddressAlign: 0x0000000000000001
268 - Name: .note.gnu.gold-version
270 AddressAlign: 0x0000000000000004
271 Content: 040000000900000004000000474E5500676F6C6420312E3131000000
272 - Name: .ARM.attributes
273 Type: SHT_ARM_ATTRIBUTES
274 AddressAlign: 0x0000000000000001
275 Content: '4113000000616561626900010900000006010801'
278 ASSERT_THAT_EXPECTED(ExpectedFile
, llvm::Succeeded());
280 ModuleSpec spec
{FileSpec(ExpectedFile
->name())};
281 spec
.GetSymbolFileSpec().SetFile(ExpectedFile
->name(),
282 FileSpec::Style::native
);
283 auto module_sp
= std::make_shared
<Module
>(spec
);
285 auto entry_point_addr
= module_sp
->GetObjectFile()->GetEntryPointAddress();
286 ASSERT_EQ(entry_point_addr
.GetAddressClass(), AddressClass::eCode
);