| 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 |  | 
|  | 28 | #include "MemoryFake.h" | 
|  | 29 |  | 
|  | 30 | #if !defined(PT_ARM_EXIDX) | 
|  | 31 | #define PT_ARM_EXIDX 0x70000001 | 
|  | 32 | #endif | 
|  | 33 |  | 
|  | 34 | #if !defined(EM_AARCH64) | 
|  | 35 | #define EM_AARCH64 183 | 
|  | 36 | #endif | 
|  | 37 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 38 | namespace unwindstack { | 
|  | 39 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 40 | class ElfInterfaceTest : public ::testing::Test { | 
|  | 41 | protected: | 
|  | 42 | void SetUp() override { | 
|  | 43 | memory_.Clear(); | 
|  | 44 | } | 
|  | 45 |  | 
|  | 46 | void SetStringMemory(uint64_t offset, const char* string) { | 
|  | 47 | memory_.SetMemory(offset, string, strlen(string) + 1); | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> | 
|  | 51 | void SinglePtLoad(); | 
|  | 52 |  | 
|  | 53 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> | 
|  | 54 | void MultipleExecutablePtLoads(); | 
|  | 55 |  | 
|  | 56 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> | 
|  | 57 | void MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr(); | 
|  | 58 |  | 
|  | 59 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> | 
|  | 60 | void NonExecutablePtLoads(); | 
|  | 61 |  | 
|  | 62 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> | 
|  | 63 | void ManyPhdrs(); | 
|  | 64 |  | 
|  | 65 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> | 
|  | 66 | void Soname(); | 
|  | 67 |  | 
|  | 68 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> | 
|  | 69 | void SonameAfterDtNull(); | 
|  | 70 |  | 
|  | 71 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> | 
|  | 72 | void SonameSize(); | 
|  | 73 |  | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 74 | template <typename ElfType> | 
|  | 75 | void InitHeadersEhFrameTest(); | 
|  | 76 |  | 
|  | 77 | template <typename ElfType> | 
|  | 78 | void InitHeadersDebugFrame(); | 
|  | 79 |  | 
|  | 80 | template <typename ElfType> | 
|  | 81 | void InitHeadersEhFrameFail(); | 
|  | 82 |  | 
|  | 83 | template <typename ElfType> | 
|  | 84 | void InitHeadersDebugFrameFail(); | 
|  | 85 |  | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 86 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> | 
|  | 87 | void InitSectionHeadersMalformed(); | 
|  | 88 |  | 
|  | 89 | template <typename Ehdr, typename Shdr, typename Sym, typename ElfInterfaceType> | 
|  | 90 | void InitSectionHeaders(uint64_t entry_size); | 
|  | 91 |  | 
|  | 92 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> | 
|  | 93 | void InitSectionHeadersOffsets(); | 
|  | 94 |  | 
|  | 95 | template <typename Sym> | 
|  | 96 | void InitSym(uint64_t offset, uint32_t value, uint32_t size, uint32_t name_offset, | 
|  | 97 | uint64_t sym_offset, const char* name); | 
|  | 98 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 99 | MemoryFake memory_; | 
|  | 100 | }; | 
|  | 101 |  | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 102 | template <typename Sym> | 
|  | 103 | void ElfInterfaceTest::InitSym(uint64_t offset, uint32_t value, uint32_t size, uint32_t name_offset, | 
|  | 104 | uint64_t sym_offset, const char* name) { | 
|  | 105 | Sym sym; | 
|  | 106 | memset(&sym, 0, sizeof(sym)); | 
|  | 107 | sym.st_info = STT_FUNC; | 
|  | 108 | sym.st_value = value; | 
|  | 109 | sym.st_size = size; | 
|  | 110 | sym.st_name = name_offset; | 
|  | 111 | sym.st_shndx = SHN_COMMON; | 
|  | 112 |  | 
|  | 113 | memory_.SetMemory(offset, &sym, sizeof(sym)); | 
|  | 114 | memory_.SetMemory(sym_offset + name_offset, name, strlen(name) + 1); | 
|  | 115 | } | 
|  | 116 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 117 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> | 
|  | 118 | void ElfInterfaceTest::SinglePtLoad() { | 
|  | 119 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); | 
|  | 120 |  | 
|  | 121 | Ehdr ehdr; | 
|  | 122 | memset(&ehdr, 0, sizeof(ehdr)); | 
|  | 123 | ehdr.e_phoff = 0x100; | 
|  | 124 | ehdr.e_phnum = 1; | 
|  | 125 | ehdr.e_phentsize = sizeof(Phdr); | 
|  | 126 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); | 
|  | 127 |  | 
|  | 128 | Phdr phdr; | 
|  | 129 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 130 | phdr.p_type = PT_LOAD; | 
|  | 131 | phdr.p_vaddr = 0x2000; | 
|  | 132 | phdr.p_memsz = 0x10000; | 
|  | 133 | phdr.p_flags = PF_R | PF_X; | 
|  | 134 | phdr.p_align = 0x1000; | 
|  | 135 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); | 
|  | 136 |  | 
|  | 137 | ASSERT_TRUE(elf->Init()); | 
|  | 138 |  | 
|  | 139 | const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads(); | 
|  | 140 | ASSERT_EQ(1U, pt_loads.size()); | 
|  | 141 | LoadInfo load_data = pt_loads.at(0); | 
|  | 142 | ASSERT_EQ(0U, load_data.offset); | 
|  | 143 | ASSERT_EQ(0x2000U, load_data.table_offset); | 
|  | 144 | ASSERT_EQ(0x10000U, load_data.table_size); | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | TEST_F(ElfInterfaceTest, elf32_single_pt_load) { | 
|  | 148 | SinglePtLoad<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | TEST_F(ElfInterfaceTest, elf64_single_pt_load) { | 
|  | 152 | SinglePtLoad<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> | 
|  | 156 | void ElfInterfaceTest::MultipleExecutablePtLoads() { | 
|  | 157 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); | 
|  | 158 |  | 
|  | 159 | Ehdr ehdr; | 
|  | 160 | memset(&ehdr, 0, sizeof(ehdr)); | 
|  | 161 | ehdr.e_phoff = 0x100; | 
|  | 162 | ehdr.e_phnum = 3; | 
|  | 163 | ehdr.e_phentsize = sizeof(Phdr); | 
|  | 164 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); | 
|  | 165 |  | 
|  | 166 | Phdr phdr; | 
|  | 167 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 168 | phdr.p_type = PT_LOAD; | 
|  | 169 | phdr.p_vaddr = 0x2000; | 
|  | 170 | phdr.p_memsz = 0x10000; | 
|  | 171 | phdr.p_flags = PF_R | PF_X; | 
|  | 172 | phdr.p_align = 0x1000; | 
|  | 173 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); | 
|  | 174 |  | 
|  | 175 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 176 | phdr.p_type = PT_LOAD; | 
|  | 177 | phdr.p_offset = 0x1000; | 
|  | 178 | phdr.p_vaddr = 0x2001; | 
|  | 179 | phdr.p_memsz = 0x10001; | 
|  | 180 | phdr.p_flags = PF_R | PF_X; | 
|  | 181 | phdr.p_align = 0x1001; | 
|  | 182 | memory_.SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr)); | 
|  | 183 |  | 
|  | 184 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 185 | phdr.p_type = PT_LOAD; | 
|  | 186 | phdr.p_offset = 0x2000; | 
|  | 187 | phdr.p_vaddr = 0x2002; | 
|  | 188 | phdr.p_memsz = 0x10002; | 
|  | 189 | phdr.p_flags = PF_R | PF_X; | 
|  | 190 | phdr.p_align = 0x1002; | 
|  | 191 | memory_.SetMemory(0x100 + 2 * sizeof(phdr), &phdr, sizeof(phdr)); | 
|  | 192 |  | 
|  | 193 | ASSERT_TRUE(elf->Init()); | 
|  | 194 |  | 
|  | 195 | const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads(); | 
|  | 196 | ASSERT_EQ(3U, pt_loads.size()); | 
|  | 197 |  | 
|  | 198 | LoadInfo load_data = pt_loads.at(0); | 
|  | 199 | ASSERT_EQ(0U, load_data.offset); | 
|  | 200 | ASSERT_EQ(0x2000U, load_data.table_offset); | 
|  | 201 | ASSERT_EQ(0x10000U, load_data.table_size); | 
|  | 202 |  | 
|  | 203 | load_data = pt_loads.at(0x1000); | 
|  | 204 | ASSERT_EQ(0x1000U, load_data.offset); | 
|  | 205 | ASSERT_EQ(0x2001U, load_data.table_offset); | 
|  | 206 | ASSERT_EQ(0x10001U, load_data.table_size); | 
|  | 207 |  | 
|  | 208 | load_data = pt_loads.at(0x2000); | 
|  | 209 | ASSERT_EQ(0x2000U, load_data.offset); | 
|  | 210 | ASSERT_EQ(0x2002U, load_data.table_offset); | 
|  | 211 | ASSERT_EQ(0x10002U, load_data.table_size); | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | TEST_F(ElfInterfaceTest, elf32_multiple_executable_pt_loads) { | 
|  | 215 | MultipleExecutablePtLoads<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); | 
|  | 216 | } | 
|  | 217 |  | 
|  | 218 | TEST_F(ElfInterfaceTest, elf64_multiple_executable_pt_loads) { | 
|  | 219 | MultipleExecutablePtLoads<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); | 
|  | 220 | } | 
|  | 221 |  | 
|  | 222 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> | 
|  | 223 | void ElfInterfaceTest::MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr() { | 
|  | 224 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); | 
|  | 225 |  | 
|  | 226 | Ehdr ehdr; | 
|  | 227 | memset(&ehdr, 0, sizeof(ehdr)); | 
|  | 228 | ehdr.e_phoff = 0x100; | 
|  | 229 | ehdr.e_phnum = 3; | 
|  | 230 | ehdr.e_phentsize = sizeof(Phdr) + 100; | 
|  | 231 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); | 
|  | 232 |  | 
|  | 233 | Phdr phdr; | 
|  | 234 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 235 | phdr.p_type = PT_LOAD; | 
|  | 236 | phdr.p_vaddr = 0x2000; | 
|  | 237 | phdr.p_memsz = 0x10000; | 
|  | 238 | phdr.p_flags = PF_R | PF_X; | 
|  | 239 | phdr.p_align = 0x1000; | 
|  | 240 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); | 
|  | 241 |  | 
|  | 242 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 243 | phdr.p_type = PT_LOAD; | 
|  | 244 | phdr.p_offset = 0x1000; | 
|  | 245 | phdr.p_vaddr = 0x2001; | 
|  | 246 | phdr.p_memsz = 0x10001; | 
|  | 247 | phdr.p_flags = PF_R | PF_X; | 
|  | 248 | phdr.p_align = 0x1001; | 
|  | 249 | memory_.SetMemory(0x100 + sizeof(phdr) + 100, &phdr, sizeof(phdr)); | 
|  | 250 |  | 
|  | 251 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 252 | phdr.p_type = PT_LOAD; | 
|  | 253 | phdr.p_offset = 0x2000; | 
|  | 254 | phdr.p_vaddr = 0x2002; | 
|  | 255 | phdr.p_memsz = 0x10002; | 
|  | 256 | phdr.p_flags = PF_R | PF_X; | 
|  | 257 | phdr.p_align = 0x1002; | 
|  | 258 | memory_.SetMemory(0x100 + 2 * (sizeof(phdr) + 100), &phdr, sizeof(phdr)); | 
|  | 259 |  | 
|  | 260 | ASSERT_TRUE(elf->Init()); | 
|  | 261 |  | 
|  | 262 | const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads(); | 
|  | 263 | ASSERT_EQ(3U, pt_loads.size()); | 
|  | 264 |  | 
|  | 265 | LoadInfo load_data = pt_loads.at(0); | 
|  | 266 | ASSERT_EQ(0U, load_data.offset); | 
|  | 267 | ASSERT_EQ(0x2000U, load_data.table_offset); | 
|  | 268 | ASSERT_EQ(0x10000U, load_data.table_size); | 
|  | 269 |  | 
|  | 270 | load_data = pt_loads.at(0x1000); | 
|  | 271 | ASSERT_EQ(0x1000U, load_data.offset); | 
|  | 272 | ASSERT_EQ(0x2001U, load_data.table_offset); | 
|  | 273 | ASSERT_EQ(0x10001U, load_data.table_size); | 
|  | 274 |  | 
|  | 275 | load_data = pt_loads.at(0x2000); | 
|  | 276 | ASSERT_EQ(0x2000U, load_data.offset); | 
|  | 277 | ASSERT_EQ(0x2002U, load_data.table_offset); | 
|  | 278 | ASSERT_EQ(0x10002U, load_data.table_size); | 
|  | 279 | } | 
|  | 280 |  | 
|  | 281 | TEST_F(ElfInterfaceTest, elf32_multiple_executable_pt_loads_increments_not_size_of_phdr) { | 
|  | 282 | MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, | 
|  | 283 | ElfInterface32>(); | 
|  | 284 | } | 
|  | 285 |  | 
|  | 286 | TEST_F(ElfInterfaceTest, elf64_multiple_executable_pt_loads_increments_not_size_of_phdr) { | 
|  | 287 | MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, | 
|  | 288 | ElfInterface64>(); | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> | 
|  | 292 | void ElfInterfaceTest::NonExecutablePtLoads() { | 
|  | 293 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); | 
|  | 294 |  | 
|  | 295 | Ehdr ehdr; | 
|  | 296 | memset(&ehdr, 0, sizeof(ehdr)); | 
|  | 297 | ehdr.e_phoff = 0x100; | 
|  | 298 | ehdr.e_phnum = 3; | 
|  | 299 | ehdr.e_phentsize = sizeof(Phdr); | 
|  | 300 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); | 
|  | 301 |  | 
|  | 302 | Phdr phdr; | 
|  | 303 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 304 | phdr.p_type = PT_LOAD; | 
|  | 305 | phdr.p_vaddr = 0x2000; | 
|  | 306 | phdr.p_memsz = 0x10000; | 
|  | 307 | phdr.p_flags = PF_R; | 
|  | 308 | phdr.p_align = 0x1000; | 
|  | 309 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); | 
|  | 310 |  | 
|  | 311 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 312 | phdr.p_type = PT_LOAD; | 
|  | 313 | phdr.p_offset = 0x1000; | 
|  | 314 | phdr.p_vaddr = 0x2001; | 
|  | 315 | phdr.p_memsz = 0x10001; | 
|  | 316 | phdr.p_flags = PF_R | PF_X; | 
|  | 317 | phdr.p_align = 0x1001; | 
|  | 318 | memory_.SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr)); | 
|  | 319 |  | 
|  | 320 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 321 | phdr.p_type = PT_LOAD; | 
|  | 322 | phdr.p_offset = 0x2000; | 
|  | 323 | phdr.p_vaddr = 0x2002; | 
|  | 324 | phdr.p_memsz = 0x10002; | 
|  | 325 | phdr.p_flags = PF_R; | 
|  | 326 | phdr.p_align = 0x1002; | 
|  | 327 | memory_.SetMemory(0x100 + 2 * sizeof(phdr), &phdr, sizeof(phdr)); | 
|  | 328 |  | 
|  | 329 | ASSERT_TRUE(elf->Init()); | 
|  | 330 |  | 
|  | 331 | const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads(); | 
|  | 332 | ASSERT_EQ(1U, pt_loads.size()); | 
|  | 333 |  | 
|  | 334 | LoadInfo load_data = pt_loads.at(0x1000); | 
|  | 335 | ASSERT_EQ(0x1000U, load_data.offset); | 
|  | 336 | ASSERT_EQ(0x2001U, load_data.table_offset); | 
|  | 337 | ASSERT_EQ(0x10001U, load_data.table_size); | 
|  | 338 | } | 
|  | 339 |  | 
|  | 340 | TEST_F(ElfInterfaceTest, elf32_non_executable_pt_loads) { | 
|  | 341 | NonExecutablePtLoads<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); | 
|  | 342 | } | 
|  | 343 |  | 
|  | 344 | TEST_F(ElfInterfaceTest, elf64_non_executable_pt_loads) { | 
|  | 345 | NonExecutablePtLoads<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); | 
|  | 346 | } | 
|  | 347 |  | 
|  | 348 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> | 
|  | 349 | void ElfInterfaceTest::ManyPhdrs() { | 
|  | 350 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); | 
|  | 351 |  | 
|  | 352 | Ehdr ehdr; | 
|  | 353 | memset(&ehdr, 0, sizeof(ehdr)); | 
|  | 354 | ehdr.e_phoff = 0x100; | 
|  | 355 | ehdr.e_phnum = 7; | 
|  | 356 | ehdr.e_phentsize = sizeof(Phdr); | 
|  | 357 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); | 
|  | 358 |  | 
|  | 359 | Phdr phdr; | 
|  | 360 | uint64_t phdr_offset = 0x100; | 
|  | 361 |  | 
|  | 362 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 363 | phdr.p_type = PT_LOAD; | 
|  | 364 | phdr.p_vaddr = 0x2000; | 
|  | 365 | phdr.p_memsz = 0x10000; | 
|  | 366 | phdr.p_flags = PF_R | PF_X; | 
|  | 367 | phdr.p_align = 0x1000; | 
|  | 368 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); | 
|  | 369 | phdr_offset += sizeof(phdr); | 
|  | 370 |  | 
|  | 371 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 372 | phdr.p_type = PT_GNU_EH_FRAME; | 
|  | 373 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); | 
|  | 374 | phdr_offset += sizeof(phdr); | 
|  | 375 |  | 
|  | 376 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 377 | phdr.p_type = PT_DYNAMIC; | 
|  | 378 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); | 
|  | 379 | phdr_offset += sizeof(phdr); | 
|  | 380 |  | 
|  | 381 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 382 | phdr.p_type = PT_INTERP; | 
|  | 383 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); | 
|  | 384 | phdr_offset += sizeof(phdr); | 
|  | 385 |  | 
|  | 386 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 387 | phdr.p_type = PT_NOTE; | 
|  | 388 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); | 
|  | 389 | phdr_offset += sizeof(phdr); | 
|  | 390 |  | 
|  | 391 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 392 | phdr.p_type = PT_SHLIB; | 
|  | 393 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); | 
|  | 394 | phdr_offset += sizeof(phdr); | 
|  | 395 |  | 
|  | 396 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 397 | phdr.p_type = PT_GNU_EH_FRAME; | 
|  | 398 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); | 
|  | 399 | phdr_offset += sizeof(phdr); | 
|  | 400 |  | 
|  | 401 | ASSERT_TRUE(elf->Init()); | 
|  | 402 |  | 
|  | 403 | const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads(); | 
|  | 404 | ASSERT_EQ(1U, pt_loads.size()); | 
|  | 405 |  | 
|  | 406 | LoadInfo load_data = pt_loads.at(0); | 
|  | 407 | ASSERT_EQ(0U, load_data.offset); | 
|  | 408 | ASSERT_EQ(0x2000U, load_data.table_offset); | 
|  | 409 | ASSERT_EQ(0x10000U, load_data.table_size); | 
|  | 410 | } | 
|  | 411 |  | 
|  | 412 | TEST_F(ElfInterfaceTest, elf32_many_phdrs) { | 
|  | 413 | ElfInterfaceTest::ManyPhdrs<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); | 
|  | 414 | } | 
|  | 415 |  | 
|  | 416 | TEST_F(ElfInterfaceTest, elf64_many_phdrs) { | 
|  | 417 | ElfInterfaceTest::ManyPhdrs<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); | 
|  | 418 | } | 
|  | 419 |  | 
|  | 420 | TEST_F(ElfInterfaceTest, elf32_arm) { | 
|  | 421 | ElfInterfaceArm elf_arm(&memory_); | 
|  | 422 |  | 
|  | 423 | Elf32_Ehdr ehdr; | 
|  | 424 | memset(&ehdr, 0, sizeof(ehdr)); | 
|  | 425 | ehdr.e_phoff = 0x100; | 
|  | 426 | ehdr.e_phnum = 1; | 
|  | 427 | ehdr.e_phentsize = sizeof(Elf32_Phdr); | 
|  | 428 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); | 
|  | 429 |  | 
|  | 430 | Elf32_Phdr phdr; | 
|  | 431 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 432 | phdr.p_type = PT_ARM_EXIDX; | 
|  | 433 | phdr.p_vaddr = 0x2000; | 
|  | 434 | phdr.p_memsz = 16; | 
|  | 435 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); | 
|  | 436 |  | 
|  | 437 | // Add arm exidx entries. | 
|  | 438 | memory_.SetData32(0x2000, 0x1000); | 
|  | 439 | memory_.SetData32(0x2008, 0x1000); | 
|  | 440 |  | 
|  | 441 | ASSERT_TRUE(elf_arm.Init()); | 
|  | 442 |  | 
|  | 443 | std::vector<uint32_t> entries; | 
|  | 444 | for (auto addr : elf_arm) { | 
|  | 445 | entries.push_back(addr); | 
|  | 446 | } | 
|  | 447 | ASSERT_EQ(2U, entries.size()); | 
|  | 448 | ASSERT_EQ(0x3000U, entries[0]); | 
|  | 449 | ASSERT_EQ(0x3008U, entries[1]); | 
|  | 450 |  | 
|  | 451 | ASSERT_EQ(0x2000U, elf_arm.start_offset()); | 
|  | 452 | ASSERT_EQ(2U, elf_arm.total_entries()); | 
|  | 453 | } | 
|  | 454 |  | 
|  | 455 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> | 
|  | 456 | void ElfInterfaceTest::Soname() { | 
|  | 457 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); | 
|  | 458 |  | 
|  | 459 | Ehdr ehdr; | 
|  | 460 | memset(&ehdr, 0, sizeof(ehdr)); | 
|  | 461 | ehdr.e_phoff = 0x100; | 
|  | 462 | ehdr.e_phnum = 1; | 
|  | 463 | ehdr.e_phentsize = sizeof(Phdr); | 
|  | 464 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); | 
|  | 465 |  | 
|  | 466 | Phdr phdr; | 
|  | 467 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 468 | phdr.p_type = PT_DYNAMIC; | 
|  | 469 | phdr.p_offset = 0x2000; | 
|  | 470 | phdr.p_memsz = sizeof(Dyn) * 3; | 
|  | 471 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); | 
|  | 472 |  | 
|  | 473 | uint64_t offset = 0x2000; | 
|  | 474 | Dyn dyn; | 
|  | 475 |  | 
|  | 476 | dyn.d_tag = DT_STRTAB; | 
|  | 477 | dyn.d_un.d_ptr = 0x10000; | 
|  | 478 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); | 
|  | 479 | offset += sizeof(dyn); | 
|  | 480 |  | 
|  | 481 | dyn.d_tag = DT_STRSZ; | 
|  | 482 | dyn.d_un.d_val = 0x1000; | 
|  | 483 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); | 
|  | 484 | offset += sizeof(dyn); | 
|  | 485 |  | 
|  | 486 | dyn.d_tag = DT_SONAME; | 
|  | 487 | dyn.d_un.d_val = 0x10; | 
|  | 488 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); | 
|  | 489 | offset += sizeof(dyn); | 
|  | 490 |  | 
|  | 491 | dyn.d_tag = DT_NULL; | 
|  | 492 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); | 
|  | 493 |  | 
|  | 494 | SetStringMemory(0x10010, "fake_soname.so"); | 
|  | 495 |  | 
|  | 496 | ASSERT_TRUE(elf->Init()); | 
|  | 497 | std::string name; | 
|  | 498 | ASSERT_TRUE(elf->GetSoname(&name)); | 
|  | 499 | ASSERT_STREQ("fake_soname.so", name.c_str()); | 
|  | 500 | } | 
|  | 501 |  | 
|  | 502 | TEST_F(ElfInterfaceTest, elf32_soname) { | 
|  | 503 | Soname<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); | 
|  | 504 | } | 
|  | 505 |  | 
|  | 506 | TEST_F(ElfInterfaceTest, elf64_soname) { | 
|  | 507 | Soname<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); | 
|  | 508 | } | 
|  | 509 |  | 
|  | 510 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> | 
|  | 511 | void ElfInterfaceTest::SonameAfterDtNull() { | 
|  | 512 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); | 
|  | 513 |  | 
|  | 514 | Ehdr ehdr; | 
|  | 515 | memset(&ehdr, 0, sizeof(ehdr)); | 
|  | 516 | ehdr.e_phoff = 0x100; | 
|  | 517 | ehdr.e_phnum = 1; | 
|  | 518 | ehdr.e_phentsize = sizeof(Phdr); | 
|  | 519 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); | 
|  | 520 |  | 
|  | 521 | Phdr phdr; | 
|  | 522 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 523 | phdr.p_type = PT_DYNAMIC; | 
|  | 524 | phdr.p_offset = 0x2000; | 
|  | 525 | phdr.p_memsz = sizeof(Dyn) * 3; | 
|  | 526 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); | 
|  | 527 |  | 
|  | 528 | Dyn dyn; | 
|  | 529 | uint64_t offset = 0x2000; | 
|  | 530 |  | 
|  | 531 | dyn.d_tag = DT_STRTAB; | 
|  | 532 | dyn.d_un.d_ptr = 0x10000; | 
|  | 533 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); | 
|  | 534 | offset += sizeof(dyn); | 
|  | 535 |  | 
|  | 536 | dyn.d_tag = DT_STRSZ; | 
|  | 537 | dyn.d_un.d_val = 0x1000; | 
|  | 538 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); | 
|  | 539 | offset += sizeof(dyn); | 
|  | 540 |  | 
|  | 541 | dyn.d_tag = DT_NULL; | 
|  | 542 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); | 
|  | 543 | offset += sizeof(dyn); | 
|  | 544 |  | 
|  | 545 | dyn.d_tag = DT_SONAME; | 
|  | 546 | dyn.d_un.d_val = 0x10; | 
|  | 547 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); | 
|  | 548 | offset += sizeof(dyn); | 
|  | 549 |  | 
|  | 550 | SetStringMemory(0x10010, "fake_soname.so"); | 
|  | 551 |  | 
|  | 552 | ASSERT_TRUE(elf->Init()); | 
|  | 553 | std::string name; | 
|  | 554 | ASSERT_FALSE(elf->GetSoname(&name)); | 
|  | 555 | } | 
|  | 556 |  | 
|  | 557 | TEST_F(ElfInterfaceTest, elf32_soname_after_dt_null) { | 
|  | 558 | SonameAfterDtNull<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); | 
|  | 559 | } | 
|  | 560 |  | 
|  | 561 | TEST_F(ElfInterfaceTest, elf64_soname_after_dt_null) { | 
|  | 562 | SonameAfterDtNull<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); | 
|  | 563 | } | 
|  | 564 |  | 
|  | 565 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> | 
|  | 566 | void ElfInterfaceTest::SonameSize() { | 
|  | 567 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); | 
|  | 568 |  | 
|  | 569 | Ehdr ehdr; | 
|  | 570 | memset(&ehdr, 0, sizeof(ehdr)); | 
|  | 571 | ehdr.e_phoff = 0x100; | 
|  | 572 | ehdr.e_phnum = 1; | 
|  | 573 | ehdr.e_phentsize = sizeof(Phdr); | 
|  | 574 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); | 
|  | 575 |  | 
|  | 576 | Phdr phdr; | 
|  | 577 | memset(&phdr, 0, sizeof(phdr)); | 
|  | 578 | phdr.p_type = PT_DYNAMIC; | 
|  | 579 | phdr.p_offset = 0x2000; | 
|  | 580 | phdr.p_memsz = sizeof(Dyn); | 
|  | 581 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); | 
|  | 582 |  | 
|  | 583 | Dyn dyn; | 
|  | 584 | uint64_t offset = 0x2000; | 
|  | 585 |  | 
|  | 586 | dyn.d_tag = DT_STRTAB; | 
|  | 587 | dyn.d_un.d_ptr = 0x10000; | 
|  | 588 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); | 
|  | 589 | offset += sizeof(dyn); | 
|  | 590 |  | 
|  | 591 | dyn.d_tag = DT_STRSZ; | 
|  | 592 | dyn.d_un.d_val = 0x10; | 
|  | 593 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); | 
|  | 594 | offset += sizeof(dyn); | 
|  | 595 |  | 
|  | 596 | dyn.d_tag = DT_SONAME; | 
|  | 597 | dyn.d_un.d_val = 0x10; | 
|  | 598 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); | 
|  | 599 | offset += sizeof(dyn); | 
|  | 600 |  | 
|  | 601 | dyn.d_tag = DT_NULL; | 
|  | 602 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); | 
|  | 603 |  | 
|  | 604 | SetStringMemory(0x10010, "fake_soname.so"); | 
|  | 605 |  | 
|  | 606 | ASSERT_TRUE(elf->Init()); | 
|  | 607 | std::string name; | 
|  | 608 | ASSERT_FALSE(elf->GetSoname(&name)); | 
|  | 609 | } | 
|  | 610 |  | 
|  | 611 | TEST_F(ElfInterfaceTest, elf32_soname_size) { | 
|  | 612 | SonameSize<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); | 
|  | 613 | } | 
|  | 614 |  | 
|  | 615 | TEST_F(ElfInterfaceTest, elf64_soname_size) { | 
|  | 616 | SonameSize<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); | 
|  | 617 | } | 
| Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 618 |  | 
|  | 619 | class MockElfInterface32 : public ElfInterface32 { | 
|  | 620 | public: | 
|  | 621 | MockElfInterface32(Memory* memory) : ElfInterface32(memory) {} | 
|  | 622 | virtual ~MockElfInterface32() = default; | 
|  | 623 |  | 
|  | 624 | void TestSetEhFrameOffset(uint64_t offset) { eh_frame_offset_ = offset; } | 
|  | 625 | void TestSetEhFrameSize(uint64_t size) { eh_frame_size_ = size; } | 
|  | 626 |  | 
|  | 627 | void TestSetDebugFrameOffset(uint64_t offset) { debug_frame_offset_ = offset; } | 
|  | 628 | void TestSetDebugFrameSize(uint64_t size) { debug_frame_size_ = size; } | 
|  | 629 | }; | 
|  | 630 |  | 
|  | 631 | class MockElfInterface64 : public ElfInterface64 { | 
|  | 632 | public: | 
|  | 633 | MockElfInterface64(Memory* memory) : ElfInterface64(memory) {} | 
|  | 634 | virtual ~MockElfInterface64() = default; | 
|  | 635 |  | 
|  | 636 | void TestSetEhFrameOffset(uint64_t offset) { eh_frame_offset_ = offset; } | 
|  | 637 | void TestSetEhFrameSize(uint64_t size) { eh_frame_size_ = size; } | 
|  | 638 |  | 
|  | 639 | void TestSetDebugFrameOffset(uint64_t offset) { debug_frame_offset_ = offset; } | 
|  | 640 | void TestSetDebugFrameSize(uint64_t size) { debug_frame_size_ = size; } | 
|  | 641 | }; | 
|  | 642 |  | 
|  | 643 | template <typename ElfType> | 
|  | 644 | void ElfInterfaceTest::InitHeadersEhFrameTest() { | 
|  | 645 | ElfType elf(&memory_); | 
|  | 646 |  | 
|  | 647 | elf.TestSetEhFrameOffset(0x10000); | 
|  | 648 | elf.TestSetEhFrameSize(0); | 
|  | 649 | elf.TestSetDebugFrameOffset(0); | 
|  | 650 | elf.TestSetDebugFrameSize(0); | 
|  | 651 |  | 
|  | 652 | memory_.SetMemory(0x10000, | 
|  | 653 | std::vector<uint8_t>{0x1, DW_EH_PE_udata2, DW_EH_PE_udata2, DW_EH_PE_udata2}); | 
|  | 654 | memory_.SetData32(0x10004, 0x500); | 
|  | 655 | memory_.SetData32(0x10008, 250); | 
|  | 656 |  | 
|  | 657 | elf.InitHeaders(); | 
|  | 658 |  | 
|  | 659 | EXPECT_FALSE(elf.eh_frame() == nullptr); | 
|  | 660 | EXPECT_TRUE(elf.debug_frame() == nullptr); | 
|  | 661 | } | 
|  | 662 |  | 
|  | 663 | TEST_F(ElfInterfaceTest, init_headers_eh_frame32) { | 
|  | 664 | InitHeadersEhFrameTest<MockElfInterface32>(); | 
|  | 665 | } | 
|  | 666 |  | 
|  | 667 | TEST_F(ElfInterfaceTest, init_headers_eh_frame64) { | 
|  | 668 | InitHeadersEhFrameTest<MockElfInterface64>(); | 
|  | 669 | } | 
|  | 670 |  | 
|  | 671 | template <typename ElfType> | 
|  | 672 | void ElfInterfaceTest::InitHeadersDebugFrame() { | 
|  | 673 | ElfType elf(&memory_); | 
|  | 674 |  | 
|  | 675 | elf.TestSetEhFrameOffset(0); | 
|  | 676 | elf.TestSetEhFrameSize(0); | 
|  | 677 | elf.TestSetDebugFrameOffset(0x5000); | 
|  | 678 | elf.TestSetDebugFrameSize(0x200); | 
|  | 679 |  | 
|  | 680 | memory_.SetData32(0x5000, 0xfc); | 
|  | 681 | memory_.SetData32(0x5004, 0xffffffff); | 
|  | 682 | memory_.SetData8(0x5008, 1); | 
|  | 683 | memory_.SetData8(0x5009, '\0'); | 
|  | 684 |  | 
|  | 685 | memory_.SetData32(0x5100, 0xfc); | 
|  | 686 | memory_.SetData32(0x5104, 0); | 
|  | 687 | memory_.SetData32(0x5108, 0x1500); | 
|  | 688 | memory_.SetData32(0x510c, 0x200); | 
|  | 689 |  | 
|  | 690 | elf.InitHeaders(); | 
|  | 691 |  | 
|  | 692 | EXPECT_TRUE(elf.eh_frame() == nullptr); | 
|  | 693 | EXPECT_FALSE(elf.debug_frame() == nullptr); | 
|  | 694 | } | 
|  | 695 |  | 
|  | 696 | TEST_F(ElfInterfaceTest, init_headers_debug_frame32) { | 
|  | 697 | InitHeadersDebugFrame<MockElfInterface32>(); | 
|  | 698 | } | 
|  | 699 |  | 
|  | 700 | TEST_F(ElfInterfaceTest, init_headers_debug_frame64) { | 
|  | 701 | InitHeadersDebugFrame<MockElfInterface64>(); | 
|  | 702 | } | 
|  | 703 |  | 
|  | 704 | template <typename ElfType> | 
|  | 705 | void ElfInterfaceTest::InitHeadersEhFrameFail() { | 
|  | 706 | ElfType elf(&memory_); | 
|  | 707 |  | 
|  | 708 | elf.TestSetEhFrameOffset(0x1000); | 
|  | 709 | elf.TestSetEhFrameSize(0x100); | 
|  | 710 | elf.TestSetDebugFrameOffset(0); | 
|  | 711 | elf.TestSetDebugFrameSize(0); | 
|  | 712 |  | 
|  | 713 | elf.InitHeaders(); | 
|  | 714 |  | 
|  | 715 | EXPECT_TRUE(elf.eh_frame() == nullptr); | 
|  | 716 | EXPECT_EQ(0U, elf.eh_frame_offset()); | 
|  | 717 | EXPECT_EQ(static_cast<uint64_t>(-1), elf.eh_frame_size()); | 
|  | 718 | EXPECT_TRUE(elf.debug_frame() == nullptr); | 
|  | 719 | } | 
|  | 720 |  | 
|  | 721 | TEST_F(ElfInterfaceTest, init_headers_eh_frame32_fail) { | 
|  | 722 | InitHeadersEhFrameFail<MockElfInterface32>(); | 
|  | 723 | } | 
|  | 724 |  | 
|  | 725 | TEST_F(ElfInterfaceTest, init_headers_eh_frame64_fail) { | 
|  | 726 | InitHeadersEhFrameFail<MockElfInterface64>(); | 
|  | 727 | } | 
|  | 728 |  | 
|  | 729 | template <typename ElfType> | 
|  | 730 | void ElfInterfaceTest::InitHeadersDebugFrameFail() { | 
|  | 731 | ElfType elf(&memory_); | 
|  | 732 |  | 
|  | 733 | elf.TestSetEhFrameOffset(0); | 
|  | 734 | elf.TestSetEhFrameSize(0); | 
|  | 735 | elf.TestSetDebugFrameOffset(0x1000); | 
|  | 736 | elf.TestSetDebugFrameSize(0x100); | 
|  | 737 |  | 
|  | 738 | elf.InitHeaders(); | 
|  | 739 |  | 
|  | 740 | EXPECT_TRUE(elf.eh_frame() == nullptr); | 
|  | 741 | EXPECT_TRUE(elf.debug_frame() == nullptr); | 
|  | 742 | EXPECT_EQ(0U, elf.debug_frame_offset()); | 
|  | 743 | EXPECT_EQ(static_cast<uint64_t>(-1), elf.debug_frame_size()); | 
|  | 744 | } | 
|  | 745 |  | 
|  | 746 | TEST_F(ElfInterfaceTest, init_headers_debug_frame32_fail) { | 
|  | 747 | InitHeadersDebugFrameFail<MockElfInterface32>(); | 
|  | 748 | } | 
|  | 749 |  | 
|  | 750 | TEST_F(ElfInterfaceTest, init_headers_debug_frame64_fail) { | 
|  | 751 | InitHeadersDebugFrameFail<MockElfInterface64>(); | 
|  | 752 | } | 
| Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 753 |  | 
|  | 754 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> | 
|  | 755 | void ElfInterfaceTest::InitSectionHeadersMalformed() { | 
|  | 756 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); | 
|  | 757 |  | 
|  | 758 | Ehdr ehdr; | 
|  | 759 | memset(&ehdr, 0, sizeof(ehdr)); | 
|  | 760 | ehdr.e_shoff = 0x1000; | 
|  | 761 | ehdr.e_shnum = 10; | 
|  | 762 | ehdr.e_shentsize = sizeof(Shdr); | 
|  | 763 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); | 
|  | 764 |  | 
|  | 765 | ASSERT_TRUE(elf->Init()); | 
|  | 766 | } | 
|  | 767 |  | 
|  | 768 | TEST_F(ElfInterfaceTest, init_section_headers_malformed32) { | 
|  | 769 | InitSectionHeadersMalformed<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(); | 
|  | 770 | } | 
|  | 771 |  | 
|  | 772 | TEST_F(ElfInterfaceTest, init_section_headers_malformed64) { | 
|  | 773 | InitSectionHeadersMalformed<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(); | 
|  | 774 | } | 
|  | 775 |  | 
|  | 776 | template <typename Ehdr, typename Shdr, typename Sym, typename ElfInterfaceType> | 
|  | 777 | void ElfInterfaceTest::InitSectionHeaders(uint64_t entry_size) { | 
|  | 778 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); | 
|  | 779 |  | 
|  | 780 | uint64_t offset = 0x1000; | 
|  | 781 |  | 
|  | 782 | Ehdr ehdr; | 
|  | 783 | memset(&ehdr, 0, sizeof(ehdr)); | 
|  | 784 | ehdr.e_shoff = offset; | 
|  | 785 | ehdr.e_shnum = 10; | 
|  | 786 | ehdr.e_shentsize = entry_size; | 
|  | 787 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); | 
|  | 788 |  | 
|  | 789 | offset += ehdr.e_shentsize; | 
|  | 790 |  | 
|  | 791 | Shdr shdr; | 
|  | 792 | memset(&shdr, 0, sizeof(shdr)); | 
|  | 793 | shdr.sh_type = SHT_SYMTAB; | 
|  | 794 | shdr.sh_link = 4; | 
|  | 795 | shdr.sh_addr = 0x5000; | 
|  | 796 | shdr.sh_offset = 0x5000; | 
|  | 797 | shdr.sh_entsize = sizeof(Sym); | 
|  | 798 | shdr.sh_size = shdr.sh_entsize * 10; | 
|  | 799 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); | 
|  | 800 | offset += ehdr.e_shentsize; | 
|  | 801 |  | 
|  | 802 | memset(&shdr, 0, sizeof(shdr)); | 
|  | 803 | shdr.sh_type = SHT_DYNSYM; | 
|  | 804 | shdr.sh_link = 4; | 
|  | 805 | shdr.sh_addr = 0x6000; | 
|  | 806 | shdr.sh_offset = 0x6000; | 
|  | 807 | shdr.sh_entsize = sizeof(Sym); | 
|  | 808 | shdr.sh_size = shdr.sh_entsize * 10; | 
|  | 809 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); | 
|  | 810 | offset += ehdr.e_shentsize; | 
|  | 811 |  | 
|  | 812 | memset(&shdr, 0, sizeof(shdr)); | 
|  | 813 | shdr.sh_type = SHT_PROGBITS; | 
|  | 814 | shdr.sh_name = 0xa000; | 
|  | 815 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); | 
|  | 816 | offset += ehdr.e_shentsize; | 
|  | 817 |  | 
|  | 818 | // The string data for the entries. | 
|  | 819 | memset(&shdr, 0, sizeof(shdr)); | 
|  | 820 | shdr.sh_type = SHT_STRTAB; | 
|  | 821 | shdr.sh_name = 0x20000; | 
|  | 822 | shdr.sh_offset = 0xf000; | 
|  | 823 | shdr.sh_size = 0x1000; | 
|  | 824 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); | 
|  | 825 | offset += ehdr.e_shentsize; | 
|  | 826 |  | 
|  | 827 | InitSym<Sym>(0x5000, 0x90000, 0x1000, 0x100, 0xf000, "function_one"); | 
|  | 828 | InitSym<Sym>(0x6000, 0xd0000, 0x1000, 0x300, 0xf000, "function_two"); | 
|  | 829 |  | 
|  | 830 | ASSERT_TRUE(elf->Init()); | 
|  | 831 | EXPECT_EQ(0U, elf->debug_frame_offset()); | 
|  | 832 | EXPECT_EQ(0U, elf->debug_frame_size()); | 
|  | 833 | EXPECT_EQ(0U, elf->gnu_debugdata_offset()); | 
|  | 834 | EXPECT_EQ(0U, elf->gnu_debugdata_size()); | 
|  | 835 |  | 
|  | 836 | // Look in the first symbol table. | 
|  | 837 | std::string name; | 
|  | 838 | uint64_t name_offset; | 
|  | 839 | ASSERT_TRUE(elf->GetFunctionName(0x90010, &name, &name_offset)); | 
|  | 840 | EXPECT_EQ("function_one", name); | 
|  | 841 | EXPECT_EQ(16U, name_offset); | 
|  | 842 | ASSERT_TRUE(elf->GetFunctionName(0xd0020, &name, &name_offset)); | 
|  | 843 | EXPECT_EQ("function_two", name); | 
|  | 844 | EXPECT_EQ(32U, name_offset); | 
|  | 845 | } | 
|  | 846 |  | 
|  | 847 | TEST_F(ElfInterfaceTest, init_section_headers32) { | 
|  | 848 | InitSectionHeaders<Elf32_Ehdr, Elf32_Shdr, Elf32_Sym, ElfInterface32>(sizeof(Elf32_Shdr)); | 
|  | 849 | } | 
|  | 850 |  | 
|  | 851 | TEST_F(ElfInterfaceTest, init_section_headers64) { | 
|  | 852 | InitSectionHeaders<Elf64_Ehdr, Elf64_Shdr, Elf64_Sym, ElfInterface64>(sizeof(Elf64_Shdr)); | 
|  | 853 | } | 
|  | 854 |  | 
|  | 855 | TEST_F(ElfInterfaceTest, init_section_headers_non_std_entry_size32) { | 
|  | 856 | InitSectionHeaders<Elf32_Ehdr, Elf32_Shdr, Elf32_Sym, ElfInterface32>(0x100); | 
|  | 857 | } | 
|  | 858 |  | 
|  | 859 | TEST_F(ElfInterfaceTest, init_section_headers_non_std_entry_size64) { | 
|  | 860 | InitSectionHeaders<Elf64_Ehdr, Elf64_Shdr, Elf64_Sym, ElfInterface64>(0x100); | 
|  | 861 | } | 
|  | 862 |  | 
|  | 863 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> | 
|  | 864 | void ElfInterfaceTest::InitSectionHeadersOffsets() { | 
|  | 865 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); | 
|  | 866 |  | 
|  | 867 | uint64_t offset = 0x2000; | 
|  | 868 |  | 
|  | 869 | Ehdr ehdr; | 
|  | 870 | memset(&ehdr, 0, sizeof(ehdr)); | 
|  | 871 | ehdr.e_shoff = offset; | 
|  | 872 | ehdr.e_shnum = 10; | 
|  | 873 | ehdr.e_shentsize = sizeof(Shdr); | 
|  | 874 | ehdr.e_shstrndx = 2; | 
|  | 875 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); | 
|  | 876 |  | 
|  | 877 | offset += ehdr.e_shentsize; | 
|  | 878 |  | 
|  | 879 | Shdr shdr; | 
|  | 880 | memset(&shdr, 0, sizeof(shdr)); | 
|  | 881 | shdr.sh_type = SHT_PROGBITS; | 
|  | 882 | shdr.sh_link = 2; | 
|  | 883 | shdr.sh_name = 0x200; | 
|  | 884 | shdr.sh_addr = 0x5000; | 
|  | 885 | shdr.sh_offset = 0x5000; | 
|  | 886 | shdr.sh_entsize = 0x100; | 
|  | 887 | shdr.sh_size = 0x800; | 
|  | 888 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); | 
|  | 889 | offset += ehdr.e_shentsize; | 
|  | 890 |  | 
|  | 891 | // The string data for section header names. | 
|  | 892 | memset(&shdr, 0, sizeof(shdr)); | 
|  | 893 | shdr.sh_type = SHT_STRTAB; | 
|  | 894 | shdr.sh_name = 0x20000; | 
|  | 895 | shdr.sh_offset = 0xf000; | 
|  | 896 | shdr.sh_size = 0x1000; | 
|  | 897 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); | 
|  | 898 | offset += ehdr.e_shentsize; | 
|  | 899 |  | 
|  | 900 | memset(&shdr, 0, sizeof(shdr)); | 
|  | 901 | shdr.sh_type = SHT_PROGBITS; | 
|  | 902 | shdr.sh_link = 2; | 
|  | 903 | shdr.sh_name = 0x100; | 
|  | 904 | shdr.sh_addr = 0x6000; | 
|  | 905 | shdr.sh_offset = 0x6000; | 
|  | 906 | shdr.sh_entsize = 0x100; | 
|  | 907 | shdr.sh_size = 0x500; | 
|  | 908 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); | 
|  | 909 | offset += ehdr.e_shentsize; | 
|  | 910 |  | 
|  | 911 | memory_.SetMemory(0xf100, ".debug_frame", sizeof(".debug_frame")); | 
|  | 912 | memory_.SetMemory(0xf200, ".gnu_debugdata", sizeof(".gnu_debugdata")); | 
|  | 913 |  | 
|  | 914 | ASSERT_TRUE(elf->Init()); | 
|  | 915 | EXPECT_EQ(0x6000U, elf->debug_frame_offset()); | 
|  | 916 | EXPECT_EQ(0x500U, elf->debug_frame_size()); | 
|  | 917 | EXPECT_EQ(0x5000U, elf->gnu_debugdata_offset()); | 
|  | 918 | EXPECT_EQ(0x800U, elf->gnu_debugdata_size()); | 
|  | 919 | } | 
|  | 920 |  | 
|  | 921 | TEST_F(ElfInterfaceTest, init_section_headers_offsets32) { | 
|  | 922 | InitSectionHeadersOffsets<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(); | 
|  | 923 | } | 
|  | 924 |  | 
|  | 925 | TEST_F(ElfInterfaceTest, init_section_headers_offsets64) { | 
|  | 926 | InitSectionHeadersOffsets<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(); | 
|  | 927 | } | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 928 |  | 
|  | 929 | }  // namespace unwindstack |