blob: bf97e30bfa484d280d9e61dcb04ef8b788387d7d [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) {
119 Sym sym;
120 memset(&sym, 0, sizeof(sym));
121 sym.st_info = STT_FUNC;
122 sym.st_value = value;
123 sym.st_size = size;
124 sym.st_name = name_offset;
125 sym.st_shndx = SHN_COMMON;
126
127 memory_.SetMemory(offset, &sym, sizeof(sym));
128 memory_.SetMemory(sym_offset + name_offset, name, strlen(name) + 1);
129}
130
Christopher Ferris3958f802017-02-01 15:44:40 -0800131template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
132void ElfInterfaceTest::SinglePtLoad() {
133 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
134
135 Ehdr ehdr;
136 memset(&ehdr, 0, sizeof(ehdr));
137 ehdr.e_phoff = 0x100;
138 ehdr.e_phnum = 1;
139 ehdr.e_phentsize = sizeof(Phdr);
140 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
141
142 Phdr phdr;
143 memset(&phdr, 0, sizeof(phdr));
144 phdr.p_type = PT_LOAD;
145 phdr.p_vaddr = 0x2000;
146 phdr.p_memsz = 0x10000;
147 phdr.p_flags = PF_R | PF_X;
148 phdr.p_align = 0x1000;
149 memory_.SetMemory(0x100, &phdr, sizeof(phdr));
150
Christopher Ferrise69f4702017-10-19 16:08:58 -0700151 uint64_t load_bias = 0;
152 ASSERT_TRUE(elf->Init(&load_bias));
153 EXPECT_EQ(0x2000U, load_bias);
Christopher Ferris3958f802017-02-01 15:44:40 -0800154
155 const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads();
156 ASSERT_EQ(1U, pt_loads.size());
157 LoadInfo load_data = pt_loads.at(0);
158 ASSERT_EQ(0U, load_data.offset);
159 ASSERT_EQ(0x2000U, load_data.table_offset);
160 ASSERT_EQ(0x10000U, load_data.table_size);
161}
162
163TEST_F(ElfInterfaceTest, elf32_single_pt_load) {
164 SinglePtLoad<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>();
165}
166
167TEST_F(ElfInterfaceTest, elf64_single_pt_load) {
168 SinglePtLoad<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>();
169}
170
171template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
172void ElfInterfaceTest::MultipleExecutablePtLoads() {
173 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
174
175 Ehdr ehdr;
176 memset(&ehdr, 0, sizeof(ehdr));
177 ehdr.e_phoff = 0x100;
178 ehdr.e_phnum = 3;
179 ehdr.e_phentsize = sizeof(Phdr);
180 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
181
182 Phdr phdr;
183 memset(&phdr, 0, sizeof(phdr));
184 phdr.p_type = PT_LOAD;
185 phdr.p_vaddr = 0x2000;
186 phdr.p_memsz = 0x10000;
187 phdr.p_flags = PF_R | PF_X;
188 phdr.p_align = 0x1000;
189 memory_.SetMemory(0x100, &phdr, sizeof(phdr));
190
191 memset(&phdr, 0, sizeof(phdr));
192 phdr.p_type = PT_LOAD;
193 phdr.p_offset = 0x1000;
194 phdr.p_vaddr = 0x2001;
195 phdr.p_memsz = 0x10001;
196 phdr.p_flags = PF_R | PF_X;
197 phdr.p_align = 0x1001;
198 memory_.SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr));
199
200 memset(&phdr, 0, sizeof(phdr));
201 phdr.p_type = PT_LOAD;
202 phdr.p_offset = 0x2000;
203 phdr.p_vaddr = 0x2002;
204 phdr.p_memsz = 0x10002;
205 phdr.p_flags = PF_R | PF_X;
206 phdr.p_align = 0x1002;
207 memory_.SetMemory(0x100 + 2 * sizeof(phdr), &phdr, sizeof(phdr));
208
Christopher Ferrise69f4702017-10-19 16:08:58 -0700209 uint64_t load_bias = 0;
210 ASSERT_TRUE(elf->Init(&load_bias));
211 EXPECT_EQ(0x2000U, load_bias);
Christopher Ferris3958f802017-02-01 15:44:40 -0800212
213 const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads();
214 ASSERT_EQ(3U, pt_loads.size());
215
216 LoadInfo load_data = pt_loads.at(0);
217 ASSERT_EQ(0U, load_data.offset);
218 ASSERT_EQ(0x2000U, load_data.table_offset);
219 ASSERT_EQ(0x10000U, load_data.table_size);
220
221 load_data = pt_loads.at(0x1000);
222 ASSERT_EQ(0x1000U, load_data.offset);
223 ASSERT_EQ(0x2001U, load_data.table_offset);
224 ASSERT_EQ(0x10001U, load_data.table_size);
225
226 load_data = pt_loads.at(0x2000);
227 ASSERT_EQ(0x2000U, load_data.offset);
228 ASSERT_EQ(0x2002U, load_data.table_offset);
229 ASSERT_EQ(0x10002U, load_data.table_size);
230}
231
232TEST_F(ElfInterfaceTest, elf32_multiple_executable_pt_loads) {
233 MultipleExecutablePtLoads<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>();
234}
235
236TEST_F(ElfInterfaceTest, elf64_multiple_executable_pt_loads) {
237 MultipleExecutablePtLoads<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>();
238}
239
240template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
241void ElfInterfaceTest::MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr() {
242 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
243
244 Ehdr ehdr;
245 memset(&ehdr, 0, sizeof(ehdr));
246 ehdr.e_phoff = 0x100;
247 ehdr.e_phnum = 3;
248 ehdr.e_phentsize = sizeof(Phdr) + 100;
249 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
250
251 Phdr phdr;
252 memset(&phdr, 0, sizeof(phdr));
253 phdr.p_type = PT_LOAD;
254 phdr.p_vaddr = 0x2000;
255 phdr.p_memsz = 0x10000;
256 phdr.p_flags = PF_R | PF_X;
257 phdr.p_align = 0x1000;
258 memory_.SetMemory(0x100, &phdr, sizeof(phdr));
259
260 memset(&phdr, 0, sizeof(phdr));
261 phdr.p_type = PT_LOAD;
262 phdr.p_offset = 0x1000;
263 phdr.p_vaddr = 0x2001;
264 phdr.p_memsz = 0x10001;
265 phdr.p_flags = PF_R | PF_X;
266 phdr.p_align = 0x1001;
267 memory_.SetMemory(0x100 + sizeof(phdr) + 100, &phdr, sizeof(phdr));
268
269 memset(&phdr, 0, sizeof(phdr));
270 phdr.p_type = PT_LOAD;
271 phdr.p_offset = 0x2000;
272 phdr.p_vaddr = 0x2002;
273 phdr.p_memsz = 0x10002;
274 phdr.p_flags = PF_R | PF_X;
275 phdr.p_align = 0x1002;
276 memory_.SetMemory(0x100 + 2 * (sizeof(phdr) + 100), &phdr, sizeof(phdr));
277
Christopher Ferrise69f4702017-10-19 16:08:58 -0700278 uint64_t load_bias = 0;
279 ASSERT_TRUE(elf->Init(&load_bias));
280 EXPECT_EQ(0x2000U, load_bias);
Christopher Ferris3958f802017-02-01 15:44:40 -0800281
282 const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads();
283 ASSERT_EQ(3U, pt_loads.size());
284
285 LoadInfo load_data = pt_loads.at(0);
286 ASSERT_EQ(0U, load_data.offset);
287 ASSERT_EQ(0x2000U, load_data.table_offset);
288 ASSERT_EQ(0x10000U, load_data.table_size);
289
290 load_data = pt_loads.at(0x1000);
291 ASSERT_EQ(0x1000U, load_data.offset);
292 ASSERT_EQ(0x2001U, load_data.table_offset);
293 ASSERT_EQ(0x10001U, load_data.table_size);
294
295 load_data = pt_loads.at(0x2000);
296 ASSERT_EQ(0x2000U, load_data.offset);
297 ASSERT_EQ(0x2002U, load_data.table_offset);
298 ASSERT_EQ(0x10002U, load_data.table_size);
299}
300
301TEST_F(ElfInterfaceTest, elf32_multiple_executable_pt_loads_increments_not_size_of_phdr) {
302 MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn,
303 ElfInterface32>();
304}
305
306TEST_F(ElfInterfaceTest, elf64_multiple_executable_pt_loads_increments_not_size_of_phdr) {
307 MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn,
308 ElfInterface64>();
309}
310
311template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
312void ElfInterfaceTest::NonExecutablePtLoads() {
313 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
314
315 Ehdr ehdr;
316 memset(&ehdr, 0, sizeof(ehdr));
317 ehdr.e_phoff = 0x100;
318 ehdr.e_phnum = 3;
319 ehdr.e_phentsize = sizeof(Phdr);
320 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
321
322 Phdr phdr;
323 memset(&phdr, 0, sizeof(phdr));
324 phdr.p_type = PT_LOAD;
325 phdr.p_vaddr = 0x2000;
326 phdr.p_memsz = 0x10000;
327 phdr.p_flags = PF_R;
328 phdr.p_align = 0x1000;
329 memory_.SetMemory(0x100, &phdr, sizeof(phdr));
330
331 memset(&phdr, 0, sizeof(phdr));
332 phdr.p_type = PT_LOAD;
333 phdr.p_offset = 0x1000;
334 phdr.p_vaddr = 0x2001;
335 phdr.p_memsz = 0x10001;
336 phdr.p_flags = PF_R | PF_X;
337 phdr.p_align = 0x1001;
338 memory_.SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr));
339
340 memset(&phdr, 0, sizeof(phdr));
341 phdr.p_type = PT_LOAD;
342 phdr.p_offset = 0x2000;
343 phdr.p_vaddr = 0x2002;
344 phdr.p_memsz = 0x10002;
345 phdr.p_flags = PF_R;
346 phdr.p_align = 0x1002;
347 memory_.SetMemory(0x100 + 2 * sizeof(phdr), &phdr, sizeof(phdr));
348
Christopher Ferrise69f4702017-10-19 16:08:58 -0700349 uint64_t load_bias = 0;
350 ASSERT_TRUE(elf->Init(&load_bias));
351 EXPECT_EQ(0U, load_bias);
Christopher Ferris3958f802017-02-01 15:44:40 -0800352
353 const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads();
354 ASSERT_EQ(1U, pt_loads.size());
355
356 LoadInfo load_data = pt_loads.at(0x1000);
357 ASSERT_EQ(0x1000U, load_data.offset);
358 ASSERT_EQ(0x2001U, load_data.table_offset);
359 ASSERT_EQ(0x10001U, load_data.table_size);
360}
361
362TEST_F(ElfInterfaceTest, elf32_non_executable_pt_loads) {
363 NonExecutablePtLoads<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>();
364}
365
366TEST_F(ElfInterfaceTest, elf64_non_executable_pt_loads) {
367 NonExecutablePtLoads<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>();
368}
369
370template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType>
371void ElfInterfaceTest::ManyPhdrs() {
372 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
373
374 Ehdr ehdr;
375 memset(&ehdr, 0, sizeof(ehdr));
376 ehdr.e_phoff = 0x100;
377 ehdr.e_phnum = 7;
378 ehdr.e_phentsize = sizeof(Phdr);
379 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
380
381 Phdr phdr;
382 uint64_t phdr_offset = 0x100;
383
384 memset(&phdr, 0, sizeof(phdr));
385 phdr.p_type = PT_LOAD;
386 phdr.p_vaddr = 0x2000;
387 phdr.p_memsz = 0x10000;
388 phdr.p_flags = PF_R | PF_X;
389 phdr.p_align = 0x1000;
390 memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr));
391 phdr_offset += sizeof(phdr);
392
393 memset(&phdr, 0, sizeof(phdr));
394 phdr.p_type = PT_GNU_EH_FRAME;
395 memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr));
396 phdr_offset += sizeof(phdr);
397
398 memset(&phdr, 0, sizeof(phdr));
399 phdr.p_type = PT_DYNAMIC;
400 memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr));
401 phdr_offset += sizeof(phdr);
402
403 memset(&phdr, 0, sizeof(phdr));
404 phdr.p_type = PT_INTERP;
405 memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr));
406 phdr_offset += sizeof(phdr);
407
408 memset(&phdr, 0, sizeof(phdr));
409 phdr.p_type = PT_NOTE;
410 memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr));
411 phdr_offset += sizeof(phdr);
412
413 memset(&phdr, 0, sizeof(phdr));
414 phdr.p_type = PT_SHLIB;
415 memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr));
416 phdr_offset += sizeof(phdr);
417
418 memset(&phdr, 0, sizeof(phdr));
419 phdr.p_type = PT_GNU_EH_FRAME;
420 memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr));
421 phdr_offset += sizeof(phdr);
422
Christopher Ferrise69f4702017-10-19 16:08:58 -0700423 uint64_t load_bias = 0;
424 ASSERT_TRUE(elf->Init(&load_bias));
425 EXPECT_EQ(0x2000U, load_bias);
Christopher Ferris3958f802017-02-01 15:44:40 -0800426
427 const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads();
428 ASSERT_EQ(1U, pt_loads.size());
429
430 LoadInfo load_data = pt_loads.at(0);
431 ASSERT_EQ(0U, load_data.offset);
432 ASSERT_EQ(0x2000U, load_data.table_offset);
433 ASSERT_EQ(0x10000U, load_data.table_size);
434}
435
436TEST_F(ElfInterfaceTest, elf32_many_phdrs) {
437 ElfInterfaceTest::ManyPhdrs<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>();
438}
439
440TEST_F(ElfInterfaceTest, elf64_many_phdrs) {
441 ElfInterfaceTest::ManyPhdrs<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>();
442}
443
444TEST_F(ElfInterfaceTest, elf32_arm) {
445 ElfInterfaceArm elf_arm(&memory_);
446
447 Elf32_Ehdr ehdr;
448 memset(&ehdr, 0, sizeof(ehdr));
449 ehdr.e_phoff = 0x100;
450 ehdr.e_phnum = 1;
451 ehdr.e_phentsize = sizeof(Elf32_Phdr);
452 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
453
454 Elf32_Phdr phdr;
455 memset(&phdr, 0, sizeof(phdr));
456 phdr.p_type = PT_ARM_EXIDX;
457 phdr.p_vaddr = 0x2000;
458 phdr.p_memsz = 16;
459 memory_.SetMemory(0x100, &phdr, sizeof(phdr));
460
461 // Add arm exidx entries.
462 memory_.SetData32(0x2000, 0x1000);
463 memory_.SetData32(0x2008, 0x1000);
464
Christopher Ferrise69f4702017-10-19 16:08:58 -0700465 uint64_t load_bias = 0;
466 ASSERT_TRUE(elf_arm.Init(&load_bias));
467 EXPECT_EQ(0U, load_bias);
Christopher Ferris3958f802017-02-01 15:44:40 -0800468
469 std::vector<uint32_t> entries;
470 for (auto addr : elf_arm) {
471 entries.push_back(addr);
472 }
473 ASSERT_EQ(2U, entries.size());
474 ASSERT_EQ(0x3000U, entries[0]);
475 ASSERT_EQ(0x3008U, entries[1]);
476
477 ASSERT_EQ(0x2000U, elf_arm.start_offset());
478 ASSERT_EQ(2U, elf_arm.total_entries());
479}
480
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800481template <typename Ehdr, typename Phdr, typename Shdr, typename Dyn>
482void ElfInterfaceTest::SonameInit(SonameTestEnum test_type) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800483 Ehdr ehdr;
484 memset(&ehdr, 0, sizeof(ehdr));
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800485 ehdr.e_shoff = 0x200;
486 ehdr.e_shnum = 2;
487 ehdr.e_shentsize = sizeof(Shdr);
Christopher Ferris3958f802017-02-01 15:44:40 -0800488 ehdr.e_phoff = 0x100;
489 ehdr.e_phnum = 1;
490 ehdr.e_phentsize = sizeof(Phdr);
491 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
492
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800493 Shdr shdr;
494 memset(&shdr, 0, sizeof(shdr));
495 shdr.sh_type = SHT_STRTAB;
496 if (test_type == SONAME_MISSING_MAP) {
497 shdr.sh_addr = 0x20100;
498 } else {
499 shdr.sh_addr = 0x10100;
500 }
501 shdr.sh_offset = 0x10000;
502 memory_.SetMemory(0x200 + sizeof(shdr), &shdr, sizeof(shdr));
503
Christopher Ferris3958f802017-02-01 15:44:40 -0800504 Phdr phdr;
505 memset(&phdr, 0, sizeof(phdr));
506 phdr.p_type = PT_DYNAMIC;
507 phdr.p_offset = 0x2000;
508 phdr.p_memsz = sizeof(Dyn) * 3;
509 memory_.SetMemory(0x100, &phdr, sizeof(phdr));
510
511 uint64_t offset = 0x2000;
512 Dyn dyn;
513
514 dyn.d_tag = DT_STRTAB;
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800515 dyn.d_un.d_ptr = 0x10100;
Christopher Ferris3958f802017-02-01 15:44:40 -0800516 memory_.SetMemory(offset, &dyn, sizeof(dyn));
517 offset += sizeof(dyn);
518
519 dyn.d_tag = DT_STRSZ;
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800520 if (test_type == SONAME_DTSIZE_SMALL) {
521 dyn.d_un.d_val = 0x10;
522 } else {
523 dyn.d_un.d_val = 0x1000;
524 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800525 memory_.SetMemory(offset, &dyn, sizeof(dyn));
526 offset += sizeof(dyn);
527
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800528 if (test_type == SONAME_DTNULL_AFTER) {
529 dyn.d_tag = DT_NULL;
530 memory_.SetMemory(offset, &dyn, sizeof(dyn));
531 offset += sizeof(dyn);
532 }
533
Christopher Ferris3958f802017-02-01 15:44:40 -0800534 dyn.d_tag = DT_SONAME;
535 dyn.d_un.d_val = 0x10;
536 memory_.SetMemory(offset, &dyn, sizeof(dyn));
537 offset += sizeof(dyn);
538
539 dyn.d_tag = DT_NULL;
540 memory_.SetMemory(offset, &dyn, sizeof(dyn));
541
542 SetStringMemory(0x10010, "fake_soname.so");
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800543}
544
545template <typename ElfInterfaceType>
546void ElfInterfaceTest::Soname() {
547 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
Christopher Ferris3958f802017-02-01 15:44:40 -0800548
Christopher Ferrise69f4702017-10-19 16:08:58 -0700549 uint64_t load_bias = 0;
550 ASSERT_TRUE(elf->Init(&load_bias));
551 EXPECT_EQ(0U, load_bias);
552
Christopher Ferris3958f802017-02-01 15:44:40 -0800553 std::string name;
554 ASSERT_TRUE(elf->GetSoname(&name));
555 ASSERT_STREQ("fake_soname.so", name.c_str());
556}
557
558TEST_F(ElfInterfaceTest, elf32_soname) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800559 SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>();
560 Soname<ElfInterface32>();
Christopher Ferris3958f802017-02-01 15:44:40 -0800561}
562
563TEST_F(ElfInterfaceTest, elf64_soname) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800564 SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>();
565 Soname<ElfInterface64>();
Christopher Ferris3958f802017-02-01 15:44:40 -0800566}
567
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800568template <typename ElfInterfaceType>
Christopher Ferris3958f802017-02-01 15:44:40 -0800569void ElfInterfaceTest::SonameAfterDtNull() {
570 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
571
Christopher Ferrise69f4702017-10-19 16:08:58 -0700572 uint64_t load_bias = 0;
573 ASSERT_TRUE(elf->Init(&load_bias));
574 EXPECT_EQ(0U, load_bias);
575
Christopher Ferris3958f802017-02-01 15:44:40 -0800576 std::string name;
577 ASSERT_FALSE(elf->GetSoname(&name));
578}
579
580TEST_F(ElfInterfaceTest, elf32_soname_after_dt_null) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800581 SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_DTNULL_AFTER);
582 SonameAfterDtNull<ElfInterface32>();
Christopher Ferris3958f802017-02-01 15:44:40 -0800583}
584
585TEST_F(ElfInterfaceTest, elf64_soname_after_dt_null) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800586 SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(SONAME_DTNULL_AFTER);
587 SonameAfterDtNull<ElfInterface64>();
Christopher Ferris3958f802017-02-01 15:44:40 -0800588}
589
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800590template <typename ElfInterfaceType>
Christopher Ferris3958f802017-02-01 15:44:40 -0800591void ElfInterfaceTest::SonameSize() {
592 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
593
Christopher Ferrise69f4702017-10-19 16:08:58 -0700594 uint64_t load_bias = 0;
595 ASSERT_TRUE(elf->Init(&load_bias));
596 EXPECT_EQ(0U, load_bias);
597
Christopher Ferris3958f802017-02-01 15:44:40 -0800598 std::string name;
599 ASSERT_FALSE(elf->GetSoname(&name));
600}
601
602TEST_F(ElfInterfaceTest, elf32_soname_size) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800603 SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_DTSIZE_SMALL);
604 SonameSize<ElfInterface32>();
Christopher Ferris3958f802017-02-01 15:44:40 -0800605}
606
607TEST_F(ElfInterfaceTest, elf64_soname_size) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800608 SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(SONAME_DTSIZE_SMALL);
609 SonameSize<ElfInterface64>();
610}
611
612// Verify that there is no map from STRTAB in the dynamic section to a
613// STRTAB entry in the section headers.
614template <typename ElfInterfaceType>
615void ElfInterfaceTest::SonameMissingMap() {
616 std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_));
617
618 uint64_t load_bias = 0;
619 ASSERT_TRUE(elf->Init(&load_bias));
620 EXPECT_EQ(0U, load_bias);
621
622 std::string name;
623 ASSERT_FALSE(elf->GetSoname(&name));
624}
625
626TEST_F(ElfInterfaceTest, elf32_soname_missing_map) {
627 SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_MISSING_MAP);
628 SonameMissingMap<ElfInterface32>();
629}
630
631TEST_F(ElfInterfaceTest, elf64_soname_missing_map) {
632 SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(SONAME_MISSING_MAP);
633 SonameMissingMap<ElfInterface64>();
Christopher Ferris3958f802017-02-01 15:44:40 -0800634}
Christopher Ferris61d40972017-06-12 19:14:20 -0700635
Christopher Ferris61d40972017-06-12 19:14:20 -0700636template <typename ElfType>
637void ElfInterfaceTest::InitHeadersEhFrameTest() {
638 ElfType elf(&memory_);
639
Christopher Ferrise69f4702017-10-19 16:08:58 -0700640 elf.FakeSetEhFrameOffset(0x10000);
641 elf.FakeSetEhFrameSize(0);
642 elf.FakeSetDebugFrameOffset(0);
643 elf.FakeSetDebugFrameSize(0);
Christopher Ferris61d40972017-06-12 19:14:20 -0700644
645 memory_.SetMemory(0x10000,
646 std::vector<uint8_t>{0x1, DW_EH_PE_udata2, DW_EH_PE_udata2, DW_EH_PE_udata2});
647 memory_.SetData32(0x10004, 0x500);
648 memory_.SetData32(0x10008, 250);
649
650 elf.InitHeaders();
651
652 EXPECT_FALSE(elf.eh_frame() == nullptr);
653 EXPECT_TRUE(elf.debug_frame() == nullptr);
654}
655
656TEST_F(ElfInterfaceTest, init_headers_eh_frame32) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700657 InitHeadersEhFrameTest<ElfInterface32Fake>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700658}
659
660TEST_F(ElfInterfaceTest, init_headers_eh_frame64) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700661 InitHeadersEhFrameTest<ElfInterface64Fake>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700662}
663
664template <typename ElfType>
665void ElfInterfaceTest::InitHeadersDebugFrame() {
666 ElfType elf(&memory_);
667
Christopher Ferrise69f4702017-10-19 16:08:58 -0700668 elf.FakeSetEhFrameOffset(0);
669 elf.FakeSetEhFrameSize(0);
670 elf.FakeSetDebugFrameOffset(0x5000);
671 elf.FakeSetDebugFrameSize(0x200);
Christopher Ferris61d40972017-06-12 19:14:20 -0700672
673 memory_.SetData32(0x5000, 0xfc);
674 memory_.SetData32(0x5004, 0xffffffff);
675 memory_.SetData8(0x5008, 1);
676 memory_.SetData8(0x5009, '\0');
677
678 memory_.SetData32(0x5100, 0xfc);
679 memory_.SetData32(0x5104, 0);
680 memory_.SetData32(0x5108, 0x1500);
681 memory_.SetData32(0x510c, 0x200);
682
683 elf.InitHeaders();
684
685 EXPECT_TRUE(elf.eh_frame() == nullptr);
686 EXPECT_FALSE(elf.debug_frame() == nullptr);
687}
688
689TEST_F(ElfInterfaceTest, init_headers_debug_frame32) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700690 InitHeadersDebugFrame<ElfInterface32Fake>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700691}
692
693TEST_F(ElfInterfaceTest, init_headers_debug_frame64) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700694 InitHeadersDebugFrame<ElfInterface64Fake>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700695}
696
697template <typename ElfType>
698void ElfInterfaceTest::InitHeadersEhFrameFail() {
699 ElfType elf(&memory_);
700
Christopher Ferrise69f4702017-10-19 16:08:58 -0700701 elf.FakeSetEhFrameOffset(0x1000);
702 elf.FakeSetEhFrameSize(0x100);
703 elf.FakeSetDebugFrameOffset(0);
704 elf.FakeSetDebugFrameSize(0);
Christopher Ferris61d40972017-06-12 19:14:20 -0700705
706 elf.InitHeaders();
707
708 EXPECT_TRUE(elf.eh_frame() == nullptr);
709 EXPECT_EQ(0U, elf.eh_frame_offset());
710 EXPECT_EQ(static_cast<uint64_t>(-1), elf.eh_frame_size());
711 EXPECT_TRUE(elf.debug_frame() == nullptr);
712}
713
714TEST_F(ElfInterfaceTest, init_headers_eh_frame32_fail) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700715 InitHeadersEhFrameFail<ElfInterface32Fake>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700716}
717
718TEST_F(ElfInterfaceTest, init_headers_eh_frame64_fail) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700719 InitHeadersEhFrameFail<ElfInterface64Fake>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700720}
721
722template <typename ElfType>
723void ElfInterfaceTest::InitHeadersDebugFrameFail() {
724 ElfType elf(&memory_);
725
Christopher Ferrise69f4702017-10-19 16:08:58 -0700726 elf.FakeSetEhFrameOffset(0);
727 elf.FakeSetEhFrameSize(0);
728 elf.FakeSetDebugFrameOffset(0x1000);
729 elf.FakeSetDebugFrameSize(0x100);
Christopher Ferris61d40972017-06-12 19:14:20 -0700730
731 elf.InitHeaders();
732
733 EXPECT_TRUE(elf.eh_frame() == nullptr);
734 EXPECT_TRUE(elf.debug_frame() == nullptr);
735 EXPECT_EQ(0U, elf.debug_frame_offset());
736 EXPECT_EQ(static_cast<uint64_t>(-1), elf.debug_frame_size());
737}
738
739TEST_F(ElfInterfaceTest, init_headers_debug_frame32_fail) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700740 InitHeadersDebugFrameFail<ElfInterface32Fake>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700741}
742
743TEST_F(ElfInterfaceTest, init_headers_debug_frame64_fail) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700744 InitHeadersDebugFrameFail<ElfInterface64Fake>();
Christopher Ferris61d40972017-06-12 19:14:20 -0700745}
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700746
747template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
748void ElfInterfaceTest::InitSectionHeadersMalformed() {
749 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_));
750
751 Ehdr ehdr;
752 memset(&ehdr, 0, sizeof(ehdr));
753 ehdr.e_shoff = 0x1000;
754 ehdr.e_shnum = 10;
755 ehdr.e_shentsize = sizeof(Shdr);
756 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
757
Christopher Ferrise69f4702017-10-19 16:08:58 -0700758 uint64_t load_bias = 0;
759 ASSERT_TRUE(elf->Init(&load_bias));
760 EXPECT_EQ(0U, load_bias);
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700761}
762
763TEST_F(ElfInterfaceTest, init_section_headers_malformed32) {
764 InitSectionHeadersMalformed<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>();
765}
766
767TEST_F(ElfInterfaceTest, init_section_headers_malformed64) {
768 InitSectionHeadersMalformed<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>();
769}
770
771template <typename Ehdr, typename Shdr, typename Sym, typename ElfInterfaceType>
772void ElfInterfaceTest::InitSectionHeaders(uint64_t entry_size) {
773 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_));
774
775 uint64_t offset = 0x1000;
776
777 Ehdr ehdr;
778 memset(&ehdr, 0, sizeof(ehdr));
779 ehdr.e_shoff = offset;
780 ehdr.e_shnum = 10;
781 ehdr.e_shentsize = entry_size;
782 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
783
784 offset += ehdr.e_shentsize;
785
786 Shdr shdr;
787 memset(&shdr, 0, sizeof(shdr));
788 shdr.sh_type = SHT_SYMTAB;
789 shdr.sh_link = 4;
790 shdr.sh_addr = 0x5000;
791 shdr.sh_offset = 0x5000;
792 shdr.sh_entsize = sizeof(Sym);
793 shdr.sh_size = shdr.sh_entsize * 10;
794 memory_.SetMemory(offset, &shdr, sizeof(shdr));
795 offset += ehdr.e_shentsize;
796
797 memset(&shdr, 0, sizeof(shdr));
798 shdr.sh_type = SHT_DYNSYM;
799 shdr.sh_link = 4;
800 shdr.sh_addr = 0x6000;
801 shdr.sh_offset = 0x6000;
802 shdr.sh_entsize = sizeof(Sym);
803 shdr.sh_size = shdr.sh_entsize * 10;
804 memory_.SetMemory(offset, &shdr, sizeof(shdr));
805 offset += ehdr.e_shentsize;
806
807 memset(&shdr, 0, sizeof(shdr));
808 shdr.sh_type = SHT_PROGBITS;
809 shdr.sh_name = 0xa000;
810 memory_.SetMemory(offset, &shdr, sizeof(shdr));
811 offset += ehdr.e_shentsize;
812
813 // The string data for the entries.
814 memset(&shdr, 0, sizeof(shdr));
815 shdr.sh_type = SHT_STRTAB;
816 shdr.sh_name = 0x20000;
817 shdr.sh_offset = 0xf000;
818 shdr.sh_size = 0x1000;
819 memory_.SetMemory(offset, &shdr, sizeof(shdr));
820 offset += ehdr.e_shentsize;
821
822 InitSym<Sym>(0x5000, 0x90000, 0x1000, 0x100, 0xf000, "function_one");
823 InitSym<Sym>(0x6000, 0xd0000, 0x1000, 0x300, 0xf000, "function_two");
824
Christopher Ferrise69f4702017-10-19 16:08:58 -0700825 uint64_t load_bias = 0;
826 ASSERT_TRUE(elf->Init(&load_bias));
827 EXPECT_EQ(0U, load_bias);
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700828 EXPECT_EQ(0U, elf->debug_frame_offset());
829 EXPECT_EQ(0U, elf->debug_frame_size());
830 EXPECT_EQ(0U, elf->gnu_debugdata_offset());
831 EXPECT_EQ(0U, elf->gnu_debugdata_size());
832
833 // Look in the first symbol table.
834 std::string name;
835 uint64_t name_offset;
Christopher Ferrise69f4702017-10-19 16:08:58 -0700836 ASSERT_TRUE(elf->GetFunctionName(0x90010, 0, &name, &name_offset));
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700837 EXPECT_EQ("function_one", name);
838 EXPECT_EQ(16U, name_offset);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700839 ASSERT_TRUE(elf->GetFunctionName(0xd0020, 0, &name, &name_offset));
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700840 EXPECT_EQ("function_two", name);
841 EXPECT_EQ(32U, name_offset);
842}
843
844TEST_F(ElfInterfaceTest, init_section_headers32) {
845 InitSectionHeaders<Elf32_Ehdr, Elf32_Shdr, Elf32_Sym, ElfInterface32>(sizeof(Elf32_Shdr));
846}
847
848TEST_F(ElfInterfaceTest, init_section_headers64) {
849 InitSectionHeaders<Elf64_Ehdr, Elf64_Shdr, Elf64_Sym, ElfInterface64>(sizeof(Elf64_Shdr));
850}
851
852TEST_F(ElfInterfaceTest, init_section_headers_non_std_entry_size32) {
853 InitSectionHeaders<Elf32_Ehdr, Elf32_Shdr, Elf32_Sym, ElfInterface32>(0x100);
854}
855
856TEST_F(ElfInterfaceTest, init_section_headers_non_std_entry_size64) {
857 InitSectionHeaders<Elf64_Ehdr, Elf64_Shdr, Elf64_Sym, ElfInterface64>(0x100);
858}
859
860template <typename Ehdr, typename Shdr, typename ElfInterfaceType>
861void ElfInterfaceTest::InitSectionHeadersOffsets() {
862 std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_));
863
864 uint64_t offset = 0x2000;
865
866 Ehdr ehdr;
867 memset(&ehdr, 0, sizeof(ehdr));
868 ehdr.e_shoff = offset;
869 ehdr.e_shnum = 10;
870 ehdr.e_shentsize = sizeof(Shdr);
871 ehdr.e_shstrndx = 2;
872 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
873
874 offset += ehdr.e_shentsize;
875
876 Shdr shdr;
877 memset(&shdr, 0, sizeof(shdr));
878 shdr.sh_type = SHT_PROGBITS;
879 shdr.sh_link = 2;
880 shdr.sh_name = 0x200;
881 shdr.sh_addr = 0x5000;
882 shdr.sh_offset = 0x5000;
883 shdr.sh_entsize = 0x100;
884 shdr.sh_size = 0x800;
885 memory_.SetMemory(offset, &shdr, sizeof(shdr));
886 offset += ehdr.e_shentsize;
887
888 // The string data for section header names.
889 memset(&shdr, 0, sizeof(shdr));
890 shdr.sh_type = SHT_STRTAB;
891 shdr.sh_name = 0x20000;
892 shdr.sh_offset = 0xf000;
893 shdr.sh_size = 0x1000;
894 memory_.SetMemory(offset, &shdr, sizeof(shdr));
895 offset += ehdr.e_shentsize;
896
897 memset(&shdr, 0, sizeof(shdr));
898 shdr.sh_type = SHT_PROGBITS;
899 shdr.sh_link = 2;
900 shdr.sh_name = 0x100;
901 shdr.sh_addr = 0x6000;
902 shdr.sh_offset = 0x6000;
903 shdr.sh_entsize = 0x100;
904 shdr.sh_size = 0x500;
905 memory_.SetMemory(offset, &shdr, sizeof(shdr));
906 offset += ehdr.e_shentsize;
907
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700908 memset(&shdr, 0, sizeof(shdr));
909 shdr.sh_type = SHT_PROGBITS;
910 shdr.sh_link = 2;
911 shdr.sh_name = 0x300;
912 shdr.sh_addr = 0x7000;
913 shdr.sh_offset = 0x7000;
914 shdr.sh_entsize = 0x100;
915 shdr.sh_size = 0x800;
916 memory_.SetMemory(offset, &shdr, sizeof(shdr));
917 offset += ehdr.e_shentsize;
918
919 memset(&shdr, 0, sizeof(shdr));
920 shdr.sh_type = SHT_PROGBITS;
921 shdr.sh_link = 2;
922 shdr.sh_name = 0x400;
923 shdr.sh_addr = 0x6000;
924 shdr.sh_offset = 0xa000;
925 shdr.sh_entsize = 0x100;
926 shdr.sh_size = 0xf00;
927 memory_.SetMemory(offset, &shdr, sizeof(shdr));
928 offset += ehdr.e_shentsize;
929
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700930 memory_.SetMemory(0xf100, ".debug_frame", sizeof(".debug_frame"));
931 memory_.SetMemory(0xf200, ".gnu_debugdata", sizeof(".gnu_debugdata"));
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700932 memory_.SetMemory(0xf300, ".eh_frame", sizeof(".eh_frame"));
933 memory_.SetMemory(0xf400, ".eh_frame_hdr", sizeof(".eh_frame_hdr"));
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700934
Christopher Ferrise69f4702017-10-19 16:08:58 -0700935 uint64_t load_bias = 0;
936 ASSERT_TRUE(elf->Init(&load_bias));
937 EXPECT_EQ(0U, load_bias);
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700938 EXPECT_EQ(0x6000U, elf->debug_frame_offset());
939 EXPECT_EQ(0x500U, elf->debug_frame_size());
940 EXPECT_EQ(0x5000U, elf->gnu_debugdata_offset());
941 EXPECT_EQ(0x800U, elf->gnu_debugdata_size());
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700942 EXPECT_EQ(0x7000U, elf->eh_frame_offset());
943 EXPECT_EQ(0x800U, elf->eh_frame_size());
944 EXPECT_EQ(0xa000U, elf->eh_frame_hdr_offset());
945 EXPECT_EQ(0xf00U, elf->eh_frame_hdr_size());
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700946}
947
948TEST_F(ElfInterfaceTest, init_section_headers_offsets32) {
949 InitSectionHeadersOffsets<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>();
950}
951
952TEST_F(ElfInterfaceTest, init_section_headers_offsets64) {
953 InitSectionHeadersOffsets<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>();
954}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700955
Christopher Ferris150db122017-12-20 18:49:01 -0800956TEST_F(ElfInterfaceTest, is_valid_pc_from_pt_load) {
957 std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_));
958
959 Elf32_Ehdr ehdr;
960 memset(&ehdr, 0, sizeof(ehdr));
961 ehdr.e_phoff = 0x100;
962 ehdr.e_phnum = 1;
963 ehdr.e_phentsize = sizeof(Elf32_Phdr);
964 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
965
966 Elf32_Phdr phdr;
967 memset(&phdr, 0, sizeof(phdr));
968 phdr.p_type = PT_LOAD;
969 phdr.p_vaddr = 0;
970 phdr.p_memsz = 0x10000;
971 phdr.p_flags = PF_R | PF_X;
972 phdr.p_align = 0x1000;
973 memory_.SetMemory(0x100, &phdr, sizeof(phdr));
974
975 uint64_t load_bias = 0;
976 ASSERT_TRUE(elf->Init(&load_bias));
977 EXPECT_EQ(0U, load_bias);
978 EXPECT_TRUE(elf->IsValidPc(0));
979 EXPECT_TRUE(elf->IsValidPc(0x5000));
980 EXPECT_TRUE(elf->IsValidPc(0xffff));
981 EXPECT_FALSE(elf->IsValidPc(0x10000));
982}
983
984TEST_F(ElfInterfaceTest, is_valid_pc_from_pt_load_non_zero_load_bias) {
985 std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_));
986
987 Elf32_Ehdr ehdr;
988 memset(&ehdr, 0, sizeof(ehdr));
989 ehdr.e_phoff = 0x100;
990 ehdr.e_phnum = 1;
991 ehdr.e_phentsize = sizeof(Elf32_Phdr);
992 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
993
994 Elf32_Phdr phdr;
995 memset(&phdr, 0, sizeof(phdr));
996 phdr.p_type = PT_LOAD;
997 phdr.p_vaddr = 0x2000;
998 phdr.p_memsz = 0x10000;
999 phdr.p_flags = PF_R | PF_X;
1000 phdr.p_align = 0x1000;
1001 memory_.SetMemory(0x100, &phdr, sizeof(phdr));
1002
1003 uint64_t load_bias = 0;
1004 ASSERT_TRUE(elf->Init(&load_bias));
1005 EXPECT_EQ(0x2000U, load_bias);
1006 EXPECT_FALSE(elf->IsValidPc(0));
1007 EXPECT_FALSE(elf->IsValidPc(0x1000));
1008 EXPECT_FALSE(elf->IsValidPc(0x1fff));
1009 EXPECT_TRUE(elf->IsValidPc(0x2000));
1010 EXPECT_TRUE(elf->IsValidPc(0x5000));
1011 EXPECT_TRUE(elf->IsValidPc(0x11fff));
1012 EXPECT_FALSE(elf->IsValidPc(0x12000));
1013}
1014
1015TEST_F(ElfInterfaceTest, is_valid_pc_from_debug_frame) {
1016 std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_));
1017
1018 uint64_t sh_offset = 0x100;
1019
1020 Elf32_Ehdr ehdr;
1021 memset(&ehdr, 0, sizeof(ehdr));
1022 ehdr.e_shstrndx = 1;
1023 ehdr.e_shoff = sh_offset;
1024 ehdr.e_shentsize = sizeof(Elf32_Shdr);
1025 ehdr.e_shnum = 3;
1026 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
1027
1028 Elf32_Shdr shdr;
1029 memset(&shdr, 0, sizeof(shdr));
1030 shdr.sh_type = SHT_NULL;
1031 memory_.SetMemory(sh_offset, &shdr, sizeof(shdr));
1032
1033 sh_offset += sizeof(shdr);
1034 memset(&shdr, 0, sizeof(shdr));
1035 shdr.sh_type = SHT_STRTAB;
1036 shdr.sh_name = 1;
1037 shdr.sh_offset = 0x500;
1038 shdr.sh_size = 0x100;
1039 memory_.SetMemory(sh_offset, &shdr, sizeof(shdr));
1040 memory_.SetMemory(0x500, ".debug_frame");
1041
1042 sh_offset += sizeof(shdr);
1043 memset(&shdr, 0, sizeof(shdr));
1044 shdr.sh_type = SHT_PROGBITS;
1045 shdr.sh_name = 0;
1046 shdr.sh_addr = 0x600;
1047 shdr.sh_offset = 0x600;
1048 shdr.sh_size = 0x200;
1049 memory_.SetMemory(sh_offset, &shdr, sizeof(shdr));
1050
1051 // CIE 32.
1052 memory_.SetData32(0x600, 0xfc);
1053 memory_.SetData32(0x604, 0xffffffff);
1054 memory_.SetData8(0x608, 1);
1055 memory_.SetData8(0x609, '\0');
1056 memory_.SetData8(0x60a, 0x4);
1057 memory_.SetData8(0x60b, 0x4);
1058 memory_.SetData8(0x60c, 0x1);
1059
1060 // FDE 32.
1061 memory_.SetData32(0x700, 0xfc);
1062 memory_.SetData32(0x704, 0);
1063 memory_.SetData32(0x708, 0x2100);
1064 memory_.SetData32(0x70c, 0x200);
1065
1066 uint64_t load_bias = 0;
1067 ASSERT_TRUE(elf->Init(&load_bias));
1068 elf->InitHeaders();
1069 EXPECT_EQ(0U, load_bias);
1070 EXPECT_FALSE(elf->IsValidPc(0));
1071 EXPECT_FALSE(elf->IsValidPc(0x20ff));
1072 EXPECT_TRUE(elf->IsValidPc(0x2100));
1073 EXPECT_TRUE(elf->IsValidPc(0x2200));
1074 EXPECT_TRUE(elf->IsValidPc(0x22ff));
1075 EXPECT_FALSE(elf->IsValidPc(0x2300));
1076}
1077
1078TEST_F(ElfInterfaceTest, is_valid_pc_from_eh_frame) {
1079 std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_));
1080
1081 uint64_t sh_offset = 0x100;
1082
1083 Elf32_Ehdr ehdr;
1084 memset(&ehdr, 0, sizeof(ehdr));
1085 ehdr.e_shstrndx = 1;
1086 ehdr.e_shoff = sh_offset;
1087 ehdr.e_shentsize = sizeof(Elf32_Shdr);
1088 ehdr.e_shnum = 3;
1089 memory_.SetMemory(0, &ehdr, sizeof(ehdr));
1090
1091 Elf32_Shdr shdr;
1092 memset(&shdr, 0, sizeof(shdr));
1093 shdr.sh_type = SHT_NULL;
1094 memory_.SetMemory(sh_offset, &shdr, sizeof(shdr));
1095
1096 sh_offset += sizeof(shdr);
1097 memset(&shdr, 0, sizeof(shdr));
1098 shdr.sh_type = SHT_STRTAB;
1099 shdr.sh_name = 1;
1100 shdr.sh_offset = 0x500;
1101 shdr.sh_size = 0x100;
1102 memory_.SetMemory(sh_offset, &shdr, sizeof(shdr));
1103 memory_.SetMemory(0x500, ".eh_frame");
1104
1105 sh_offset += sizeof(shdr);
1106 memset(&shdr, 0, sizeof(shdr));
1107 shdr.sh_type = SHT_PROGBITS;
1108 shdr.sh_name = 0;
1109 shdr.sh_addr = 0x600;
1110 shdr.sh_offset = 0x600;
1111 shdr.sh_size = 0x200;
1112 memory_.SetMemory(sh_offset, &shdr, sizeof(shdr));
1113
1114 // CIE 32.
1115 memory_.SetData32(0x600, 0xfc);
1116 memory_.SetData32(0x604, 0);
1117 memory_.SetData8(0x608, 1);
1118 memory_.SetData8(0x609, '\0');
1119 memory_.SetData8(0x60a, 0x4);
1120 memory_.SetData8(0x60b, 0x4);
1121 memory_.SetData8(0x60c, 0x1);
1122
1123 // FDE 32.
1124 memory_.SetData32(0x700, 0xfc);
1125 memory_.SetData32(0x704, 0x104);
1126 memory_.SetData32(0x708, 0x20f8);
1127 memory_.SetData32(0x70c, 0x200);
1128
1129 uint64_t load_bias = 0;
1130 ASSERT_TRUE(elf->Init(&load_bias));
1131 elf->InitHeaders();
1132 EXPECT_EQ(0U, load_bias);
1133 EXPECT_FALSE(elf->IsValidPc(0));
1134 EXPECT_FALSE(elf->IsValidPc(0x27ff));
1135 EXPECT_TRUE(elf->IsValidPc(0x2800));
1136 EXPECT_TRUE(elf->IsValidPc(0x2900));
1137 EXPECT_TRUE(elf->IsValidPc(0x29ff));
1138 EXPECT_FALSE(elf->IsValidPc(0x2a00));
1139}
1140
Christopher Ferrisd226a512017-07-14 10:37:19 -07001141} // namespace unwindstack