blob: 487d39c87cbd610476f668a1e9071488b12c7965 [file] [log] [blame]
Christopher Ferris3958f802017-02-01 15:44:40 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <elf.h>
18
19#include <memory>
20
21#include <gtest/gtest.h>
22
Christopher Ferrisd226a512017-07-14 10:37:19 -070023#include <unwindstack/ElfInterface.h>
24
25#include "DwarfEncoding.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080026#include "ElfInterfaceArm.h"
27
Christopher Ferrise69f4702017-10-19 16:08:58 -070028#include "ElfFake.h"
Christopher Ferris3958f802017-02-01 15:44:40 -080029#include "MemoryFake.h"
30
31#if !defined(PT_ARM_EXIDX)
32#define PT_ARM_EXIDX 0x70000001
33#endif
34
35#if !defined(EM_AARCH64)
36#define EM_AARCH64 183
37#endif
38
Christopher Ferrisd226a512017-07-14 10:37:19 -070039namespace unwindstack {
40
Christopher Ferris3958f802017-02-01 15:44:40 -080041class ElfInterfaceTest : public ::testing::Test {
42 protected:
43 void SetUp() override {
44 memory_.Clear();
45 }
46
47 void SetStringMemory(uint64_t offset, const char* string) {
48 memory_.SetMemory(offset, string, strlen(string) + 1);
49 }
50
51 template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
52 void SinglePtLoad();
53
54 template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
55 void MultipleExecutablePtLoads();
56
57 template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
58 void MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr();
59
60 template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
61 void NonExecutablePtLoads();
62
63 template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
64 void ManyPhdrs();
65
Christopher Ferrisbeae42b2018-02-15 17:36:33 -080066 enum SonameTestEnum : uint8_t {
67 SONAME_NORMAL,
68 SONAME_DTNULL_AFTER,
69 SONAME_DTSIZE_SMALL,
70 SONAME_MISSING_MAP,
71 };
72
73 template <typename Ehdr, typename Phdr, typename Shdr, typename Dyn>
74 void SonameInit(SonameTestEnum test_type = SONAME_NORMAL);
75
76 template <typename ElfInterfaceType>
Christopher Ferris3958f802017-02-01 15:44:40 -080077 void Soname();
78
Christopher Ferrisbeae42b2018-02-15 17:36:33 -080079 template <typename ElfInterfaceType>
Christopher Ferris3958f802017-02-01 15:44:40 -080080 void SonameAfterDtNull();
81
Christopher Ferrisbeae42b2018-02-15 17:36:33 -080082 template <typename ElfInterfaceType>
Christopher Ferris3958f802017-02-01 15:44:40 -080083 void SonameSize();
84
Christopher Ferrisbeae42b2018-02-15 17:36:33 -080085 template <typename ElfInterfaceType>
86 void SonameMissingMap();
87
Christopher Ferris61d40972017-06-12 19:14:20 -070088 template <typename ElfType>
89 void InitHeadersEhFrameTest();
90
91 template <typename ElfType>
92 void InitHeadersDebugFrame();
93
94 template <typename ElfType>
95 void InitHeadersEhFrameFail();
96
97 template <typename ElfType>
98 void InitHeadersDebugFrameFail();
99
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700100 template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
101 void InitSectionHeadersMalformed();
102
103 template <typename Ehdr, typename Shdr, typename Sym, typename ElfInterfaceType>
104 void InitSectionHeaders(uint64_t entry_size);
105
106 template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
107 void InitSectionHeadersOffsets();
108
109 template <typename Sym>
110 void InitSym(uint64_t offset, uint32_t value, uint32_t size, uint32_t name_offset,
111 uint64_t sym_offset, const char* name);
112
Christopher Ferris3958f802017-02-01 15:44:40 -0800113 MemoryFake memory_;
114};
115
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700116template <typename Sym>
117void ElfInterfaceTest::InitSym(uint64_t offset, uint32_t value, uint32_t size, uint32_t name_offset,
118 uint64_t sym_offset, const char* name) {
Christopher Ferrisf882a382018-06-22 16:48:02 -0700119 Sym sym = {};
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700120 sym.st_info = STT_FUNC;
121 sym.st_value = value;
122 sym.st_size = size;
123 sym.st_name = name_offset;
124 sym.st_shndx = SHN_COMMON;
125
126 memory_.SetMemory(offset, &sym, sizeof(sym));
127 memory_.SetMemory(sym_offset + name_offset, name, strlen(name) + 1);
128}
129
Christopher Ferris3958f802017-02-01 15:44:40 -0800130template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
131void ElfInterfaceTest::SinglePtLoad() {
132 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
133
Christopher Ferrisf882a382018-06-22 16:48:02 -0700134 Ehdr ehdr = {};
Christopher Ferris3958f802017-02-01 15:44:40 -0800135 ehdr.e_phoff = 0x100;
136 ehdr.e_phnum = 1;
137 ehdr.e_phentsize = sizeof(Phdr);
138 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
139
Christopher Ferrisf882a382018-06-22 16:48:02 -0700140 Phdr phdr = {};
Christopher Ferris3958f802017-02-01 15:44:40 -0800141 phdr.p_type = PT_LOAD;
142 phdr.p_vaddr = 0x2000;
143 phdr.p_memsz = 0x10000;
144 phdr.p_flags = PF_R | PF_X;
145 phdr.p_align = 0x1000;
146 memory_.SetMemory(0x100, &phdr, sizeof(phdr));
147
Christopher Ferrise69f4702017-10-19 16:08:58 -0700148 uint64_t load_bias = 0;
149 ASSERT_TRUE(elf->Init(&load_bias));
150 EXPECT_EQ(0x2000U, load_bias);
Christopher Ferris3958f802017-02-01 15:44:40 -0800151
152 const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads();
153 ASSERT_EQ(1U, pt_loads.size());
154 LoadInfo load_data = pt_loads.at(0);
155 ASSERT_EQ(0U, load_data.offset);
156 ASSERT_EQ(0x2000U, load_data.table_offset);
157 ASSERT_EQ(0x10000U, load_data.table_size);
158}
159
160TEST_F(ElfInterfaceTest, elf32_single_pt_load) {
161 SinglePtLoad<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>();
162}
163
164TEST_F(ElfInterfaceTest, elf64_single_pt_load) {
165 SinglePtLoad<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>();
166}
167
168template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
169void ElfInterfaceTest::MultipleExecutablePtLoads() {
170 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
171
Christopher Ferrisf882a382018-06-22 16:48:02 -0700172 Ehdr ehdr = {};
Christopher Ferris3958f802017-02-01 15:44:40 -0800173 ehdr.e_phoff = 0x100;
174 ehdr.e_phnum = 3;
175 ehdr.e_phentsize = sizeof(Phdr);
176 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
177
Christopher Ferrisf882a382018-06-22 16:48:02 -0700178 Phdr phdr = {};
Christopher Ferris3958f802017-02-01 15:44:40 -0800179 phdr.p_type = PT_LOAD;
180 phdr.p_vaddr = 0x2000;
181 phdr.p_memsz = 0x10000;
182 phdr.p_flags = PF_R | PF_X;
183 phdr.p_align = 0x1000;
184 memory_.SetMemory(0x100, &phdr, sizeof(phdr));
185
186 memset(&phdr, 0, sizeof(phdr));
187 phdr.p_type = PT_LOAD;
188 phdr.p_offset = 0x1000;
189 phdr.p_vaddr = 0x2001;
190 phdr.p_memsz = 0x10001;
191 phdr.p_flags = PF_R | PF_X;
192 phdr.p_align = 0x1001;
193 memory_.SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr));
194
195 memset(&phdr, 0, sizeof(phdr));
196 phdr.p_type = PT_LOAD;
197 phdr.p_offset = 0x2000;
198 phdr.p_vaddr = 0x2002;
199 phdr.p_memsz = 0x10002;
200 phdr.p_flags = PF_R | PF_X;
201 phdr.p_align = 0x1002;
202 memory_.SetMemory(0x100 + 2 * sizeof(phdr), &phdr, sizeof(phdr));
203
Christopher Ferrise69f4702017-10-19 16:08:58 -0700204 uint64_t load_bias = 0;
205 ASSERT_TRUE(elf->Init(&load_bias));
206 EXPECT_EQ(0x2000U, load_bias);
Christopher Ferris3958f802017-02-01 15:44:40 -0800207
208 const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads();
209 ASSERT_EQ(3U, pt_loads.size());
210
211 LoadInfo load_data = pt_loads.at(0);
212 ASSERT_EQ(0U, load_data.offset);
213 ASSERT_EQ(0x2000U, load_data.table_offset);
214 ASSERT_EQ(0x10000U, load_data.table_size);
215
216 load_data = pt_loads.at(0x1000);
217 ASSERT_EQ(0x1000U, load_data.offset);
218 ASSERT_EQ(0x2001U, load_data.table_offset);
219 ASSERT_EQ(0x10001U, load_data.table_size);
220
221 load_data = pt_loads.at(0x2000);
222 ASSERT_EQ(0x2000U, load_data.offset);
223 ASSERT_EQ(0x2002U, load_data.table_offset);
224 ASSERT_EQ(0x10002U, load_data.table_size);
225}
226
227TEST_F(ElfInterfaceTest, elf32_multiple_executable_pt_loads) {
228 MultipleExecutablePtLoads<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>();
229}
230
231TEST_F(ElfInterfaceTest, elf64_multiple_executable_pt_loads) {
232 MultipleExecutablePtLoads<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>();
233}
234
235template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
236void ElfInterfaceTest::MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr() {
237 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
238
Christopher Ferrisf882a382018-06-22 16:48:02 -0700239 Ehdr ehdr = {};
Christopher Ferris3958f802017-02-01 15:44:40 -0800240 ehdr.e_phoff = 0x100;
241 ehdr.e_phnum = 3;
242 ehdr.e_phentsize = sizeof(Phdr) + 100;
243 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
244
Christopher Ferrisf882a382018-06-22 16:48:02 -0700245 Phdr phdr = {};
Christopher Ferris3958f802017-02-01 15:44:40 -0800246 phdr.p_type = PT_LOAD;
247 phdr.p_vaddr = 0x2000;
248 phdr.p_memsz = 0x10000;
249 phdr.p_flags = PF_R | PF_X;
250 phdr.p_align = 0x1000;
251 memory_.SetMemory(0x100, &phdr, sizeof(phdr));
252
253 memset(&phdr, 0, sizeof(phdr));
254 phdr.p_type = PT_LOAD;
255 phdr.p_offset = 0x1000;
256 phdr.p_vaddr = 0x2001;
257 phdr.p_memsz = 0x10001;
258 phdr.p_flags = PF_R | PF_X;
259 phdr.p_align = 0x1001;
260 memory_.SetMemory(0x100 + sizeof(phdr) + 100, &phdr, sizeof(phdr));
261
262 memset(&phdr, 0, sizeof(phdr));
263 phdr.p_type = PT_LOAD;
264 phdr.p_offset = 0x2000;
265 phdr.p_vaddr = 0x2002;
266 phdr.p_memsz = 0x10002;
267 phdr.p_flags = PF_R | PF_X;
268 phdr.p_align = 0x1002;
269 memory_.SetMemory(0x100 + 2 * (sizeof(phdr) + 100), &phdr, sizeof(phdr));
270
Christopher Ferrise69f4702017-10-19 16:08:58 -0700271 uint64_t load_bias = 0;
272 ASSERT_TRUE(elf->Init(&load_bias));
273 EXPECT_EQ(0x2000U, load_bias);
Christopher Ferris3958f802017-02-01 15:44:40 -0800274
275 const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads();
276 ASSERT_EQ(3U, pt_loads.size());
277
278 LoadInfo load_data = pt_loads.at(0);
279 ASSERT_EQ(0U, load_data.offset);
280 ASSERT_EQ(0x2000U, load_data.table_offset);
281 ASSERT_EQ(0x10000U, load_data.table_size);
282
283 load_data = pt_loads.at(0x1000);
284 ASSERT_EQ(0x1000U, load_data.offset);
285 ASSERT_EQ(0x2001U, load_data.table_offset);
286 ASSERT_EQ(0x10001U, load_data.table_size);
287
288 load_data = pt_loads.at(0x2000);
289 ASSERT_EQ(0x2000U, load_data.offset);
290 ASSERT_EQ(0x2002U, load_data.table_offset);
291 ASSERT_EQ(0x10002U, load_data.table_size);
292}
293
294TEST_F(ElfInterfaceTest, elf32_multiple_executable_pt_loads_increments_not_size_of_phdr) {
295 MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn,
296 ElfInterface32>();
297}
298
299TEST_F(ElfInterfaceTest, elf64_multiple_executable_pt_loads_increments_not_size_of_phdr) {
300 MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn,
301 ElfInterface64>();
302}
303
304template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
305void ElfInterfaceTest::NonExecutablePtLoads() {
306 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
307
Christopher Ferrisf882a382018-06-22 16:48:02 -0700308 Ehdr ehdr = {};
Christopher Ferris3958f802017-02-01 15:44:40 -0800309 ehdr.e_phoff = 0x100;
310 ehdr.e_phnum = 3;
311 ehdr.e_phentsize = sizeof(Phdr);
312 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
313
Christopher Ferrisf882a382018-06-22 16:48:02 -0700314 Phdr phdr = {};
Christopher Ferris3958f802017-02-01 15:44:40 -0800315 phdr.p_type = PT_LOAD;
316 phdr.p_vaddr = 0x2000;
317 phdr.p_memsz = 0x10000;
318 phdr.p_flags = PF_R;
319 phdr.p_align = 0x1000;
320 memory_.SetMemory(0x100, &phdr, sizeof(phdr));
321
322 memset(&phdr, 0, sizeof(phdr));
323 phdr.p_type = PT_LOAD;
324 phdr.p_offset = 0x1000;
325 phdr.p_vaddr = 0x2001;
326 phdr.p_memsz = 0x10001;
327 phdr.p_flags = PF_R | PF_X;
328 phdr.p_align = 0x1001;
329 memory_.SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr));
330
331 memset(&phdr, 0, sizeof(phdr));
332 phdr.p_type = PT_LOAD;
333 phdr.p_offset = 0x2000;
334 phdr.p_vaddr = 0x2002;
335 phdr.p_memsz = 0x10002;
336 phdr.p_flags = PF_R;
337 phdr.p_align = 0x1002;
338 memory_.SetMemory(0x100 + 2 * sizeof(phdr), &phdr, sizeof(phdr));
339
Christopher Ferrise69f4702017-10-19 16:08:58 -0700340 uint64_t load_bias = 0;
341 ASSERT_TRUE(elf->Init(&load_bias));
342 EXPECT_EQ(0U, load_bias);
Christopher Ferris3958f802017-02-01 15:44:40 -0800343
344 const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads();
345 ASSERT_EQ(1U, pt_loads.size());
346
347 LoadInfo load_data = pt_loads.at(0x1000);
348 ASSERT_EQ(0x1000U, load_data.offset);
349 ASSERT_EQ(0x2001U, load_data.table_offset);
350 ASSERT_EQ(0x10001U, load_data.table_size);
351}
352
353TEST_F(ElfInterfaceTest, elf32_non_executable_pt_loads) {
354 NonExecutablePtLoads<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>();
355}
356
357TEST_F(ElfInterfaceTest, elf64_non_executable_pt_loads) {
358 NonExecutablePtLoads<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>();
359}
360
361template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
362void ElfInterfaceTest::ManyPhdrs() {
363 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
364
Christopher Ferrisf882a382018-06-22 16:48:02 -0700365 Ehdr ehdr = {};
Christopher Ferris3958f802017-02-01 15:44:40 -0800366 ehdr.e_phoff = 0x100;
367 ehdr.e_phnum = 7;
368 ehdr.e_phentsize = sizeof(Phdr);
369 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
370
Christopher Ferris3958f802017-02-01 15:44:40 -0800371 uint64_t phdr_offset = 0x100;
372
Christopher Ferrisf882a382018-06-22 16:48:02 -0700373 Phdr phdr = {};
Christopher Ferris3958f802017-02-01 15:44:40 -0800374 phdr.p_type = PT_LOAD;
375 phdr.p_vaddr = 0x2000;
376 phdr.p_memsz = 0x10000;
377 phdr.p_flags = PF_R | PF_X;
378 phdr.p_align = 0x1000;
379 memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr));
380 phdr_offset += sizeof(phdr);
381
382 memset(&phdr, 0, sizeof(phdr));
383 phdr.p_type = PT_GNU_EH_FRAME;
384 memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr));
385 phdr_offset += sizeof(phdr);
386
387 memset(&phdr, 0, sizeof(phdr));
388 phdr.p_type = PT_DYNAMIC;
389 memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr));
390 phdr_offset += sizeof(phdr);
391
392 memset(&phdr, 0, sizeof(phdr));
393 phdr.p_type = PT_INTERP;
394 memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr));
395 phdr_offset += sizeof(phdr);
396
397 memset(&phdr, 0, sizeof(phdr));
398 phdr.p_type = PT_NOTE;
399 memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr));
400 phdr_offset += sizeof(phdr);
401
402 memset(&phdr, 0, sizeof(phdr));
403 phdr.p_type = PT_SHLIB;
404 memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr));
405 phdr_offset += sizeof(phdr);
406
407 memset(&phdr, 0, sizeof(phdr));
408 phdr.p_type = PT_GNU_EH_FRAME;
409 memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr));
410 phdr_offset += sizeof(phdr);
411
Christopher Ferrise69f4702017-10-19 16:08:58 -0700412 uint64_t load_bias = 0;
413 ASSERT_TRUE(elf->Init(&load_bias));
414 EXPECT_EQ(0x2000U, load_bias);
Christopher Ferris3958f802017-02-01 15:44:40 -0800415
416 const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads();
417 ASSERT_EQ(1U, pt_loads.size());
418
419 LoadInfo load_data = pt_loads.at(0);
420 ASSERT_EQ(0U, load_data.offset);
421 ASSERT_EQ(0x2000U, load_data.table_offset);
422 ASSERT_EQ(0x10000U, load_data.table_size);
423}
424
425TEST_F(ElfInterfaceTest, elf32_many_phdrs) {
426 ElfInterfaceTest::ManyPhdrs<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>();
427}
428
429TEST_F(ElfInterfaceTest, elf64_many_phdrs) {
430 ElfInterfaceTest::ManyPhdrs<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>();
431}
432
433TEST_F(ElfInterfaceTest, elf32_arm) {
434 ElfInterfaceArm elf_arm(&memory_);
435
Christopher Ferrisf882a382018-06-22 16:48:02 -0700436 Elf32_Ehdr ehdr = {};
Christopher Ferris3958f802017-02-01 15:44:40 -0800437 ehdr.e_phoff = 0x100;
438 ehdr.e_phnum = 1;
439 ehdr.e_phentsize = sizeof(Elf32_Phdr);
440 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
441
Christopher Ferrisf882a382018-06-22 16:48:02 -0700442 Elf32_Phdr phdr = {};
Christopher Ferris3958f802017-02-01 15:44:40 -0800443 phdr.p_type = PT_ARM_EXIDX;
Christopher Ferrisf882a382018-06-22 16:48:02 -0700444 phdr.p_offset = 0x2000;
445 phdr.p_filesz = 16;
Christopher Ferris3958f802017-02-01 15:44:40 -0800446 memory_.SetMemory(0x100, &phdr, sizeof(phdr));
447
448 // Add arm exidx entries.
449 memory_.SetData32(0x2000, 0x1000);
450 memory_.SetData32(0x2008, 0x1000);
451
Christopher Ferrise69f4702017-10-19 16:08:58 -0700452 uint64_t load_bias = 0;
453 ASSERT_TRUE(elf_arm.Init(&load_bias));
454 EXPECT_EQ(0U, load_bias);
Christopher Ferris3958f802017-02-01 15:44:40 -0800455
456 std::vector<uint32_t> entries;
457 for (auto addr : elf_arm) {
458 entries.push_back(addr);
459 }
460 ASSERT_EQ(2U, entries.size());
461 ASSERT_EQ(0x3000U, entries[0]);
462 ASSERT_EQ(0x3008U, entries[1]);
463
464 ASSERT_EQ(0x2000U, elf_arm.start_offset());
465 ASSERT_EQ(2U, elf_arm.total_entries());
466}
467
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800468template <typename Ehdr, typename Phdr, typename Shdr, typename Dyn>
469void ElfInterfaceTest::SonameInit(SonameTestEnum test_type) {
Christopher Ferrisf882a382018-06-22 16:48:02 -0700470 Ehdr ehdr = {};
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800471 ehdr.e_shoff = 0x200;
472 ehdr.e_shnum = 2;
473 ehdr.e_shentsize = sizeof(Shdr);
Christopher Ferris3958f802017-02-01 15:44:40 -0800474 ehdr.e_phoff = 0x100;
475 ehdr.e_phnum = 1;
476 ehdr.e_phentsize = sizeof(Phdr);
477 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
478
Christopher Ferrisf882a382018-06-22 16:48:02 -0700479 Shdr shdr = {};
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800480 shdr.sh_type = SHT_STRTAB;
481 if (test_type == SONAME_MISSING_MAP) {
482 shdr.sh_addr = 0x20100;
483 } else {
484 shdr.sh_addr = 0x10100;
485 }
486 shdr.sh_offset = 0x10000;
487 memory_.SetMemory(0x200 + sizeof(shdr), &shdr, sizeof(shdr));
488
Christopher Ferrisf882a382018-06-22 16:48:02 -0700489 Phdr phdr = {};
Christopher Ferris3958f802017-02-01 15:44:40 -0800490 phdr.p_type = PT_DYNAMIC;
491 phdr.p_offset = 0x2000;
492 phdr.p_memsz = sizeof(Dyn) * 3;
493 memory_.SetMemory(0x100, &phdr, sizeof(phdr));
494
495 uint64_t offset = 0x2000;
496 Dyn dyn;
497
498 dyn.d_tag = DT_STRTAB;
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800499 dyn.d_un.d_ptr = 0x10100;
Christopher Ferris3958f802017-02-01 15:44:40 -0800500 memory_.SetMemory(offset, &dyn, sizeof(dyn));
501 offset += sizeof(dyn);
502
503 dyn.d_tag = DT_STRSZ;
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800504 if (test_type == SONAME_DTSIZE_SMALL) {
505 dyn.d_un.d_val = 0x10;
506 } else {
507 dyn.d_un.d_val = 0x1000;
508 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800509 memory_.SetMemory(offset, &dyn, sizeof(dyn));
510 offset += sizeof(dyn);
511
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800512 if (test_type == SONAME_DTNULL_AFTER) {
513 dyn.d_tag = DT_NULL;
514 memory_.SetMemory(offset, &dyn, sizeof(dyn));
515 offset += sizeof(dyn);
516 }
517
Christopher Ferris3958f802017-02-01 15:44:40 -0800518 dyn.d_tag = DT_SONAME;
519 dyn.d_un.d_val = 0x10;
520 memory_.SetMemory(offset, &dyn, sizeof(dyn));
521 offset += sizeof(dyn);
522
523 dyn.d_tag = DT_NULL;
524 memory_.SetMemory(offset, &dyn, sizeof(dyn));
525
526 SetStringMemory(0x10010, "fake_soname.so");
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800527}
528
529template <typename ElfInterfaceType>
530void ElfInterfaceTest::Soname() {
531 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
Christopher Ferris3958f802017-02-01 15:44:40 -0800532
Christopher Ferrise69f4702017-10-19 16:08:58 -0700533 uint64_t load_bias = 0;
534 ASSERT_TRUE(elf->Init(&load_bias));
535 EXPECT_EQ(0U, load_bias);
536
Christopher Ferris3958f802017-02-01 15:44:40 -0800537 std::string name;
538 ASSERT_TRUE(elf->GetSoname(&name));
539 ASSERT_STREQ("fake_soname.so", name.c_str());
540}
541
542TEST_F(ElfInterfaceTest, elf32_soname) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800543 SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>();
544 Soname<ElfInterface32>();
Christopher Ferris3958f802017-02-01 15:44:40 -0800545}
546
547TEST_F(ElfInterfaceTest, elf64_soname) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800548 SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>();
549 Soname<ElfInterface64>();
Christopher Ferris3958f802017-02-01 15:44:40 -0800550}
551
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800552template <typename ElfInterfaceType>
Christopher Ferris3958f802017-02-01 15:44:40 -0800553void ElfInterfaceTest::SonameAfterDtNull() {
554 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
555
Christopher Ferrise69f4702017-10-19 16:08:58 -0700556 uint64_t load_bias = 0;
557 ASSERT_TRUE(elf->Init(&load_bias));
558 EXPECT_EQ(0U, load_bias);
559
Christopher Ferris3958f802017-02-01 15:44:40 -0800560 std::string name;
561 ASSERT_FALSE(elf->GetSoname(&name));
562}
563
564TEST_F(ElfInterfaceTest, elf32_soname_after_dt_null) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800565 SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_DTNULL_AFTER);
566 SonameAfterDtNull<ElfInterface32>();
Christopher Ferris3958f802017-02-01 15:44:40 -0800567}
568
569TEST_F(ElfInterfaceTest, elf64_soname_after_dt_null) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800570 SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(SONAME_DTNULL_AFTER);
571 SonameAfterDtNull<ElfInterface64>();
Christopher Ferris3958f802017-02-01 15:44:40 -0800572}
573
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800574template <typename ElfInterfaceType>
Christopher Ferris3958f802017-02-01 15:44:40 -0800575void ElfInterfaceTest::SonameSize() {
576 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
577
Christopher Ferrise69f4702017-10-19 16:08:58 -0700578 uint64_t load_bias = 0;
579 ASSERT_TRUE(elf->Init(&load_bias));
580 EXPECT_EQ(0U, load_bias);
581
Christopher Ferris3958f802017-02-01 15:44:40 -0800582 std::string name;
583 ASSERT_FALSE(elf->GetSoname(&name));
584}
585
586TEST_F(ElfInterfaceTest, elf32_soname_size) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800587 SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_DTSIZE_SMALL);
588 SonameSize<ElfInterface32>();
Christopher Ferris3958f802017-02-01 15:44:40 -0800589}
590
591TEST_F(ElfInterfaceTest, elf64_soname_size) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800592 SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(SONAME_DTSIZE_SMALL);
593 SonameSize<ElfInterface64>();
594}
595
596// Verify that there is no map from STRTAB in the dynamic section to a
597// STRTAB entry in the section headers.
598template <typename ElfInterfaceType>
599void ElfInterfaceTest::SonameMissingMap() {
600 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
601
602 uint64_t load_bias = 0;
603 ASSERT_TRUE(elf->Init(&load_bias));
604 EXPECT_EQ(0U, load_bias);
605
606 std::string name;
607 ASSERT_FALSE(elf->GetSoname(&name));
608}
609
610TEST_F(ElfInterfaceTest, elf32_soname_missing_map) {
611 SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_MISSING_MAP);
612 SonameMissingMap<ElfInterface32>();
613}
614
615TEST_F(ElfInterfaceTest, elf64_soname_missing_map) {
616 SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(SONAME_MISSING_MAP);
617 SonameMissingMap<ElfInterface64>();
Christopher Ferris3958f802017-02-01 15:44:40 -0800618}
Christopher Ferris61d40972017-06-12 19:14:20 -0700619
Christopher Ferris61d40972017-06-12 19:14:20 -0700620template <typename ElfType>
621void ElfInterfaceTest::InitHeadersEhFrameTest() {
622 ElfType elf(&memory_);
623
Christopher Ferrise69f4702017-10-19 16:08:58 -0700624 elf.FakeSetEhFrameOffset(0x10000);
625 elf.FakeSetEhFrameSize(0);
626 elf.FakeSetDebugFrameOffset(0);
627 elf.FakeSetDebugFrameSize(0);
Christopher Ferris61d40972017-06-12 19:14:20 -0700628
629 memory_.SetMemory(0x10000,
630 std::vector<uint8_t>{0x1, DW_EH_PE_udata2, DW_EH_PE_udata2, DW_EH_PE_udata2});
631 memory_.SetData32(0x10004, 0x500);
632 memory_.SetData32(0x10008, 250);
633
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700634 elf.InitHeaders(0);
Christopher Ferris61d40972017-06-12 19:14:20 -0700635
636 EXPECT_FALSE(elf.eh_frame() == nullptr);
637 EXPECT_TRUE(elf.debug_frame() == nullptr);
638}
639
640TEST_F(ElfInterfaceTest, init_headers_eh_frame32) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700641 InitHeadersEhFrameTest<ElfInterface32Fake>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700642}
643
644TEST_F(ElfInterfaceTest, init_headers_eh_frame64) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700645 InitHeadersEhFrameTest<ElfInterface64Fake>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700646}
647
648template <typename ElfType>
649void ElfInterfaceTest::InitHeadersDebugFrame() {
650 ElfType elf(&memory_);
651
Christopher Ferrise69f4702017-10-19 16:08:58 -0700652 elf.FakeSetEhFrameOffset(0);
653 elf.FakeSetEhFrameSize(0);
654 elf.FakeSetDebugFrameOffset(0x5000);
655 elf.FakeSetDebugFrameSize(0x200);
Christopher Ferris61d40972017-06-12 19:14:20 -0700656
657 memory_.SetData32(0x5000, 0xfc);
658 memory_.SetData32(0x5004, 0xffffffff);
659 memory_.SetData8(0x5008, 1);
660 memory_.SetData8(0x5009, '\0');
661
662 memory_.SetData32(0x5100, 0xfc);
663 memory_.SetData32(0x5104, 0);
664 memory_.SetData32(0x5108, 0x1500);
665 memory_.SetData32(0x510c, 0x200);
666
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700667 elf.InitHeaders(0);
Christopher Ferris61d40972017-06-12 19:14:20 -0700668
669 EXPECT_TRUE(elf.eh_frame() == nullptr);
670 EXPECT_FALSE(elf.debug_frame() == nullptr);
671}
672
673TEST_F(ElfInterfaceTest, init_headers_debug_frame32) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700674 InitHeadersDebugFrame<ElfInterface32Fake>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700675}
676
677TEST_F(ElfInterfaceTest, init_headers_debug_frame64) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700678 InitHeadersDebugFrame<ElfInterface64Fake>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700679}
680
681template <typename ElfType>
682void ElfInterfaceTest::InitHeadersEhFrameFail() {
683 ElfType elf(&memory_);
684
Christopher Ferrise69f4702017-10-19 16:08:58 -0700685 elf.FakeSetEhFrameOffset(0x1000);
686 elf.FakeSetEhFrameSize(0x100);
687 elf.FakeSetDebugFrameOffset(0);
688 elf.FakeSetDebugFrameSize(0);
Christopher Ferris61d40972017-06-12 19:14:20 -0700689
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700690 elf.InitHeaders(0);
Christopher Ferris61d40972017-06-12 19:14:20 -0700691
692 EXPECT_TRUE(elf.eh_frame() == nullptr);
693 EXPECT_EQ(0U, elf.eh_frame_offset());
694 EXPECT_EQ(static_cast<uint64_t>(-1), elf.eh_frame_size());
695 EXPECT_TRUE(elf.debug_frame() == nullptr);
696}
697
698TEST_F(ElfInterfaceTest, init_headers_eh_frame32_fail) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700699 InitHeadersEhFrameFail<ElfInterface32Fake>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700700}
701
702TEST_F(ElfInterfaceTest, init_headers_eh_frame64_fail) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700703 InitHeadersEhFrameFail<ElfInterface64Fake>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700704}
705
706template <typename ElfType>
707void ElfInterfaceTest::InitHeadersDebugFrameFail() {
708 ElfType elf(&memory_);
709
Christopher Ferrise69f4702017-10-19 16:08:58 -0700710 elf.FakeSetEhFrameOffset(0);
711 elf.FakeSetEhFrameSize(0);
712 elf.FakeSetDebugFrameOffset(0x1000);
713 elf.FakeSetDebugFrameSize(0x100);
Christopher Ferris61d40972017-06-12 19:14:20 -0700714
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700715 elf.InitHeaders(0);
Christopher Ferris61d40972017-06-12 19:14:20 -0700716
717 EXPECT_TRUE(elf.eh_frame() == nullptr);
718 EXPECT_TRUE(elf.debug_frame() == nullptr);
719 EXPECT_EQ(0U, elf.debug_frame_offset());
720 EXPECT_EQ(static_cast<uint64_t>(-1), elf.debug_frame_size());
721}
722
723TEST_F(ElfInterfaceTest, init_headers_debug_frame32_fail) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700724 InitHeadersDebugFrameFail<ElfInterface32Fake>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700725}
726
727TEST_F(ElfInterfaceTest, init_headers_debug_frame64_fail) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700728 InitHeadersDebugFrameFail<ElfInterface64Fake>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700729}
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700730
731template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
732void ElfInterfaceTest::InitSectionHeadersMalformed() {
733 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_));
734
Christopher Ferrisf882a382018-06-22 16:48:02 -0700735 Ehdr ehdr = {};
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700736 ehdr.e_shoff = 0x1000;
737 ehdr.e_shnum = 10;
738 ehdr.e_shentsize = sizeof(Shdr);
739 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
740
Christopher Ferrise69f4702017-10-19 16:08:58 -0700741 uint64_t load_bias = 0;
742 ASSERT_TRUE(elf->Init(&load_bias));
743 EXPECT_EQ(0U, load_bias);
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700744}
745
746TEST_F(ElfInterfaceTest, init_section_headers_malformed32) {
747 InitSectionHeadersMalformed<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>();
748}
749
750TEST_F(ElfInterfaceTest, init_section_headers_malformed64) {
751 InitSectionHeadersMalformed<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>();
752}
753
754template <typename Ehdr, typename Shdr, typename Sym, typename ElfInterfaceType>
755void ElfInterfaceTest::InitSectionHeaders(uint64_t entry_size) {
756 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_));
757
758 uint64_t offset = 0x1000;
759
Christopher Ferrisf882a382018-06-22 16:48:02 -0700760 Ehdr ehdr = {};
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700761 ehdr.e_shoff = offset;
762 ehdr.e_shnum = 10;
763 ehdr.e_shentsize = entry_size;
764 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
765
766 offset += ehdr.e_shentsize;
767
Christopher Ferrisf882a382018-06-22 16:48:02 -0700768 Shdr shdr = {};
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700769 shdr.sh_type = SHT_SYMTAB;
770 shdr.sh_link = 4;
771 shdr.sh_addr = 0x5000;
772 shdr.sh_offset = 0x5000;
773 shdr.sh_entsize = sizeof(Sym);
774 shdr.sh_size = shdr.sh_entsize * 10;
775 memory_.SetMemory(offset, &shdr, sizeof(shdr));
776 offset += ehdr.e_shentsize;
777
778 memset(&shdr, 0, sizeof(shdr));
779 shdr.sh_type = SHT_DYNSYM;
780 shdr.sh_link = 4;
781 shdr.sh_addr = 0x6000;
782 shdr.sh_offset = 0x6000;
783 shdr.sh_entsize = sizeof(Sym);
784 shdr.sh_size = shdr.sh_entsize * 10;
785 memory_.SetMemory(offset, &shdr, sizeof(shdr));
786 offset += ehdr.e_shentsize;
787
788 memset(&shdr, 0, sizeof(shdr));
789 shdr.sh_type = SHT_PROGBITS;
790 shdr.sh_name = 0xa000;
791 memory_.SetMemory(offset, &shdr, sizeof(shdr));
792 offset += ehdr.e_shentsize;
793
794 // The string data for the entries.
795 memset(&shdr, 0, sizeof(shdr));
796 shdr.sh_type = SHT_STRTAB;
797 shdr.sh_name = 0x20000;
798 shdr.sh_offset = 0xf000;
799 shdr.sh_size = 0x1000;
800 memory_.SetMemory(offset, &shdr, sizeof(shdr));
801 offset += ehdr.e_shentsize;
802
803 InitSym<Sym>(0x5000, 0x90000, 0x1000, 0x100, 0xf000, "function_one");
804 InitSym<Sym>(0x6000, 0xd0000, 0x1000, 0x300, 0xf000, "function_two");
805
Christopher Ferrise69f4702017-10-19 16:08:58 -0700806 uint64_t load_bias = 0;
807 ASSERT_TRUE(elf->Init(&load_bias));
808 EXPECT_EQ(0U, load_bias);
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700809 EXPECT_EQ(0U, elf->debug_frame_offset());
810 EXPECT_EQ(0U, elf->debug_frame_size());
811 EXPECT_EQ(0U, elf->gnu_debugdata_offset());
812 EXPECT_EQ(0U, elf->gnu_debugdata_size());
813
814 // Look in the first symbol table.
815 std::string name;
816 uint64_t name_offset;
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700817 ASSERT_TRUE(elf->GetFunctionName(0x90010, &name, &name_offset));
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700818 EXPECT_EQ("function_one", name);
819 EXPECT_EQ(16U, name_offset);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700820 ASSERT_TRUE(elf->GetFunctionName(0xd0020, &name, &name_offset));
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700821 EXPECT_EQ("function_two", name);
822 EXPECT_EQ(32U, name_offset);
823}
824
825TEST_F(ElfInterfaceTest, init_section_headers32) {
826 InitSectionHeaders<Elf32_Ehdr, Elf32_Shdr, Elf32_Sym, ElfInterface32>(sizeof(Elf32_Shdr));
827}
828
829TEST_F(ElfInterfaceTest, init_section_headers64) {
830 InitSectionHeaders<Elf64_Ehdr, Elf64_Shdr, Elf64_Sym, ElfInterface64>(sizeof(Elf64_Shdr));
831}
832
833TEST_F(ElfInterfaceTest, init_section_headers_non_std_entry_size32) {
834 InitSectionHeaders<Elf32_Ehdr, Elf32_Shdr, Elf32_Sym, ElfInterface32>(0x100);
835}
836
837TEST_F(ElfInterfaceTest, init_section_headers_non_std_entry_size64) {
838 InitSectionHeaders<Elf64_Ehdr, Elf64_Shdr, Elf64_Sym, ElfInterface64>(0x100);
839}
840
841template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
842void ElfInterfaceTest::InitSectionHeadersOffsets() {
843 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_));
844
845 uint64_t offset = 0x2000;
846
Christopher Ferrisf882a382018-06-22 16:48:02 -0700847 Ehdr ehdr = {};
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700848 ehdr.e_shoff = offset;
849 ehdr.e_shnum = 10;
850 ehdr.e_shentsize = sizeof(Shdr);
851 ehdr.e_shstrndx = 2;
852 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
853
854 offset += ehdr.e_shentsize;
855
Christopher Ferrisf882a382018-06-22 16:48:02 -0700856 Shdr shdr = {};
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700857 shdr.sh_type = SHT_PROGBITS;
858 shdr.sh_link = 2;
859 shdr.sh_name = 0x200;
860 shdr.sh_addr = 0x5000;
861 shdr.sh_offset = 0x5000;
862 shdr.sh_entsize = 0x100;
863 shdr.sh_size = 0x800;
864 memory_.SetMemory(offset, &shdr, sizeof(shdr));
865 offset += ehdr.e_shentsize;
866
867 // The string data for section header names.
868 memset(&shdr, 0, sizeof(shdr));
869 shdr.sh_type = SHT_STRTAB;
870 shdr.sh_name = 0x20000;
871 shdr.sh_offset = 0xf000;
872 shdr.sh_size = 0x1000;
873 memory_.SetMemory(offset, &shdr, sizeof(shdr));
874 offset += ehdr.e_shentsize;
875
876 memset(&shdr, 0, sizeof(shdr));
877 shdr.sh_type = SHT_PROGBITS;
878 shdr.sh_link = 2;
879 shdr.sh_name = 0x100;
880 shdr.sh_addr = 0x6000;
881 shdr.sh_offset = 0x6000;
882 shdr.sh_entsize = 0x100;
883 shdr.sh_size = 0x500;
884 memory_.SetMemory(offset, &shdr, sizeof(shdr));
885 offset += ehdr.e_shentsize;
886
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700887 memset(&shdr, 0, sizeof(shdr));
888 shdr.sh_type = SHT_PROGBITS;
889 shdr.sh_link = 2;
890 shdr.sh_name = 0x300;
891 shdr.sh_addr = 0x7000;
892 shdr.sh_offset = 0x7000;
893 shdr.sh_entsize = 0x100;
894 shdr.sh_size = 0x800;
895 memory_.SetMemory(offset, &shdr, sizeof(shdr));
896 offset += ehdr.e_shentsize;
897
898 memset(&shdr, 0, sizeof(shdr));
899 shdr.sh_type = SHT_PROGBITS;
900 shdr.sh_link = 2;
901 shdr.sh_name = 0x400;
902 shdr.sh_addr = 0x6000;
903 shdr.sh_offset = 0xa000;
904 shdr.sh_entsize = 0x100;
905 shdr.sh_size = 0xf00;
906 memory_.SetMemory(offset, &shdr, sizeof(shdr));
907 offset += ehdr.e_shentsize;
908
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700909 memory_.SetMemory(0xf100, ".debug_frame", sizeof(".debug_frame"));
910 memory_.SetMemory(0xf200, ".gnu_debugdata", sizeof(".gnu_debugdata"));
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700911 memory_.SetMemory(0xf300, ".eh_frame", sizeof(".eh_frame"));
912 memory_.SetMemory(0xf400, ".eh_frame_hdr", sizeof(".eh_frame_hdr"));
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700913
Christopher Ferrise69f4702017-10-19 16:08:58 -0700914 uint64_t load_bias = 0;
915 ASSERT_TRUE(elf->Init(&load_bias));
916 EXPECT_EQ(0U, load_bias);
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700917 EXPECT_EQ(0x6000U, elf->debug_frame_offset());
918 EXPECT_EQ(0x500U, elf->debug_frame_size());
919 EXPECT_EQ(0x5000U, elf->gnu_debugdata_offset());
920 EXPECT_EQ(0x800U, elf->gnu_debugdata_size());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700921 EXPECT_EQ(0x7000U, elf->eh_frame_offset());
922 EXPECT_EQ(0x800U, elf->eh_frame_size());
923 EXPECT_EQ(0xa000U, elf->eh_frame_hdr_offset());
924 EXPECT_EQ(0xf00U, elf->eh_frame_hdr_size());
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700925}
926
927TEST_F(ElfInterfaceTest, init_section_headers_offsets32) {
928 InitSectionHeadersOffsets<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>();
929}
930
931TEST_F(ElfInterfaceTest, init_section_headers_offsets64) {
932 InitSectionHeadersOffsets<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>();
933}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700934
Christopher Ferris150db122017-12-20 18:49:01 -0800935TEST_F(ElfInterfaceTest, is_valid_pc_from_pt_load) {
936 std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_));
937
Christopher Ferrisf882a382018-06-22 16:48:02 -0700938 Elf32_Ehdr ehdr = {};
Christopher Ferris150db122017-12-20 18:49:01 -0800939 ehdr.e_phoff = 0x100;
940 ehdr.e_phnum = 1;
941 ehdr.e_phentsize = sizeof(Elf32_Phdr);
942 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
943
Christopher Ferrisf882a382018-06-22 16:48:02 -0700944 Elf32_Phdr phdr = {};
Christopher Ferris150db122017-12-20 18:49:01 -0800945 phdr.p_type = PT_LOAD;
946 phdr.p_vaddr = 0;
947 phdr.p_memsz = 0x10000;
948 phdr.p_flags = PF_R | PF_X;
949 phdr.p_align = 0x1000;
950 memory_.SetMemory(0x100, &phdr, sizeof(phdr));
951
952 uint64_t load_bias = 0;
953 ASSERT_TRUE(elf->Init(&load_bias));
954 EXPECT_EQ(0U, load_bias);
955 EXPECT_TRUE(elf->IsValidPc(0));
956 EXPECT_TRUE(elf->IsValidPc(0x5000));
957 EXPECT_TRUE(elf->IsValidPc(0xffff));
958 EXPECT_FALSE(elf->IsValidPc(0x10000));
959}
960
961TEST_F(ElfInterfaceTest, is_valid_pc_from_pt_load_non_zero_load_bias) {
962 std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_));
963
Christopher Ferrisf882a382018-06-22 16:48:02 -0700964 Elf32_Ehdr ehdr = {};
Christopher Ferris150db122017-12-20 18:49:01 -0800965 ehdr.e_phoff = 0x100;
966 ehdr.e_phnum = 1;
967 ehdr.e_phentsize = sizeof(Elf32_Phdr);
968 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
969
Christopher Ferrisf882a382018-06-22 16:48:02 -0700970 Elf32_Phdr phdr = {};
Christopher Ferris150db122017-12-20 18:49:01 -0800971 phdr.p_type = PT_LOAD;
972 phdr.p_vaddr = 0x2000;
973 phdr.p_memsz = 0x10000;
974 phdr.p_flags = PF_R | PF_X;
975 phdr.p_align = 0x1000;
976 memory_.SetMemory(0x100, &phdr, sizeof(phdr));
977
978 uint64_t load_bias = 0;
979 ASSERT_TRUE(elf->Init(&load_bias));
980 EXPECT_EQ(0x2000U, load_bias);
981 EXPECT_FALSE(elf->IsValidPc(0));
982 EXPECT_FALSE(elf->IsValidPc(0x1000));
983 EXPECT_FALSE(elf->IsValidPc(0x1fff));
984 EXPECT_TRUE(elf->IsValidPc(0x2000));
985 EXPECT_TRUE(elf->IsValidPc(0x5000));
986 EXPECT_TRUE(elf->IsValidPc(0x11fff));
987 EXPECT_FALSE(elf->IsValidPc(0x12000));
988}
989
990TEST_F(ElfInterfaceTest, is_valid_pc_from_debug_frame) {
991 std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_));
992
993 uint64_t sh_offset = 0x100;
994
Christopher Ferrisf882a382018-06-22 16:48:02 -0700995 Elf32_Ehdr ehdr = {};
Christopher Ferris150db122017-12-20 18:49:01 -0800996 ehdr.e_shstrndx = 1;
997 ehdr.e_shoff = sh_offset;
998 ehdr.e_shentsize = sizeof(Elf32_Shdr);
999 ehdr.e_shnum = 3;
1000 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
1001
Christopher Ferrisf882a382018-06-22 16:48:02 -07001002 Elf32_Shdr shdr = {};
Christopher Ferris150db122017-12-20 18:49:01 -08001003 shdr.sh_type = SHT_NULL;
1004 memory_.SetMemory(sh_offset, &shdr, sizeof(shdr));
1005
1006 sh_offset += sizeof(shdr);
1007 memset(&shdr, 0, sizeof(shdr));
1008 shdr.sh_type = SHT_STRTAB;
1009 shdr.sh_name = 1;
1010 shdr.sh_offset = 0x500;
1011 shdr.sh_size = 0x100;
1012 memory_.SetMemory(sh_offset, &shdr, sizeof(shdr));
1013 memory_.SetMemory(0x500, ".debug_frame");
1014
1015 sh_offset += sizeof(shdr);
1016 memset(&shdr, 0, sizeof(shdr));
1017 shdr.sh_type = SHT_PROGBITS;
1018 shdr.sh_name = 0;
1019 shdr.sh_addr = 0x600;
1020 shdr.sh_offset = 0x600;
1021 shdr.sh_size = 0x200;
1022 memory_.SetMemory(sh_offset, &shdr, sizeof(shdr));
1023
1024 // CIE 32.
1025 memory_.SetData32(0x600, 0xfc);
1026 memory_.SetData32(0x604, 0xffffffff);
1027 memory_.SetData8(0x608, 1);
1028 memory_.SetData8(0x609, '\0');
1029 memory_.SetData8(0x60a, 0x4);
1030 memory_.SetData8(0x60b, 0x4);
1031 memory_.SetData8(0x60c, 0x1);
1032
1033 // FDE 32.
1034 memory_.SetData32(0x700, 0xfc);
1035 memory_.SetData32(0x704, 0);
1036 memory_.SetData32(0x708, 0x2100);
1037 memory_.SetData32(0x70c, 0x200);
1038
1039 uint64_t load_bias = 0;
1040 ASSERT_TRUE(elf->Init(&load_bias));
Christopher Ferris4cc36d22018-06-06 14:47:31 -07001041 elf->InitHeaders(0);
Christopher Ferris150db122017-12-20 18:49:01 -08001042 EXPECT_EQ(0U, load_bias);
1043 EXPECT_FALSE(elf->IsValidPc(0));
1044 EXPECT_FALSE(elf->IsValidPc(0x20ff));
1045 EXPECT_TRUE(elf->IsValidPc(0x2100));
1046 EXPECT_TRUE(elf->IsValidPc(0x2200));
1047 EXPECT_TRUE(elf->IsValidPc(0x22ff));
1048 EXPECT_FALSE(elf->IsValidPc(0x2300));
1049}
1050
1051TEST_F(ElfInterfaceTest, is_valid_pc_from_eh_frame) {
1052 std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_));
1053
1054 uint64_t sh_offset = 0x100;
1055
Christopher Ferrisf882a382018-06-22 16:48:02 -07001056 Elf32_Ehdr ehdr = {};
Christopher Ferris150db122017-12-20 18:49:01 -08001057 ehdr.e_shstrndx = 1;
1058 ehdr.e_shoff = sh_offset;
1059 ehdr.e_shentsize = sizeof(Elf32_Shdr);
1060 ehdr.e_shnum = 3;
1061 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
1062
Christopher Ferrisf882a382018-06-22 16:48:02 -07001063 Elf32_Shdr shdr = {};
Christopher Ferris150db122017-12-20 18:49:01 -08001064 shdr.sh_type = SHT_NULL;
1065 memory_.SetMemory(sh_offset, &shdr, sizeof(shdr));
1066
1067 sh_offset += sizeof(shdr);
1068 memset(&shdr, 0, sizeof(shdr));
1069 shdr.sh_type = SHT_STRTAB;
1070 shdr.sh_name = 1;
1071 shdr.sh_offset = 0x500;
1072 shdr.sh_size = 0x100;
1073 memory_.SetMemory(sh_offset, &shdr, sizeof(shdr));
1074 memory_.SetMemory(0x500, ".eh_frame");
1075
1076 sh_offset += sizeof(shdr);
1077 memset(&shdr, 0, sizeof(shdr));
1078 shdr.sh_type = SHT_PROGBITS;
1079 shdr.sh_name = 0;
1080 shdr.sh_addr = 0x600;
1081 shdr.sh_offset = 0x600;
1082 shdr.sh_size = 0x200;
1083 memory_.SetMemory(sh_offset, &shdr, sizeof(shdr));
1084
1085 // CIE 32.
1086 memory_.SetData32(0x600, 0xfc);
1087 memory_.SetData32(0x604, 0);
1088 memory_.SetData8(0x608, 1);
1089 memory_.SetData8(0x609, '\0');
1090 memory_.SetData8(0x60a, 0x4);
1091 memory_.SetData8(0x60b, 0x4);
1092 memory_.SetData8(0x60c, 0x1);
1093
1094 // FDE 32.
1095 memory_.SetData32(0x700, 0xfc);
1096 memory_.SetData32(0x704, 0x104);
1097 memory_.SetData32(0x708, 0x20f8);
1098 memory_.SetData32(0x70c, 0x200);
1099
1100 uint64_t load_bias = 0;
1101 ASSERT_TRUE(elf->Init(&load_bias));
Christopher Ferris4cc36d22018-06-06 14:47:31 -07001102 elf->InitHeaders(0);
Christopher Ferris150db122017-12-20 18:49:01 -08001103 EXPECT_EQ(0U, load_bias);
1104 EXPECT_FALSE(elf->IsValidPc(0));
1105 EXPECT_FALSE(elf->IsValidPc(0x27ff));
1106 EXPECT_TRUE(elf->IsValidPc(0x2800));
1107 EXPECT_TRUE(elf->IsValidPc(0x2900));
1108 EXPECT_TRUE(elf->IsValidPc(0x29ff));
1109 EXPECT_FALSE(elf->IsValidPc(0x2a00));
1110}
1111
Christopher Ferrisd226a512017-07-14 10:37:19 -07001112} // namespace unwindstack