Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 1 | /* |
| 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 Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 23 | #include <unwindstack/ElfInterface.h> |
| 24 | |
| 25 | #include "DwarfEncoding.h" |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 26 | #include "ElfInterfaceArm.h" |
| 27 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 28 | #include "ElfFake.h" |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 29 | #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 Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 39 | namespace unwindstack { |
| 40 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 41 | class 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 Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 66 | 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 Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 77 | void Soname(); |
| 78 | |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 79 | template <typename ElfInterfaceType> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 80 | void SonameAfterDtNull(); |
| 81 | |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 82 | template <typename ElfInterfaceType> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 83 | void SonameSize(); |
| 84 | |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 85 | template <typename ElfInterfaceType> |
| 86 | void SonameMissingMap(); |
| 87 | |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 88 | 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 Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 100 | 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 Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 113 | MemoryFake memory_; |
| 114 | }; |
| 115 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 116 | template <typename Sym> |
| 117 | void ElfInterfaceTest::InitSym(uint64_t offset, uint32_t value, uint32_t size, uint32_t name_offset, |
| 118 | uint64_t sym_offset, const char* name) { |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 119 | Sym sym = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 120 | 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 Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 130 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> |
| 131 | void ElfInterfaceTest::SinglePtLoad() { |
| 132 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 133 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 134 | Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 135 | ehdr.e_phoff = 0x100; |
| 136 | ehdr.e_phnum = 1; |
| 137 | ehdr.e_phentsize = sizeof(Phdr); |
| 138 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 139 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 140 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 141 | 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 Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 148 | uint64_t load_bias = 0; |
| 149 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 150 | EXPECT_EQ(0x2000U, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 151 | |
| 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 | |
| 160 | TEST_F(ElfInterfaceTest, elf32_single_pt_load) { |
| 161 | SinglePtLoad<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); |
| 162 | } |
| 163 | |
| 164 | TEST_F(ElfInterfaceTest, elf64_single_pt_load) { |
| 165 | SinglePtLoad<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); |
| 166 | } |
| 167 | |
| 168 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> |
| 169 | void ElfInterfaceTest::MultipleExecutablePtLoads() { |
| 170 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 171 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 172 | Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 173 | ehdr.e_phoff = 0x100; |
| 174 | ehdr.e_phnum = 3; |
| 175 | ehdr.e_phentsize = sizeof(Phdr); |
| 176 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 177 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 178 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 179 | 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 Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 204 | uint64_t load_bias = 0; |
| 205 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 206 | EXPECT_EQ(0x2000U, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 207 | |
| 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 | |
| 227 | TEST_F(ElfInterfaceTest, elf32_multiple_executable_pt_loads) { |
| 228 | MultipleExecutablePtLoads<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); |
| 229 | } |
| 230 | |
| 231 | TEST_F(ElfInterfaceTest, elf64_multiple_executable_pt_loads) { |
| 232 | MultipleExecutablePtLoads<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); |
| 233 | } |
| 234 | |
| 235 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> |
| 236 | void ElfInterfaceTest::MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr() { |
| 237 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 238 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 239 | Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 240 | 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 Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 245 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 246 | 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 Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 271 | uint64_t load_bias = 0; |
| 272 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 273 | EXPECT_EQ(0x2000U, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 274 | |
| 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 | |
| 294 | TEST_F(ElfInterfaceTest, elf32_multiple_executable_pt_loads_increments_not_size_of_phdr) { |
| 295 | MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, |
| 296 | ElfInterface32>(); |
| 297 | } |
| 298 | |
| 299 | TEST_F(ElfInterfaceTest, elf64_multiple_executable_pt_loads_increments_not_size_of_phdr) { |
| 300 | MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, |
| 301 | ElfInterface64>(); |
| 302 | } |
| 303 | |
| 304 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> |
| 305 | void ElfInterfaceTest::NonExecutablePtLoads() { |
| 306 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 307 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 308 | Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 309 | ehdr.e_phoff = 0x100; |
| 310 | ehdr.e_phnum = 3; |
| 311 | ehdr.e_phentsize = sizeof(Phdr); |
| 312 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 313 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 314 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 315 | 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 Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 340 | uint64_t load_bias = 0; |
| 341 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 342 | EXPECT_EQ(0U, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 343 | |
| 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 | |
| 353 | TEST_F(ElfInterfaceTest, elf32_non_executable_pt_loads) { |
| 354 | NonExecutablePtLoads<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); |
| 355 | } |
| 356 | |
| 357 | TEST_F(ElfInterfaceTest, elf64_non_executable_pt_loads) { |
| 358 | NonExecutablePtLoads<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); |
| 359 | } |
| 360 | |
| 361 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> |
| 362 | void ElfInterfaceTest::ManyPhdrs() { |
| 363 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 364 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 365 | Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 366 | ehdr.e_phoff = 0x100; |
| 367 | ehdr.e_phnum = 7; |
| 368 | ehdr.e_phentsize = sizeof(Phdr); |
| 369 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 370 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 371 | uint64_t phdr_offset = 0x100; |
| 372 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 373 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 374 | 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 Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 412 | uint64_t load_bias = 0; |
| 413 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 414 | EXPECT_EQ(0x2000U, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 415 | |
| 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 | |
| 425 | TEST_F(ElfInterfaceTest, elf32_many_phdrs) { |
| 426 | ElfInterfaceTest::ManyPhdrs<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); |
| 427 | } |
| 428 | |
| 429 | TEST_F(ElfInterfaceTest, elf64_many_phdrs) { |
| 430 | ElfInterfaceTest::ManyPhdrs<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); |
| 431 | } |
| 432 | |
| 433 | TEST_F(ElfInterfaceTest, elf32_arm) { |
| 434 | ElfInterfaceArm elf_arm(&memory_); |
| 435 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 436 | Elf32_Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 437 | 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 Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 442 | Elf32_Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 443 | phdr.p_type = PT_ARM_EXIDX; |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 444 | phdr.p_offset = 0x2000; |
| 445 | phdr.p_filesz = 16; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 446 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 447 | |
| 448 | // Add arm exidx entries. |
| 449 | memory_.SetData32(0x2000, 0x1000); |
| 450 | memory_.SetData32(0x2008, 0x1000); |
| 451 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 452 | uint64_t load_bias = 0; |
| 453 | ASSERT_TRUE(elf_arm.Init(&load_bias)); |
| 454 | EXPECT_EQ(0U, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 455 | |
| 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 Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 468 | template <typename Ehdr, typename Phdr, typename Shdr, typename Dyn> |
| 469 | void ElfInterfaceTest::SonameInit(SonameTestEnum test_type) { |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 470 | Ehdr ehdr = {}; |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 471 | ehdr.e_shoff = 0x200; |
| 472 | ehdr.e_shnum = 2; |
| 473 | ehdr.e_shentsize = sizeof(Shdr); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 474 | ehdr.e_phoff = 0x100; |
| 475 | ehdr.e_phnum = 1; |
| 476 | ehdr.e_phentsize = sizeof(Phdr); |
| 477 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 478 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 479 | Shdr shdr = {}; |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 480 | 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 Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 489 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 490 | 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 Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 499 | dyn.d_un.d_ptr = 0x10100; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 500 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); |
| 501 | offset += sizeof(dyn); |
| 502 | |
| 503 | dyn.d_tag = DT_STRSZ; |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 504 | if (test_type == SONAME_DTSIZE_SMALL) { |
| 505 | dyn.d_un.d_val = 0x10; |
| 506 | } else { |
| 507 | dyn.d_un.d_val = 0x1000; |
| 508 | } |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 509 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); |
| 510 | offset += sizeof(dyn); |
| 511 | |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 512 | 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 Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 518 | 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 Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | template <typename ElfInterfaceType> |
| 530 | void ElfInterfaceTest::Soname() { |
| 531 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 532 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 533 | uint64_t load_bias = 0; |
| 534 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 535 | EXPECT_EQ(0U, load_bias); |
| 536 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 537 | std::string name; |
| 538 | ASSERT_TRUE(elf->GetSoname(&name)); |
| 539 | ASSERT_STREQ("fake_soname.so", name.c_str()); |
| 540 | } |
| 541 | |
| 542 | TEST_F(ElfInterfaceTest, elf32_soname) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 543 | SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(); |
| 544 | Soname<ElfInterface32>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | TEST_F(ElfInterfaceTest, elf64_soname) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 548 | SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(); |
| 549 | Soname<ElfInterface64>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 550 | } |
| 551 | |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 552 | template <typename ElfInterfaceType> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 553 | void ElfInterfaceTest::SonameAfterDtNull() { |
| 554 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 555 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 556 | uint64_t load_bias = 0; |
| 557 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 558 | EXPECT_EQ(0U, load_bias); |
| 559 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 560 | std::string name; |
| 561 | ASSERT_FALSE(elf->GetSoname(&name)); |
| 562 | } |
| 563 | |
| 564 | TEST_F(ElfInterfaceTest, elf32_soname_after_dt_null) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 565 | SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_DTNULL_AFTER); |
| 566 | SonameAfterDtNull<ElfInterface32>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | TEST_F(ElfInterfaceTest, elf64_soname_after_dt_null) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 570 | SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(SONAME_DTNULL_AFTER); |
| 571 | SonameAfterDtNull<ElfInterface64>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 572 | } |
| 573 | |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 574 | template <typename ElfInterfaceType> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 575 | void ElfInterfaceTest::SonameSize() { |
| 576 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 577 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 578 | uint64_t load_bias = 0; |
| 579 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 580 | EXPECT_EQ(0U, load_bias); |
| 581 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 582 | std::string name; |
| 583 | ASSERT_FALSE(elf->GetSoname(&name)); |
| 584 | } |
| 585 | |
| 586 | TEST_F(ElfInterfaceTest, elf32_soname_size) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 587 | SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_DTSIZE_SMALL); |
| 588 | SonameSize<ElfInterface32>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | TEST_F(ElfInterfaceTest, elf64_soname_size) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 592 | 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. |
| 598 | template <typename ElfInterfaceType> |
| 599 | void 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 | |
| 610 | TEST_F(ElfInterfaceTest, elf32_soname_missing_map) { |
| 611 | SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_MISSING_MAP); |
| 612 | SonameMissingMap<ElfInterface32>(); |
| 613 | } |
| 614 | |
| 615 | TEST_F(ElfInterfaceTest, elf64_soname_missing_map) { |
| 616 | SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(SONAME_MISSING_MAP); |
| 617 | SonameMissingMap<ElfInterface64>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 618 | } |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 619 | |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 620 | template <typename ElfType> |
| 621 | void ElfInterfaceTest::InitHeadersEhFrameTest() { |
| 622 | ElfType elf(&memory_); |
| 623 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 624 | elf.FakeSetEhFrameOffset(0x10000); |
| 625 | elf.FakeSetEhFrameSize(0); |
| 626 | elf.FakeSetDebugFrameOffset(0); |
| 627 | elf.FakeSetDebugFrameSize(0); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 628 | |
| 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 Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 634 | elf.InitHeaders(0); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 635 | |
| 636 | EXPECT_FALSE(elf.eh_frame() == nullptr); |
| 637 | EXPECT_TRUE(elf.debug_frame() == nullptr); |
| 638 | } |
| 639 | |
| 640 | TEST_F(ElfInterfaceTest, init_headers_eh_frame32) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 641 | InitHeadersEhFrameTest<ElfInterface32Fake>(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | TEST_F(ElfInterfaceTest, init_headers_eh_frame64) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 645 | InitHeadersEhFrameTest<ElfInterface64Fake>(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | template <typename ElfType> |
| 649 | void ElfInterfaceTest::InitHeadersDebugFrame() { |
| 650 | ElfType elf(&memory_); |
| 651 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 652 | elf.FakeSetEhFrameOffset(0); |
| 653 | elf.FakeSetEhFrameSize(0); |
| 654 | elf.FakeSetDebugFrameOffset(0x5000); |
| 655 | elf.FakeSetDebugFrameSize(0x200); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 656 | |
| 657 | memory_.SetData32(0x5000, 0xfc); |
| 658 | memory_.SetData32(0x5004, 0xffffffff); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame^] | 659 | memory_.SetMemory(0x5008, std::vector<uint8_t>{1, '\0', 4, 8, 2}); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 660 | |
| 661 | memory_.SetData32(0x5100, 0xfc); |
| 662 | memory_.SetData32(0x5104, 0); |
| 663 | memory_.SetData32(0x5108, 0x1500); |
| 664 | memory_.SetData32(0x510c, 0x200); |
| 665 | |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 666 | elf.InitHeaders(0); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 667 | |
| 668 | EXPECT_TRUE(elf.eh_frame() == nullptr); |
| 669 | EXPECT_FALSE(elf.debug_frame() == nullptr); |
| 670 | } |
| 671 | |
| 672 | TEST_F(ElfInterfaceTest, init_headers_debug_frame32) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 673 | InitHeadersDebugFrame<ElfInterface32Fake>(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | TEST_F(ElfInterfaceTest, init_headers_debug_frame64) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 677 | InitHeadersDebugFrame<ElfInterface64Fake>(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 680 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> |
| 681 | void ElfInterfaceTest::InitSectionHeadersMalformed() { |
| 682 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 683 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 684 | Ehdr ehdr = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 685 | ehdr.e_shoff = 0x1000; |
| 686 | ehdr.e_shnum = 10; |
| 687 | ehdr.e_shentsize = sizeof(Shdr); |
| 688 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 689 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 690 | uint64_t load_bias = 0; |
| 691 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 692 | EXPECT_EQ(0U, load_bias); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | TEST_F(ElfInterfaceTest, init_section_headers_malformed32) { |
| 696 | InitSectionHeadersMalformed<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(); |
| 697 | } |
| 698 | |
| 699 | TEST_F(ElfInterfaceTest, init_section_headers_malformed64) { |
| 700 | InitSectionHeadersMalformed<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(); |
| 701 | } |
| 702 | |
| 703 | template <typename Ehdr, typename Shdr, typename Sym, typename ElfInterfaceType> |
| 704 | void ElfInterfaceTest::InitSectionHeaders(uint64_t entry_size) { |
| 705 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 706 | |
| 707 | uint64_t offset = 0x1000; |
| 708 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 709 | Ehdr ehdr = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 710 | ehdr.e_shoff = offset; |
| 711 | ehdr.e_shnum = 10; |
| 712 | ehdr.e_shentsize = entry_size; |
| 713 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 714 | |
| 715 | offset += ehdr.e_shentsize; |
| 716 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 717 | Shdr shdr = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 718 | shdr.sh_type = SHT_SYMTAB; |
| 719 | shdr.sh_link = 4; |
| 720 | shdr.sh_addr = 0x5000; |
| 721 | shdr.sh_offset = 0x5000; |
| 722 | shdr.sh_entsize = sizeof(Sym); |
| 723 | shdr.sh_size = shdr.sh_entsize * 10; |
| 724 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 725 | offset += ehdr.e_shentsize; |
| 726 | |
| 727 | memset(&shdr, 0, sizeof(shdr)); |
| 728 | shdr.sh_type = SHT_DYNSYM; |
| 729 | shdr.sh_link = 4; |
| 730 | shdr.sh_addr = 0x6000; |
| 731 | shdr.sh_offset = 0x6000; |
| 732 | shdr.sh_entsize = sizeof(Sym); |
| 733 | shdr.sh_size = shdr.sh_entsize * 10; |
| 734 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 735 | offset += ehdr.e_shentsize; |
| 736 | |
| 737 | memset(&shdr, 0, sizeof(shdr)); |
| 738 | shdr.sh_type = SHT_PROGBITS; |
| 739 | shdr.sh_name = 0xa000; |
| 740 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 741 | offset += ehdr.e_shentsize; |
| 742 | |
| 743 | // The string data for the entries. |
| 744 | memset(&shdr, 0, sizeof(shdr)); |
| 745 | shdr.sh_type = SHT_STRTAB; |
| 746 | shdr.sh_name = 0x20000; |
| 747 | shdr.sh_offset = 0xf000; |
| 748 | shdr.sh_size = 0x1000; |
| 749 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 750 | offset += ehdr.e_shentsize; |
| 751 | |
| 752 | InitSym<Sym>(0x5000, 0x90000, 0x1000, 0x100, 0xf000, "function_one"); |
| 753 | InitSym<Sym>(0x6000, 0xd0000, 0x1000, 0x300, 0xf000, "function_two"); |
| 754 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 755 | uint64_t load_bias = 0; |
| 756 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 757 | EXPECT_EQ(0U, load_bias); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 758 | EXPECT_EQ(0U, elf->debug_frame_offset()); |
| 759 | EXPECT_EQ(0U, elf->debug_frame_size()); |
| 760 | EXPECT_EQ(0U, elf->gnu_debugdata_offset()); |
| 761 | EXPECT_EQ(0U, elf->gnu_debugdata_size()); |
| 762 | |
| 763 | // Look in the first symbol table. |
| 764 | std::string name; |
| 765 | uint64_t name_offset; |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 766 | ASSERT_TRUE(elf->GetFunctionName(0x90010, &name, &name_offset)); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 767 | EXPECT_EQ("function_one", name); |
| 768 | EXPECT_EQ(16U, name_offset); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 769 | ASSERT_TRUE(elf->GetFunctionName(0xd0020, &name, &name_offset)); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 770 | EXPECT_EQ("function_two", name); |
| 771 | EXPECT_EQ(32U, name_offset); |
| 772 | } |
| 773 | |
| 774 | TEST_F(ElfInterfaceTest, init_section_headers32) { |
| 775 | InitSectionHeaders<Elf32_Ehdr, Elf32_Shdr, Elf32_Sym, ElfInterface32>(sizeof(Elf32_Shdr)); |
| 776 | } |
| 777 | |
| 778 | TEST_F(ElfInterfaceTest, init_section_headers64) { |
| 779 | InitSectionHeaders<Elf64_Ehdr, Elf64_Shdr, Elf64_Sym, ElfInterface64>(sizeof(Elf64_Shdr)); |
| 780 | } |
| 781 | |
| 782 | TEST_F(ElfInterfaceTest, init_section_headers_non_std_entry_size32) { |
| 783 | InitSectionHeaders<Elf32_Ehdr, Elf32_Shdr, Elf32_Sym, ElfInterface32>(0x100); |
| 784 | } |
| 785 | |
| 786 | TEST_F(ElfInterfaceTest, init_section_headers_non_std_entry_size64) { |
| 787 | InitSectionHeaders<Elf64_Ehdr, Elf64_Shdr, Elf64_Sym, ElfInterface64>(0x100); |
| 788 | } |
| 789 | |
| 790 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> |
| 791 | void ElfInterfaceTest::InitSectionHeadersOffsets() { |
| 792 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 793 | |
| 794 | uint64_t offset = 0x2000; |
| 795 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 796 | Ehdr ehdr = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 797 | ehdr.e_shoff = offset; |
| 798 | ehdr.e_shnum = 10; |
| 799 | ehdr.e_shentsize = sizeof(Shdr); |
| 800 | ehdr.e_shstrndx = 2; |
| 801 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 802 | |
| 803 | offset += ehdr.e_shentsize; |
| 804 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 805 | Shdr shdr = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 806 | shdr.sh_type = SHT_PROGBITS; |
| 807 | shdr.sh_link = 2; |
| 808 | shdr.sh_name = 0x200; |
| 809 | shdr.sh_addr = 0x5000; |
| 810 | shdr.sh_offset = 0x5000; |
| 811 | shdr.sh_entsize = 0x100; |
| 812 | shdr.sh_size = 0x800; |
| 813 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 814 | offset += ehdr.e_shentsize; |
| 815 | |
| 816 | // The string data for section header names. |
| 817 | memset(&shdr, 0, sizeof(shdr)); |
| 818 | shdr.sh_type = SHT_STRTAB; |
| 819 | shdr.sh_name = 0x20000; |
| 820 | shdr.sh_offset = 0xf000; |
| 821 | shdr.sh_size = 0x1000; |
| 822 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 823 | offset += ehdr.e_shentsize; |
| 824 | |
| 825 | memset(&shdr, 0, sizeof(shdr)); |
| 826 | shdr.sh_type = SHT_PROGBITS; |
| 827 | shdr.sh_link = 2; |
| 828 | shdr.sh_name = 0x100; |
| 829 | shdr.sh_addr = 0x6000; |
| 830 | shdr.sh_offset = 0x6000; |
| 831 | shdr.sh_entsize = 0x100; |
| 832 | shdr.sh_size = 0x500; |
| 833 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 834 | offset += ehdr.e_shentsize; |
| 835 | |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 836 | memset(&shdr, 0, sizeof(shdr)); |
| 837 | shdr.sh_type = SHT_PROGBITS; |
| 838 | shdr.sh_link = 2; |
| 839 | shdr.sh_name = 0x300; |
| 840 | shdr.sh_addr = 0x7000; |
| 841 | shdr.sh_offset = 0x7000; |
| 842 | shdr.sh_entsize = 0x100; |
| 843 | shdr.sh_size = 0x800; |
| 844 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 845 | offset += ehdr.e_shentsize; |
| 846 | |
| 847 | memset(&shdr, 0, sizeof(shdr)); |
| 848 | shdr.sh_type = SHT_PROGBITS; |
| 849 | shdr.sh_link = 2; |
| 850 | shdr.sh_name = 0x400; |
| 851 | shdr.sh_addr = 0x6000; |
| 852 | shdr.sh_offset = 0xa000; |
| 853 | shdr.sh_entsize = 0x100; |
| 854 | shdr.sh_size = 0xf00; |
| 855 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 856 | offset += ehdr.e_shentsize; |
| 857 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 858 | memory_.SetMemory(0xf100, ".debug_frame", sizeof(".debug_frame")); |
| 859 | memory_.SetMemory(0xf200, ".gnu_debugdata", sizeof(".gnu_debugdata")); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 860 | memory_.SetMemory(0xf300, ".eh_frame", sizeof(".eh_frame")); |
| 861 | memory_.SetMemory(0xf400, ".eh_frame_hdr", sizeof(".eh_frame_hdr")); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 862 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 863 | uint64_t load_bias = 0; |
| 864 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 865 | EXPECT_EQ(0U, load_bias); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 866 | EXPECT_EQ(0x6000U, elf->debug_frame_offset()); |
| 867 | EXPECT_EQ(0x500U, elf->debug_frame_size()); |
| 868 | EXPECT_EQ(0x5000U, elf->gnu_debugdata_offset()); |
| 869 | EXPECT_EQ(0x800U, elf->gnu_debugdata_size()); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 870 | EXPECT_EQ(0x7000U, elf->eh_frame_offset()); |
| 871 | EXPECT_EQ(0x800U, elf->eh_frame_size()); |
| 872 | EXPECT_EQ(0xa000U, elf->eh_frame_hdr_offset()); |
| 873 | EXPECT_EQ(0xf00U, elf->eh_frame_hdr_size()); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | TEST_F(ElfInterfaceTest, init_section_headers_offsets32) { |
| 877 | InitSectionHeadersOffsets<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(); |
| 878 | } |
| 879 | |
| 880 | TEST_F(ElfInterfaceTest, init_section_headers_offsets64) { |
| 881 | InitSectionHeadersOffsets<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(); |
| 882 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 883 | |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 884 | TEST_F(ElfInterfaceTest, is_valid_pc_from_pt_load) { |
| 885 | std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_)); |
| 886 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 887 | Elf32_Ehdr ehdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 888 | ehdr.e_phoff = 0x100; |
| 889 | ehdr.e_phnum = 1; |
| 890 | ehdr.e_phentsize = sizeof(Elf32_Phdr); |
| 891 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 892 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 893 | Elf32_Phdr phdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 894 | phdr.p_type = PT_LOAD; |
| 895 | phdr.p_vaddr = 0; |
| 896 | phdr.p_memsz = 0x10000; |
| 897 | phdr.p_flags = PF_R | PF_X; |
| 898 | phdr.p_align = 0x1000; |
| 899 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 900 | |
| 901 | uint64_t load_bias = 0; |
| 902 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 903 | EXPECT_EQ(0U, load_bias); |
| 904 | EXPECT_TRUE(elf->IsValidPc(0)); |
| 905 | EXPECT_TRUE(elf->IsValidPc(0x5000)); |
| 906 | EXPECT_TRUE(elf->IsValidPc(0xffff)); |
| 907 | EXPECT_FALSE(elf->IsValidPc(0x10000)); |
| 908 | } |
| 909 | |
| 910 | TEST_F(ElfInterfaceTest, is_valid_pc_from_pt_load_non_zero_load_bias) { |
| 911 | std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_)); |
| 912 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 913 | Elf32_Ehdr ehdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 914 | ehdr.e_phoff = 0x100; |
| 915 | ehdr.e_phnum = 1; |
| 916 | ehdr.e_phentsize = sizeof(Elf32_Phdr); |
| 917 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 918 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 919 | Elf32_Phdr phdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 920 | phdr.p_type = PT_LOAD; |
| 921 | phdr.p_vaddr = 0x2000; |
| 922 | phdr.p_memsz = 0x10000; |
| 923 | phdr.p_flags = PF_R | PF_X; |
| 924 | phdr.p_align = 0x1000; |
| 925 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 926 | |
| 927 | uint64_t load_bias = 0; |
| 928 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 929 | EXPECT_EQ(0x2000U, load_bias); |
| 930 | EXPECT_FALSE(elf->IsValidPc(0)); |
| 931 | EXPECT_FALSE(elf->IsValidPc(0x1000)); |
| 932 | EXPECT_FALSE(elf->IsValidPc(0x1fff)); |
| 933 | EXPECT_TRUE(elf->IsValidPc(0x2000)); |
| 934 | EXPECT_TRUE(elf->IsValidPc(0x5000)); |
| 935 | EXPECT_TRUE(elf->IsValidPc(0x11fff)); |
| 936 | EXPECT_FALSE(elf->IsValidPc(0x12000)); |
| 937 | } |
| 938 | |
| 939 | TEST_F(ElfInterfaceTest, is_valid_pc_from_debug_frame) { |
| 940 | std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_)); |
| 941 | |
| 942 | uint64_t sh_offset = 0x100; |
| 943 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 944 | Elf32_Ehdr ehdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 945 | ehdr.e_shstrndx = 1; |
| 946 | ehdr.e_shoff = sh_offset; |
| 947 | ehdr.e_shentsize = sizeof(Elf32_Shdr); |
| 948 | ehdr.e_shnum = 3; |
| 949 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 950 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 951 | Elf32_Shdr shdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 952 | shdr.sh_type = SHT_NULL; |
| 953 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 954 | |
| 955 | sh_offset += sizeof(shdr); |
| 956 | memset(&shdr, 0, sizeof(shdr)); |
| 957 | shdr.sh_type = SHT_STRTAB; |
| 958 | shdr.sh_name = 1; |
| 959 | shdr.sh_offset = 0x500; |
| 960 | shdr.sh_size = 0x100; |
| 961 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 962 | memory_.SetMemory(0x500, ".debug_frame"); |
| 963 | |
| 964 | sh_offset += sizeof(shdr); |
| 965 | memset(&shdr, 0, sizeof(shdr)); |
| 966 | shdr.sh_type = SHT_PROGBITS; |
| 967 | shdr.sh_name = 0; |
| 968 | shdr.sh_addr = 0x600; |
| 969 | shdr.sh_offset = 0x600; |
| 970 | shdr.sh_size = 0x200; |
| 971 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 972 | |
| 973 | // CIE 32. |
| 974 | memory_.SetData32(0x600, 0xfc); |
| 975 | memory_.SetData32(0x604, 0xffffffff); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame^] | 976 | memory_.SetMemory(0x608, std::vector<uint8_t>{1, '\0', 4, 4, 1}); |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 977 | |
| 978 | // FDE 32. |
| 979 | memory_.SetData32(0x700, 0xfc); |
| 980 | memory_.SetData32(0x704, 0); |
| 981 | memory_.SetData32(0x708, 0x2100); |
| 982 | memory_.SetData32(0x70c, 0x200); |
| 983 | |
| 984 | uint64_t load_bias = 0; |
| 985 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 986 | elf->InitHeaders(0); |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 987 | EXPECT_EQ(0U, load_bias); |
| 988 | EXPECT_FALSE(elf->IsValidPc(0)); |
| 989 | EXPECT_FALSE(elf->IsValidPc(0x20ff)); |
| 990 | EXPECT_TRUE(elf->IsValidPc(0x2100)); |
| 991 | EXPECT_TRUE(elf->IsValidPc(0x2200)); |
| 992 | EXPECT_TRUE(elf->IsValidPc(0x22ff)); |
| 993 | EXPECT_FALSE(elf->IsValidPc(0x2300)); |
| 994 | } |
| 995 | |
| 996 | TEST_F(ElfInterfaceTest, is_valid_pc_from_eh_frame) { |
| 997 | std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_)); |
| 998 | |
| 999 | uint64_t sh_offset = 0x100; |
| 1000 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1001 | Elf32_Ehdr ehdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1002 | ehdr.e_shstrndx = 1; |
| 1003 | ehdr.e_shoff = sh_offset; |
| 1004 | ehdr.e_shentsize = sizeof(Elf32_Shdr); |
| 1005 | ehdr.e_shnum = 3; |
| 1006 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1007 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1008 | Elf32_Shdr shdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1009 | shdr.sh_type = SHT_NULL; |
| 1010 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 1011 | |
| 1012 | sh_offset += sizeof(shdr); |
| 1013 | memset(&shdr, 0, sizeof(shdr)); |
| 1014 | shdr.sh_type = SHT_STRTAB; |
| 1015 | shdr.sh_name = 1; |
| 1016 | shdr.sh_offset = 0x500; |
| 1017 | shdr.sh_size = 0x100; |
| 1018 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 1019 | memory_.SetMemory(0x500, ".eh_frame"); |
| 1020 | |
| 1021 | sh_offset += sizeof(shdr); |
| 1022 | memset(&shdr, 0, sizeof(shdr)); |
| 1023 | shdr.sh_type = SHT_PROGBITS; |
| 1024 | shdr.sh_name = 0; |
| 1025 | shdr.sh_addr = 0x600; |
| 1026 | shdr.sh_offset = 0x600; |
| 1027 | shdr.sh_size = 0x200; |
| 1028 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 1029 | |
| 1030 | // CIE 32. |
| 1031 | memory_.SetData32(0x600, 0xfc); |
| 1032 | memory_.SetData32(0x604, 0); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame^] | 1033 | memory_.SetMemory(0x608, std::vector<uint8_t>{1, '\0', 4, 4, 1}); |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1034 | |
| 1035 | // FDE 32. |
| 1036 | memory_.SetData32(0x700, 0xfc); |
| 1037 | memory_.SetData32(0x704, 0x104); |
| 1038 | memory_.SetData32(0x708, 0x20f8); |
| 1039 | memory_.SetData32(0x70c, 0x200); |
| 1040 | |
| 1041 | uint64_t load_bias = 0; |
| 1042 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 1043 | elf->InitHeaders(0); |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1044 | EXPECT_EQ(0U, load_bias); |
| 1045 | EXPECT_FALSE(elf->IsValidPc(0)); |
| 1046 | EXPECT_FALSE(elf->IsValidPc(0x27ff)); |
| 1047 | EXPECT_TRUE(elf->IsValidPc(0x2800)); |
| 1048 | EXPECT_TRUE(elf->IsValidPc(0x2900)); |
| 1049 | EXPECT_TRUE(elf->IsValidPc(0x29ff)); |
| 1050 | EXPECT_FALSE(elf->IsValidPc(0x2a00)); |
| 1051 | } |
| 1052 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 1053 | } // namespace unwindstack |