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 | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 100 | template <typename Ehdr, typename Phdr, typename ElfInterfaceType> |
| 101 | void InitProgramHeadersMalformed(); |
| 102 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 103 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> |
| 104 | void InitSectionHeadersMalformed(); |
| 105 | |
Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 106 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> |
| 107 | void InitSectionHeadersMalformedSymData(); |
| 108 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 109 | template <typename Ehdr, typename Shdr, typename Sym, typename ElfInterfaceType> |
| 110 | void InitSectionHeaders(uint64_t entry_size); |
| 111 | |
| 112 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> |
| 113 | void InitSectionHeadersOffsets(); |
| 114 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 115 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> |
| 116 | void InitSectionHeadersOffsetsEhFrameSectionBias(uint64_t addr, uint64_t offset, |
| 117 | int64_t expected_bias); |
| 118 | |
| 119 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> |
| 120 | void InitSectionHeadersOffsetsEhFrameHdrSectionBias(uint64_t addr, uint64_t offset, |
| 121 | int64_t expected_bias); |
| 122 | |
| 123 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> |
| 124 | void InitSectionHeadersOffsetsDebugFrameSectionBias(uint64_t addr, uint64_t offset, |
| 125 | int64_t expected_bias); |
| 126 | |
| 127 | template <typename Ehdr, typename Phdr, typename ElfInterfaceType> |
| 128 | void CheckGnuEhFrame(uint64_t addr, uint64_t offset, int64_t expected_bias); |
| 129 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 130 | template <typename Sym> |
| 131 | void InitSym(uint64_t offset, uint32_t value, uint32_t size, uint32_t name_offset, |
| 132 | uint64_t sym_offset, const char* name); |
| 133 | |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 134 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 135 | void BuildID(); |
| 136 | |
| 137 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 138 | void BuildIDTwoNotes(); |
| 139 | |
| 140 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 141 | void BuildIDSectionTooSmallForName(); |
| 142 | |
| 143 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 144 | void BuildIDSectionTooSmallForDesc(); |
| 145 | |
| 146 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 147 | void BuildIDSectionTooSmallForHeader(); |
| 148 | |
Christopher Ferris | 6c8ac56 | 2019-10-02 17:41:16 -0700 | [diff] [blame] | 149 | template <typename Ehdr, typename Phdr, typename ElfInterfaceType> |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 150 | void CheckLoadBiasInFirstPhdr(int64_t load_bias); |
Christopher Ferris | 6c8ac56 | 2019-10-02 17:41:16 -0700 | [diff] [blame] | 151 | |
| 152 | template <typename Ehdr, typename Phdr, typename ElfInterfaceType> |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 153 | void CheckLoadBiasInFirstExecPhdr(uint64_t offset, uint64_t vaddr, int64_t load_bias); |
Christopher Ferris | 6c8ac56 | 2019-10-02 17:41:16 -0700 | [diff] [blame] | 154 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 155 | MemoryFake memory_; |
| 156 | }; |
| 157 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 158 | template <typename Sym> |
| 159 | void ElfInterfaceTest::InitSym(uint64_t offset, uint32_t value, uint32_t size, uint32_t name_offset, |
| 160 | uint64_t sym_offset, const char* name) { |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 161 | Sym sym = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 162 | sym.st_info = STT_FUNC; |
| 163 | sym.st_value = value; |
| 164 | sym.st_size = size; |
| 165 | sym.st_name = name_offset; |
| 166 | sym.st_shndx = SHN_COMMON; |
| 167 | |
| 168 | memory_.SetMemory(offset, &sym, sizeof(sym)); |
| 169 | memory_.SetMemory(sym_offset + name_offset, name, strlen(name) + 1); |
| 170 | } |
| 171 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 172 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> |
| 173 | void ElfInterfaceTest::SinglePtLoad() { |
| 174 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 175 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 176 | Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 177 | ehdr.e_phoff = 0x100; |
| 178 | ehdr.e_phnum = 1; |
| 179 | ehdr.e_phentsize = sizeof(Phdr); |
| 180 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 181 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 182 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 183 | phdr.p_type = PT_LOAD; |
| 184 | phdr.p_vaddr = 0x2000; |
| 185 | phdr.p_memsz = 0x10000; |
| 186 | phdr.p_flags = PF_R | PF_X; |
| 187 | phdr.p_align = 0x1000; |
| 188 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 189 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 190 | int64_t load_bias = 0; |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 191 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 192 | EXPECT_EQ(0x2000, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 193 | |
| 194 | const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads(); |
| 195 | ASSERT_EQ(1U, pt_loads.size()); |
| 196 | LoadInfo load_data = pt_loads.at(0); |
| 197 | ASSERT_EQ(0U, load_data.offset); |
| 198 | ASSERT_EQ(0x2000U, load_data.table_offset); |
| 199 | ASSERT_EQ(0x10000U, load_data.table_size); |
| 200 | } |
| 201 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 202 | TEST_F(ElfInterfaceTest, single_pt_load_32) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 203 | SinglePtLoad<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); |
| 204 | } |
| 205 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 206 | TEST_F(ElfInterfaceTest, single_pt_load_64) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 207 | SinglePtLoad<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); |
| 208 | } |
| 209 | |
| 210 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> |
| 211 | void ElfInterfaceTest::MultipleExecutablePtLoads() { |
| 212 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 213 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 214 | Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 215 | ehdr.e_phoff = 0x100; |
| 216 | ehdr.e_phnum = 3; |
| 217 | ehdr.e_phentsize = sizeof(Phdr); |
| 218 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 219 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 220 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 221 | phdr.p_type = PT_LOAD; |
| 222 | phdr.p_vaddr = 0x2000; |
| 223 | phdr.p_memsz = 0x10000; |
| 224 | phdr.p_flags = PF_R | PF_X; |
| 225 | phdr.p_align = 0x1000; |
| 226 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 227 | |
| 228 | memset(&phdr, 0, sizeof(phdr)); |
| 229 | phdr.p_type = PT_LOAD; |
| 230 | phdr.p_offset = 0x1000; |
| 231 | phdr.p_vaddr = 0x2001; |
| 232 | phdr.p_memsz = 0x10001; |
| 233 | phdr.p_flags = PF_R | PF_X; |
| 234 | phdr.p_align = 0x1001; |
| 235 | memory_.SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr)); |
| 236 | |
| 237 | memset(&phdr, 0, sizeof(phdr)); |
| 238 | phdr.p_type = PT_LOAD; |
| 239 | phdr.p_offset = 0x2000; |
| 240 | phdr.p_vaddr = 0x2002; |
| 241 | phdr.p_memsz = 0x10002; |
| 242 | phdr.p_flags = PF_R | PF_X; |
| 243 | phdr.p_align = 0x1002; |
| 244 | memory_.SetMemory(0x100 + 2 * sizeof(phdr), &phdr, sizeof(phdr)); |
| 245 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 246 | int64_t load_bias = 0; |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 247 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 248 | EXPECT_EQ(0x2000, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 249 | |
| 250 | const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads(); |
| 251 | ASSERT_EQ(3U, pt_loads.size()); |
| 252 | |
| 253 | LoadInfo load_data = pt_loads.at(0); |
| 254 | ASSERT_EQ(0U, load_data.offset); |
| 255 | ASSERT_EQ(0x2000U, load_data.table_offset); |
| 256 | ASSERT_EQ(0x10000U, load_data.table_size); |
| 257 | |
| 258 | load_data = pt_loads.at(0x1000); |
| 259 | ASSERT_EQ(0x1000U, load_data.offset); |
| 260 | ASSERT_EQ(0x2001U, load_data.table_offset); |
| 261 | ASSERT_EQ(0x10001U, load_data.table_size); |
| 262 | |
| 263 | load_data = pt_loads.at(0x2000); |
| 264 | ASSERT_EQ(0x2000U, load_data.offset); |
| 265 | ASSERT_EQ(0x2002U, load_data.table_offset); |
| 266 | ASSERT_EQ(0x10002U, load_data.table_size); |
| 267 | } |
| 268 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 269 | TEST_F(ElfInterfaceTest, multiple_executable_pt_loads_32) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 270 | MultipleExecutablePtLoads<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); |
| 271 | } |
| 272 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 273 | TEST_F(ElfInterfaceTest, multiple_executable_pt_loads_64) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 274 | MultipleExecutablePtLoads<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); |
| 275 | } |
| 276 | |
| 277 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> |
| 278 | void ElfInterfaceTest::MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr() { |
| 279 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 280 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 281 | Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 282 | ehdr.e_phoff = 0x100; |
| 283 | ehdr.e_phnum = 3; |
| 284 | ehdr.e_phentsize = sizeof(Phdr) + 100; |
| 285 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 286 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 287 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 288 | phdr.p_type = PT_LOAD; |
| 289 | phdr.p_vaddr = 0x2000; |
| 290 | phdr.p_memsz = 0x10000; |
| 291 | phdr.p_flags = PF_R | PF_X; |
| 292 | phdr.p_align = 0x1000; |
| 293 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 294 | |
| 295 | memset(&phdr, 0, sizeof(phdr)); |
| 296 | phdr.p_type = PT_LOAD; |
| 297 | phdr.p_offset = 0x1000; |
| 298 | phdr.p_vaddr = 0x2001; |
| 299 | phdr.p_memsz = 0x10001; |
| 300 | phdr.p_flags = PF_R | PF_X; |
| 301 | phdr.p_align = 0x1001; |
| 302 | memory_.SetMemory(0x100 + sizeof(phdr) + 100, &phdr, sizeof(phdr)); |
| 303 | |
| 304 | memset(&phdr, 0, sizeof(phdr)); |
| 305 | phdr.p_type = PT_LOAD; |
| 306 | phdr.p_offset = 0x2000; |
| 307 | phdr.p_vaddr = 0x2002; |
| 308 | phdr.p_memsz = 0x10002; |
| 309 | phdr.p_flags = PF_R | PF_X; |
| 310 | phdr.p_align = 0x1002; |
| 311 | memory_.SetMemory(0x100 + 2 * (sizeof(phdr) + 100), &phdr, sizeof(phdr)); |
| 312 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 313 | int64_t load_bias = 0; |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 314 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 315 | EXPECT_EQ(0x2000, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 316 | |
| 317 | const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads(); |
| 318 | ASSERT_EQ(3U, pt_loads.size()); |
| 319 | |
| 320 | LoadInfo load_data = pt_loads.at(0); |
| 321 | ASSERT_EQ(0U, load_data.offset); |
| 322 | ASSERT_EQ(0x2000U, load_data.table_offset); |
| 323 | ASSERT_EQ(0x10000U, load_data.table_size); |
| 324 | |
| 325 | load_data = pt_loads.at(0x1000); |
| 326 | ASSERT_EQ(0x1000U, load_data.offset); |
| 327 | ASSERT_EQ(0x2001U, load_data.table_offset); |
| 328 | ASSERT_EQ(0x10001U, load_data.table_size); |
| 329 | |
| 330 | load_data = pt_loads.at(0x2000); |
| 331 | ASSERT_EQ(0x2000U, load_data.offset); |
| 332 | ASSERT_EQ(0x2002U, load_data.table_offset); |
| 333 | ASSERT_EQ(0x10002U, load_data.table_size); |
| 334 | } |
| 335 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 336 | TEST_F(ElfInterfaceTest, multiple_executable_pt_loads_increments_not_size_of_phdr_32) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 337 | MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, |
| 338 | ElfInterface32>(); |
| 339 | } |
| 340 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 341 | TEST_F(ElfInterfaceTest, multiple_executable_pt_loads_increments_not_size_of_phdr_64) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 342 | MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, |
| 343 | ElfInterface64>(); |
| 344 | } |
| 345 | |
| 346 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> |
| 347 | void ElfInterfaceTest::NonExecutablePtLoads() { |
| 348 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 349 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 350 | Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 351 | ehdr.e_phoff = 0x100; |
| 352 | ehdr.e_phnum = 3; |
| 353 | ehdr.e_phentsize = sizeof(Phdr); |
| 354 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 355 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 356 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 357 | phdr.p_type = PT_LOAD; |
| 358 | phdr.p_vaddr = 0x2000; |
| 359 | phdr.p_memsz = 0x10000; |
| 360 | phdr.p_flags = PF_R; |
| 361 | phdr.p_align = 0x1000; |
| 362 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 363 | |
| 364 | memset(&phdr, 0, sizeof(phdr)); |
| 365 | phdr.p_type = PT_LOAD; |
| 366 | phdr.p_offset = 0x1000; |
| 367 | phdr.p_vaddr = 0x2001; |
| 368 | phdr.p_memsz = 0x10001; |
| 369 | phdr.p_flags = PF_R | PF_X; |
| 370 | phdr.p_align = 0x1001; |
| 371 | memory_.SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr)); |
| 372 | |
| 373 | memset(&phdr, 0, sizeof(phdr)); |
| 374 | phdr.p_type = PT_LOAD; |
| 375 | phdr.p_offset = 0x2000; |
| 376 | phdr.p_vaddr = 0x2002; |
| 377 | phdr.p_memsz = 0x10002; |
| 378 | phdr.p_flags = PF_R; |
| 379 | phdr.p_align = 0x1002; |
| 380 | memory_.SetMemory(0x100 + 2 * sizeof(phdr), &phdr, sizeof(phdr)); |
| 381 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 382 | int64_t load_bias = 0; |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 383 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 384 | EXPECT_EQ(0x1001, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 385 | |
| 386 | const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads(); |
| 387 | ASSERT_EQ(1U, pt_loads.size()); |
| 388 | |
| 389 | LoadInfo load_data = pt_loads.at(0x1000); |
| 390 | ASSERT_EQ(0x1000U, load_data.offset); |
| 391 | ASSERT_EQ(0x2001U, load_data.table_offset); |
| 392 | ASSERT_EQ(0x10001U, load_data.table_size); |
| 393 | } |
| 394 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 395 | TEST_F(ElfInterfaceTest, non_executable_pt_loads_32) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 396 | NonExecutablePtLoads<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); |
| 397 | } |
| 398 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 399 | TEST_F(ElfInterfaceTest, non_executable_pt_loads_64) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 400 | NonExecutablePtLoads<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); |
| 401 | } |
| 402 | |
| 403 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> |
| 404 | void ElfInterfaceTest::ManyPhdrs() { |
| 405 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 406 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 407 | Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 408 | ehdr.e_phoff = 0x100; |
| 409 | ehdr.e_phnum = 7; |
| 410 | ehdr.e_phentsize = sizeof(Phdr); |
| 411 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 412 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 413 | uint64_t phdr_offset = 0x100; |
| 414 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 415 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 416 | phdr.p_type = PT_LOAD; |
| 417 | phdr.p_vaddr = 0x2000; |
| 418 | phdr.p_memsz = 0x10000; |
| 419 | phdr.p_flags = PF_R | PF_X; |
| 420 | phdr.p_align = 0x1000; |
| 421 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); |
| 422 | phdr_offset += sizeof(phdr); |
| 423 | |
| 424 | memset(&phdr, 0, sizeof(phdr)); |
| 425 | phdr.p_type = PT_GNU_EH_FRAME; |
| 426 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); |
| 427 | phdr_offset += sizeof(phdr); |
| 428 | |
| 429 | memset(&phdr, 0, sizeof(phdr)); |
| 430 | phdr.p_type = PT_DYNAMIC; |
| 431 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); |
| 432 | phdr_offset += sizeof(phdr); |
| 433 | |
| 434 | memset(&phdr, 0, sizeof(phdr)); |
| 435 | phdr.p_type = PT_INTERP; |
| 436 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); |
| 437 | phdr_offset += sizeof(phdr); |
| 438 | |
| 439 | memset(&phdr, 0, sizeof(phdr)); |
| 440 | phdr.p_type = PT_NOTE; |
| 441 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); |
| 442 | phdr_offset += sizeof(phdr); |
| 443 | |
| 444 | memset(&phdr, 0, sizeof(phdr)); |
| 445 | phdr.p_type = PT_SHLIB; |
| 446 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); |
| 447 | phdr_offset += sizeof(phdr); |
| 448 | |
| 449 | memset(&phdr, 0, sizeof(phdr)); |
| 450 | phdr.p_type = PT_GNU_EH_FRAME; |
| 451 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 452 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 453 | int64_t load_bias = 0; |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 454 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 455 | EXPECT_EQ(0x2000, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 456 | |
| 457 | const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads(); |
| 458 | ASSERT_EQ(1U, pt_loads.size()); |
| 459 | |
| 460 | LoadInfo load_data = pt_loads.at(0); |
| 461 | ASSERT_EQ(0U, load_data.offset); |
| 462 | ASSERT_EQ(0x2000U, load_data.table_offset); |
| 463 | ASSERT_EQ(0x10000U, load_data.table_size); |
| 464 | } |
| 465 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 466 | TEST_F(ElfInterfaceTest, many_phdrs_32) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 467 | ElfInterfaceTest::ManyPhdrs<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); |
| 468 | } |
| 469 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 470 | TEST_F(ElfInterfaceTest, many_phdrs_64) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 471 | ElfInterfaceTest::ManyPhdrs<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); |
| 472 | } |
| 473 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 474 | TEST_F(ElfInterfaceTest, arm32) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 475 | ElfInterfaceArm elf_arm(&memory_); |
| 476 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 477 | Elf32_Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 478 | ehdr.e_phoff = 0x100; |
| 479 | ehdr.e_phnum = 1; |
| 480 | ehdr.e_phentsize = sizeof(Elf32_Phdr); |
| 481 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 482 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 483 | Elf32_Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 484 | phdr.p_type = PT_ARM_EXIDX; |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 485 | phdr.p_offset = 0x2000; |
| 486 | phdr.p_filesz = 16; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 487 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 488 | |
| 489 | // Add arm exidx entries. |
| 490 | memory_.SetData32(0x2000, 0x1000); |
| 491 | memory_.SetData32(0x2008, 0x1000); |
| 492 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 493 | int64_t load_bias = 0; |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 494 | ASSERT_TRUE(elf_arm.Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 495 | EXPECT_EQ(0, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 496 | |
| 497 | std::vector<uint32_t> entries; |
| 498 | for (auto addr : elf_arm) { |
| 499 | entries.push_back(addr); |
| 500 | } |
| 501 | ASSERT_EQ(2U, entries.size()); |
| 502 | ASSERT_EQ(0x3000U, entries[0]); |
| 503 | ASSERT_EQ(0x3008U, entries[1]); |
| 504 | |
| 505 | ASSERT_EQ(0x2000U, elf_arm.start_offset()); |
| 506 | ASSERT_EQ(2U, elf_arm.total_entries()); |
| 507 | } |
| 508 | |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 509 | template <typename Ehdr, typename Phdr, typename Shdr, typename Dyn> |
| 510 | void ElfInterfaceTest::SonameInit(SonameTestEnum test_type) { |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 511 | Ehdr ehdr = {}; |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 512 | ehdr.e_shoff = 0x200; |
| 513 | ehdr.e_shnum = 2; |
| 514 | ehdr.e_shentsize = sizeof(Shdr); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 515 | ehdr.e_phoff = 0x100; |
| 516 | ehdr.e_phnum = 1; |
| 517 | ehdr.e_phentsize = sizeof(Phdr); |
| 518 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 519 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 520 | Shdr shdr = {}; |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 521 | shdr.sh_type = SHT_STRTAB; |
| 522 | if (test_type == SONAME_MISSING_MAP) { |
| 523 | shdr.sh_addr = 0x20100; |
| 524 | } else { |
| 525 | shdr.sh_addr = 0x10100; |
| 526 | } |
| 527 | shdr.sh_offset = 0x10000; |
| 528 | memory_.SetMemory(0x200 + sizeof(shdr), &shdr, sizeof(shdr)); |
| 529 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 530 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 531 | phdr.p_type = PT_DYNAMIC; |
| 532 | phdr.p_offset = 0x2000; |
| 533 | phdr.p_memsz = sizeof(Dyn) * 3; |
| 534 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 535 | |
| 536 | uint64_t offset = 0x2000; |
| 537 | Dyn dyn; |
| 538 | |
| 539 | dyn.d_tag = DT_STRTAB; |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 540 | dyn.d_un.d_ptr = 0x10100; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 541 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); |
| 542 | offset += sizeof(dyn); |
| 543 | |
| 544 | dyn.d_tag = DT_STRSZ; |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 545 | if (test_type == SONAME_DTSIZE_SMALL) { |
| 546 | dyn.d_un.d_val = 0x10; |
| 547 | } else { |
| 548 | dyn.d_un.d_val = 0x1000; |
| 549 | } |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 550 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); |
| 551 | offset += sizeof(dyn); |
| 552 | |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 553 | if (test_type == SONAME_DTNULL_AFTER) { |
| 554 | dyn.d_tag = DT_NULL; |
| 555 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); |
| 556 | offset += sizeof(dyn); |
| 557 | } |
| 558 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 559 | dyn.d_tag = DT_SONAME; |
| 560 | dyn.d_un.d_val = 0x10; |
| 561 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); |
| 562 | offset += sizeof(dyn); |
| 563 | |
| 564 | dyn.d_tag = DT_NULL; |
| 565 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); |
| 566 | |
| 567 | SetStringMemory(0x10010, "fake_soname.so"); |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | template <typename ElfInterfaceType> |
| 571 | void ElfInterfaceTest::Soname() { |
| 572 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 573 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 574 | int64_t load_bias = 0; |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 575 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 576 | EXPECT_EQ(0, load_bias); |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 577 | |
Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 578 | ASSERT_EQ("fake_soname.so", elf->GetSoname()); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 581 | TEST_F(ElfInterfaceTest, soname_32) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 582 | SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(); |
| 583 | Soname<ElfInterface32>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 584 | } |
| 585 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 586 | TEST_F(ElfInterfaceTest, soname_64) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 587 | SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(); |
| 588 | Soname<ElfInterface64>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 589 | } |
| 590 | |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 591 | template <typename ElfInterfaceType> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 592 | void ElfInterfaceTest::SonameAfterDtNull() { |
| 593 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 594 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 595 | int64_t load_bias = 0; |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 596 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 597 | EXPECT_EQ(0, load_bias); |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 598 | |
Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 599 | ASSERT_EQ("", elf->GetSoname()); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 600 | } |
| 601 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 602 | TEST_F(ElfInterfaceTest, soname_after_dt_null_32) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 603 | SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_DTNULL_AFTER); |
| 604 | SonameAfterDtNull<ElfInterface32>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 605 | } |
| 606 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 607 | TEST_F(ElfInterfaceTest, soname_after_dt_null_64) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 608 | SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(SONAME_DTNULL_AFTER); |
| 609 | SonameAfterDtNull<ElfInterface64>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 610 | } |
| 611 | |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 612 | template <typename ElfInterfaceType> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 613 | void ElfInterfaceTest::SonameSize() { |
| 614 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 615 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 616 | int64_t load_bias = 0; |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 617 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 618 | EXPECT_EQ(0, load_bias); |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 619 | |
Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 620 | ASSERT_EQ("", elf->GetSoname()); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 623 | TEST_F(ElfInterfaceTest, soname_size_32) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 624 | SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_DTSIZE_SMALL); |
| 625 | SonameSize<ElfInterface32>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 626 | } |
| 627 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 628 | TEST_F(ElfInterfaceTest, soname_size_64) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 629 | SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(SONAME_DTSIZE_SMALL); |
| 630 | SonameSize<ElfInterface64>(); |
| 631 | } |
| 632 | |
| 633 | // Verify that there is no map from STRTAB in the dynamic section to a |
| 634 | // STRTAB entry in the section headers. |
| 635 | template <typename ElfInterfaceType> |
| 636 | void ElfInterfaceTest::SonameMissingMap() { |
| 637 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 638 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 639 | int64_t load_bias = 0; |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 640 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 641 | EXPECT_EQ(0, load_bias); |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 642 | |
Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 643 | ASSERT_EQ("", elf->GetSoname()); |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 644 | } |
| 645 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 646 | TEST_F(ElfInterfaceTest, soname_missing_map_32) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 647 | SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_MISSING_MAP); |
| 648 | SonameMissingMap<ElfInterface32>(); |
| 649 | } |
| 650 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 651 | TEST_F(ElfInterfaceTest, soname_missing_map_64) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 652 | SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(SONAME_MISSING_MAP); |
| 653 | SonameMissingMap<ElfInterface64>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 654 | } |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 655 | |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 656 | template <typename ElfType> |
| 657 | void ElfInterfaceTest::InitHeadersEhFrameTest() { |
| 658 | ElfType elf(&memory_); |
| 659 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 660 | elf.FakeSetEhFrameOffset(0x10000); |
| 661 | elf.FakeSetEhFrameSize(0); |
| 662 | elf.FakeSetDebugFrameOffset(0); |
| 663 | elf.FakeSetDebugFrameSize(0); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 664 | |
| 665 | memory_.SetMemory(0x10000, |
| 666 | std::vector<uint8_t>{0x1, DW_EH_PE_udata2, DW_EH_PE_udata2, DW_EH_PE_udata2}); |
| 667 | memory_.SetData32(0x10004, 0x500); |
| 668 | memory_.SetData32(0x10008, 250); |
| 669 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 670 | elf.InitHeaders(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 671 | |
| 672 | EXPECT_FALSE(elf.eh_frame() == nullptr); |
| 673 | EXPECT_TRUE(elf.debug_frame() == nullptr); |
| 674 | } |
| 675 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 676 | TEST_F(ElfInterfaceTest, init_headers_eh_frame_32) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 677 | InitHeadersEhFrameTest<ElfInterface32Fake>(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 680 | TEST_F(ElfInterfaceTest, init_headers_eh_frame_64) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 681 | InitHeadersEhFrameTest<ElfInterface64Fake>(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | template <typename ElfType> |
| 685 | void ElfInterfaceTest::InitHeadersDebugFrame() { |
| 686 | ElfType elf(&memory_); |
| 687 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 688 | elf.FakeSetEhFrameOffset(0); |
| 689 | elf.FakeSetEhFrameSize(0); |
| 690 | elf.FakeSetDebugFrameOffset(0x5000); |
| 691 | elf.FakeSetDebugFrameSize(0x200); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 692 | |
| 693 | memory_.SetData32(0x5000, 0xfc); |
| 694 | memory_.SetData32(0x5004, 0xffffffff); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 695 | memory_.SetMemory(0x5008, std::vector<uint8_t>{1, '\0', 4, 8, 2}); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 696 | |
| 697 | memory_.SetData32(0x5100, 0xfc); |
| 698 | memory_.SetData32(0x5104, 0); |
| 699 | memory_.SetData32(0x5108, 0x1500); |
| 700 | memory_.SetData32(0x510c, 0x200); |
| 701 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 702 | elf.InitHeaders(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 703 | |
| 704 | EXPECT_TRUE(elf.eh_frame() == nullptr); |
| 705 | EXPECT_FALSE(elf.debug_frame() == nullptr); |
| 706 | } |
| 707 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 708 | TEST_F(ElfInterfaceTest, init_headers_debug_frame_32) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 709 | InitHeadersDebugFrame<ElfInterface32Fake>(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 712 | TEST_F(ElfInterfaceTest, init_headers_debug_frame_64) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 713 | InitHeadersDebugFrame<ElfInterface64Fake>(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 716 | template <typename Ehdr, typename Phdr, typename ElfInterfaceType> |
| 717 | void ElfInterfaceTest::InitProgramHeadersMalformed() { |
| 718 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 719 | |
| 720 | Ehdr ehdr = {}; |
| 721 | ehdr.e_phoff = 0x100; |
| 722 | ehdr.e_phnum = 3; |
| 723 | ehdr.e_phentsize = sizeof(Phdr); |
| 724 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 725 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 726 | int64_t load_bias = 0; |
Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 727 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 728 | EXPECT_EQ(0, load_bias); |
Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 731 | TEST_F(ElfInterfaceTest, init_program_headers_malformed_32) { |
Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 732 | InitProgramHeadersMalformed<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(); |
| 733 | } |
| 734 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 735 | TEST_F(ElfInterfaceTest, init_program_headers_malformed_64) { |
Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 736 | InitProgramHeadersMalformed<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(); |
| 737 | } |
| 738 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 739 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> |
| 740 | void ElfInterfaceTest::InitSectionHeadersMalformed() { |
| 741 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 742 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 743 | Ehdr ehdr = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 744 | ehdr.e_shoff = 0x1000; |
| 745 | ehdr.e_shnum = 10; |
| 746 | ehdr.e_shentsize = sizeof(Shdr); |
| 747 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 748 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 749 | int64_t load_bias = 0; |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 750 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 751 | EXPECT_EQ(0, load_bias); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 754 | TEST_F(ElfInterfaceTest, init_section_headers_malformed_32) { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 755 | InitSectionHeadersMalformed<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(); |
| 756 | } |
| 757 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 758 | TEST_F(ElfInterfaceTest, init_section_headers_malformed_64) { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 759 | InitSectionHeadersMalformed<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(); |
| 760 | } |
| 761 | |
Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 762 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> |
| 763 | void ElfInterfaceTest::InitSectionHeadersMalformedSymData() { |
| 764 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 765 | |
| 766 | uint64_t offset = 0x1000; |
| 767 | |
| 768 | Ehdr ehdr = {}; |
| 769 | ehdr.e_shoff = offset; |
| 770 | ehdr.e_shnum = 5; |
| 771 | ehdr.e_shentsize = sizeof(Shdr); |
| 772 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 773 | |
| 774 | offset += ehdr.e_shentsize; |
| 775 | |
| 776 | Shdr shdr = {}; |
| 777 | shdr.sh_type = SHT_SYMTAB; |
| 778 | shdr.sh_link = 4; |
| 779 | shdr.sh_addr = 0x5000; |
| 780 | shdr.sh_offset = 0x5000; |
| 781 | shdr.sh_entsize = 0x100; |
| 782 | shdr.sh_size = shdr.sh_entsize * 10; |
| 783 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 784 | offset += ehdr.e_shentsize; |
| 785 | |
| 786 | memset(&shdr, 0, sizeof(shdr)); |
| 787 | shdr.sh_type = SHT_DYNSYM; |
| 788 | shdr.sh_link = 10; |
| 789 | shdr.sh_addr = 0x6000; |
| 790 | shdr.sh_offset = 0x6000; |
| 791 | shdr.sh_entsize = 0x100; |
| 792 | shdr.sh_size = shdr.sh_entsize * 10; |
| 793 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 794 | offset += ehdr.e_shentsize; |
| 795 | |
| 796 | memset(&shdr, 0, sizeof(shdr)); |
| 797 | shdr.sh_type = SHT_DYNSYM; |
| 798 | shdr.sh_link = 2; |
| 799 | shdr.sh_addr = 0x6000; |
| 800 | shdr.sh_offset = 0x6000; |
| 801 | shdr.sh_entsize = 0x100; |
| 802 | shdr.sh_size = shdr.sh_entsize * 10; |
| 803 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 804 | offset += ehdr.e_shentsize; |
| 805 | |
| 806 | // The string data for the entries. |
| 807 | memset(&shdr, 0, sizeof(shdr)); |
| 808 | shdr.sh_type = SHT_STRTAB; |
| 809 | shdr.sh_name = 0x20000; |
| 810 | shdr.sh_offset = 0xf000; |
| 811 | shdr.sh_size = 0x1000; |
| 812 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 813 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 814 | int64_t load_bias = 0; |
Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 815 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 816 | EXPECT_EQ(0, load_bias); |
Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 817 | EXPECT_EQ(0U, elf->debug_frame_offset()); |
| 818 | EXPECT_EQ(0U, elf->debug_frame_size()); |
| 819 | EXPECT_EQ(0U, elf->gnu_debugdata_offset()); |
| 820 | EXPECT_EQ(0U, elf->gnu_debugdata_size()); |
| 821 | |
| 822 | std::string name; |
| 823 | uint64_t name_offset; |
| 824 | ASSERT_FALSE(elf->GetFunctionName(0x90010, &name, &name_offset)); |
| 825 | } |
| 826 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 827 | TEST_F(ElfInterfaceTest, init_section_headers_malformed_symdata_32) { |
Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 828 | InitSectionHeadersMalformedSymData<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(); |
| 829 | } |
| 830 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 831 | TEST_F(ElfInterfaceTest, init_section_headers_malformed_symdata_64) { |
Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 832 | InitSectionHeadersMalformedSymData<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(); |
| 833 | } |
| 834 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 835 | template <typename Ehdr, typename Shdr, typename Sym, typename ElfInterfaceType> |
| 836 | void ElfInterfaceTest::InitSectionHeaders(uint64_t entry_size) { |
| 837 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 838 | |
| 839 | uint64_t offset = 0x1000; |
| 840 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 841 | Ehdr ehdr = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 842 | ehdr.e_shoff = offset; |
Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 843 | ehdr.e_shnum = 5; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 844 | ehdr.e_shentsize = entry_size; |
| 845 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 846 | |
| 847 | offset += ehdr.e_shentsize; |
| 848 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 849 | Shdr shdr = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 850 | shdr.sh_type = SHT_SYMTAB; |
| 851 | shdr.sh_link = 4; |
| 852 | shdr.sh_addr = 0x5000; |
| 853 | shdr.sh_offset = 0x5000; |
| 854 | shdr.sh_entsize = sizeof(Sym); |
| 855 | shdr.sh_size = shdr.sh_entsize * 10; |
| 856 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 857 | offset += ehdr.e_shentsize; |
| 858 | |
| 859 | memset(&shdr, 0, sizeof(shdr)); |
| 860 | shdr.sh_type = SHT_DYNSYM; |
| 861 | shdr.sh_link = 4; |
| 862 | shdr.sh_addr = 0x6000; |
| 863 | shdr.sh_offset = 0x6000; |
| 864 | shdr.sh_entsize = sizeof(Sym); |
| 865 | shdr.sh_size = shdr.sh_entsize * 10; |
| 866 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 867 | offset += ehdr.e_shentsize; |
| 868 | |
| 869 | memset(&shdr, 0, sizeof(shdr)); |
| 870 | shdr.sh_type = SHT_PROGBITS; |
| 871 | shdr.sh_name = 0xa000; |
| 872 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 873 | offset += ehdr.e_shentsize; |
| 874 | |
| 875 | // The string data for the entries. |
| 876 | memset(&shdr, 0, sizeof(shdr)); |
| 877 | shdr.sh_type = SHT_STRTAB; |
| 878 | shdr.sh_name = 0x20000; |
| 879 | shdr.sh_offset = 0xf000; |
| 880 | shdr.sh_size = 0x1000; |
| 881 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 882 | |
| 883 | InitSym<Sym>(0x5000, 0x90000, 0x1000, 0x100, 0xf000, "function_one"); |
| 884 | InitSym<Sym>(0x6000, 0xd0000, 0x1000, 0x300, 0xf000, "function_two"); |
| 885 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 886 | int64_t load_bias = 0; |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 887 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 888 | EXPECT_EQ(0, load_bias); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 889 | EXPECT_EQ(0U, elf->debug_frame_offset()); |
| 890 | EXPECT_EQ(0U, elf->debug_frame_size()); |
| 891 | EXPECT_EQ(0U, elf->gnu_debugdata_offset()); |
| 892 | EXPECT_EQ(0U, elf->gnu_debugdata_size()); |
| 893 | |
| 894 | // Look in the first symbol table. |
| 895 | std::string name; |
| 896 | uint64_t name_offset; |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 897 | ASSERT_TRUE(elf->GetFunctionName(0x90010, &name, &name_offset)); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 898 | EXPECT_EQ("function_one", name); |
| 899 | EXPECT_EQ(16U, name_offset); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 900 | ASSERT_TRUE(elf->GetFunctionName(0xd0020, &name, &name_offset)); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 901 | EXPECT_EQ("function_two", name); |
| 902 | EXPECT_EQ(32U, name_offset); |
| 903 | } |
| 904 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 905 | TEST_F(ElfInterfaceTest, init_section_headers_32) { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 906 | InitSectionHeaders<Elf32_Ehdr, Elf32_Shdr, Elf32_Sym, ElfInterface32>(sizeof(Elf32_Shdr)); |
| 907 | } |
| 908 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 909 | TEST_F(ElfInterfaceTest, init_section_headers_64) { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 910 | InitSectionHeaders<Elf64_Ehdr, Elf64_Shdr, Elf64_Sym, ElfInterface64>(sizeof(Elf64_Shdr)); |
| 911 | } |
| 912 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 913 | TEST_F(ElfInterfaceTest, init_section_headers_non_std_entry_size_32) { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 914 | InitSectionHeaders<Elf32_Ehdr, Elf32_Shdr, Elf32_Sym, ElfInterface32>(0x100); |
| 915 | } |
| 916 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 917 | TEST_F(ElfInterfaceTest, init_section_headers_non_std_entry_size_64) { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 918 | InitSectionHeaders<Elf64_Ehdr, Elf64_Shdr, Elf64_Sym, ElfInterface64>(0x100); |
| 919 | } |
| 920 | |
| 921 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> |
| 922 | void ElfInterfaceTest::InitSectionHeadersOffsets() { |
| 923 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 924 | |
| 925 | uint64_t offset = 0x2000; |
| 926 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 927 | Ehdr ehdr = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 928 | ehdr.e_shoff = offset; |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 929 | ehdr.e_shnum = 7; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 930 | ehdr.e_shentsize = sizeof(Shdr); |
| 931 | ehdr.e_shstrndx = 2; |
| 932 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 933 | |
| 934 | offset += ehdr.e_shentsize; |
| 935 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 936 | Shdr shdr = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 937 | shdr.sh_type = SHT_PROGBITS; |
| 938 | shdr.sh_link = 2; |
| 939 | shdr.sh_name = 0x200; |
| 940 | shdr.sh_addr = 0x5000; |
| 941 | shdr.sh_offset = 0x5000; |
| 942 | shdr.sh_entsize = 0x100; |
| 943 | shdr.sh_size = 0x800; |
| 944 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 945 | offset += ehdr.e_shentsize; |
| 946 | |
| 947 | // The string data for section header names. |
| 948 | memset(&shdr, 0, sizeof(shdr)); |
| 949 | shdr.sh_type = SHT_STRTAB; |
| 950 | shdr.sh_name = 0x20000; |
| 951 | shdr.sh_offset = 0xf000; |
| 952 | shdr.sh_size = 0x1000; |
| 953 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 954 | offset += ehdr.e_shentsize; |
| 955 | |
| 956 | memset(&shdr, 0, sizeof(shdr)); |
| 957 | shdr.sh_type = SHT_PROGBITS; |
| 958 | shdr.sh_link = 2; |
| 959 | shdr.sh_name = 0x100; |
| 960 | shdr.sh_addr = 0x6000; |
| 961 | shdr.sh_offset = 0x6000; |
| 962 | shdr.sh_entsize = 0x100; |
| 963 | shdr.sh_size = 0x500; |
| 964 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 965 | offset += ehdr.e_shentsize; |
| 966 | |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 967 | memset(&shdr, 0, sizeof(shdr)); |
| 968 | shdr.sh_type = SHT_PROGBITS; |
| 969 | shdr.sh_link = 2; |
| 970 | shdr.sh_name = 0x300; |
| 971 | shdr.sh_addr = 0x7000; |
| 972 | shdr.sh_offset = 0x7000; |
| 973 | shdr.sh_entsize = 0x100; |
| 974 | shdr.sh_size = 0x800; |
| 975 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 976 | offset += ehdr.e_shentsize; |
| 977 | |
| 978 | memset(&shdr, 0, sizeof(shdr)); |
| 979 | shdr.sh_type = SHT_PROGBITS; |
| 980 | shdr.sh_link = 2; |
| 981 | shdr.sh_name = 0x400; |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 982 | shdr.sh_addr = 0xa000; |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 983 | shdr.sh_offset = 0xa000; |
| 984 | shdr.sh_entsize = 0x100; |
| 985 | shdr.sh_size = 0xf00; |
| 986 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 987 | offset += ehdr.e_shentsize; |
| 988 | |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 989 | memset(&shdr, 0, sizeof(shdr)); |
| 990 | shdr.sh_type = SHT_NOTE; |
| 991 | shdr.sh_name = 0x500; |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 992 | shdr.sh_addr = 0xb000; |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 993 | shdr.sh_offset = 0xb000; |
| 994 | shdr.sh_size = 0xf00; |
| 995 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 996 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 997 | memory_.SetMemory(0xf100, ".debug_frame", sizeof(".debug_frame")); |
| 998 | memory_.SetMemory(0xf200, ".gnu_debugdata", sizeof(".gnu_debugdata")); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 999 | memory_.SetMemory(0xf300, ".eh_frame", sizeof(".eh_frame")); |
| 1000 | memory_.SetMemory(0xf400, ".eh_frame_hdr", sizeof(".eh_frame_hdr")); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1001 | memory_.SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id")); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 1002 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1003 | int64_t load_bias = 0; |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 1004 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1005 | EXPECT_EQ(0, load_bias); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 1006 | EXPECT_EQ(0x6000U, elf->debug_frame_offset()); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1007 | EXPECT_EQ(0, elf->debug_frame_section_bias()); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 1008 | EXPECT_EQ(0x500U, elf->debug_frame_size()); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1009 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 1010 | EXPECT_EQ(0x5000U, elf->gnu_debugdata_offset()); |
| 1011 | EXPECT_EQ(0x800U, elf->gnu_debugdata_size()); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1012 | |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 1013 | EXPECT_EQ(0x7000U, elf->eh_frame_offset()); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1014 | EXPECT_EQ(0, elf->eh_frame_section_bias()); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 1015 | EXPECT_EQ(0x800U, elf->eh_frame_size()); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1016 | |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 1017 | EXPECT_EQ(0xa000U, elf->eh_frame_hdr_offset()); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1018 | EXPECT_EQ(0, elf->eh_frame_hdr_section_bias()); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 1019 | EXPECT_EQ(0xf00U, elf->eh_frame_hdr_size()); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1020 | |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1021 | EXPECT_EQ(0xb000U, elf->gnu_build_id_offset()); |
| 1022 | EXPECT_EQ(0xf00U, elf->gnu_build_id_size()); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1025 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_32) { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 1026 | InitSectionHeadersOffsets<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(); |
| 1027 | } |
| 1028 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1029 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_64) { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 1030 | InitSectionHeadersOffsets<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(); |
| 1031 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 1032 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1033 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> |
| 1034 | void ElfInterfaceTest::InitSectionHeadersOffsetsEhFrameSectionBias(uint64_t addr, uint64_t offset, |
| 1035 | int64_t expected_bias) { |
| 1036 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 1037 | |
| 1038 | uint64_t elf_offset = 0x2000; |
| 1039 | |
| 1040 | Ehdr ehdr = {}; |
| 1041 | ehdr.e_shoff = elf_offset; |
| 1042 | ehdr.e_shnum = 4; |
| 1043 | ehdr.e_shentsize = sizeof(Shdr); |
| 1044 | ehdr.e_shstrndx = 2; |
| 1045 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1046 | |
| 1047 | elf_offset += ehdr.e_shentsize; |
| 1048 | |
| 1049 | Shdr shdr = {}; |
| 1050 | shdr.sh_type = SHT_PROGBITS; |
| 1051 | shdr.sh_link = 2; |
| 1052 | shdr.sh_name = 0x200; |
| 1053 | shdr.sh_addr = 0x8000; |
| 1054 | shdr.sh_offset = 0x8000; |
| 1055 | shdr.sh_entsize = 0x100; |
| 1056 | shdr.sh_size = 0x800; |
| 1057 | memory_.SetMemory(elf_offset, &shdr, sizeof(shdr)); |
| 1058 | elf_offset += ehdr.e_shentsize; |
| 1059 | |
| 1060 | // The string data for section header names. |
| 1061 | memset(&shdr, 0, sizeof(shdr)); |
| 1062 | shdr.sh_type = SHT_STRTAB; |
| 1063 | shdr.sh_name = 0x20000; |
| 1064 | shdr.sh_offset = 0xf000; |
| 1065 | shdr.sh_size = 0x1000; |
| 1066 | memory_.SetMemory(elf_offset, &shdr, sizeof(shdr)); |
| 1067 | elf_offset += ehdr.e_shentsize; |
| 1068 | |
| 1069 | memset(&shdr, 0, sizeof(shdr)); |
| 1070 | shdr.sh_type = SHT_PROGBITS; |
| 1071 | shdr.sh_link = 2; |
| 1072 | shdr.sh_name = 0x100; |
| 1073 | shdr.sh_addr = addr; |
| 1074 | shdr.sh_offset = offset; |
| 1075 | shdr.sh_entsize = 0x100; |
| 1076 | shdr.sh_size = 0x500; |
| 1077 | memory_.SetMemory(elf_offset, &shdr, sizeof(shdr)); |
| 1078 | |
| 1079 | memory_.SetMemory(0xf100, ".eh_frame", sizeof(".eh_frame")); |
| 1080 | memory_.SetMemory(0xf200, ".eh_frame_hdr", sizeof(".eh_frame_hdr")); |
| 1081 | |
| 1082 | int64_t load_bias = 0; |
| 1083 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 1084 | EXPECT_EQ(0, load_bias); |
| 1085 | EXPECT_EQ(offset, elf->eh_frame_offset()); |
| 1086 | EXPECT_EQ(expected_bias, elf->eh_frame_section_bias()); |
| 1087 | EXPECT_EQ(0x500U, elf->eh_frame_size()); |
| 1088 | |
| 1089 | EXPECT_EQ(0x8000U, elf->eh_frame_hdr_offset()); |
| 1090 | EXPECT_EQ(0, elf->eh_frame_hdr_section_bias()); |
| 1091 | EXPECT_EQ(0x800U, elf->eh_frame_hdr_size()); |
| 1092 | } |
| 1093 | |
| 1094 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_section_bias_zero_32) { |
| 1095 | InitSectionHeadersOffsetsEhFrameSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(0x4000, |
| 1096 | 0x4000, 0); |
| 1097 | } |
| 1098 | |
| 1099 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_section_bias_zero_64) { |
| 1100 | InitSectionHeadersOffsetsEhFrameSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(0x6000, |
| 1101 | 0x6000, 0); |
| 1102 | } |
| 1103 | |
| 1104 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_section_bias_positive_32) { |
| 1105 | InitSectionHeadersOffsetsEhFrameSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>( |
| 1106 | 0x5000, 0x4000, 0x1000); |
| 1107 | } |
| 1108 | |
| 1109 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_section_bias_positive_64) { |
| 1110 | InitSectionHeadersOffsetsEhFrameSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>( |
| 1111 | 0x6000, 0x4000, 0x2000); |
| 1112 | } |
| 1113 | |
| 1114 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_section_bias_negative_32) { |
| 1115 | InitSectionHeadersOffsetsEhFrameSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>( |
| 1116 | 0x3000, 0x4000, -0x1000); |
| 1117 | } |
| 1118 | |
| 1119 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_section_bias_negative_64) { |
| 1120 | InitSectionHeadersOffsetsEhFrameSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>( |
| 1121 | 0x6000, 0x9000, -0x3000); |
| 1122 | } |
| 1123 | |
| 1124 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> |
| 1125 | void ElfInterfaceTest::InitSectionHeadersOffsetsEhFrameHdrSectionBias(uint64_t addr, |
| 1126 | uint64_t offset, |
| 1127 | int64_t expected_bias) { |
| 1128 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 1129 | |
| 1130 | uint64_t elf_offset = 0x2000; |
| 1131 | |
| 1132 | Ehdr ehdr = {}; |
| 1133 | ehdr.e_shoff = elf_offset; |
| 1134 | ehdr.e_shnum = 4; |
| 1135 | ehdr.e_shentsize = sizeof(Shdr); |
| 1136 | ehdr.e_shstrndx = 2; |
| 1137 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1138 | |
| 1139 | elf_offset += ehdr.e_shentsize; |
| 1140 | |
| 1141 | Shdr shdr = {}; |
| 1142 | shdr.sh_type = SHT_PROGBITS; |
| 1143 | shdr.sh_link = 2; |
| 1144 | shdr.sh_name = 0x200; |
| 1145 | shdr.sh_addr = addr; |
| 1146 | shdr.sh_offset = offset; |
| 1147 | shdr.sh_entsize = 0x100; |
| 1148 | shdr.sh_size = 0x800; |
| 1149 | memory_.SetMemory(elf_offset, &shdr, sizeof(shdr)); |
| 1150 | elf_offset += ehdr.e_shentsize; |
| 1151 | |
| 1152 | // The string data for section header names. |
| 1153 | memset(&shdr, 0, sizeof(shdr)); |
| 1154 | shdr.sh_type = SHT_STRTAB; |
| 1155 | shdr.sh_name = 0x20000; |
| 1156 | shdr.sh_offset = 0xf000; |
| 1157 | shdr.sh_size = 0x1000; |
| 1158 | memory_.SetMemory(elf_offset, &shdr, sizeof(shdr)); |
| 1159 | elf_offset += ehdr.e_shentsize; |
| 1160 | |
| 1161 | memset(&shdr, 0, sizeof(shdr)); |
| 1162 | shdr.sh_type = SHT_PROGBITS; |
| 1163 | shdr.sh_link = 2; |
| 1164 | shdr.sh_name = 0x100; |
| 1165 | shdr.sh_addr = 0x5000; |
| 1166 | shdr.sh_offset = 0x5000; |
| 1167 | shdr.sh_entsize = 0x100; |
| 1168 | shdr.sh_size = 0x500; |
| 1169 | memory_.SetMemory(elf_offset, &shdr, sizeof(shdr)); |
| 1170 | |
| 1171 | memory_.SetMemory(0xf100, ".eh_frame", sizeof(".eh_frame")); |
| 1172 | memory_.SetMemory(0xf200, ".eh_frame_hdr", sizeof(".eh_frame_hdr")); |
| 1173 | |
| 1174 | int64_t load_bias = 0; |
| 1175 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 1176 | EXPECT_EQ(0, load_bias); |
| 1177 | EXPECT_EQ(0x5000U, elf->eh_frame_offset()); |
| 1178 | EXPECT_EQ(0, elf->eh_frame_section_bias()); |
| 1179 | EXPECT_EQ(0x500U, elf->eh_frame_size()); |
| 1180 | EXPECT_EQ(offset, elf->eh_frame_hdr_offset()); |
| 1181 | EXPECT_EQ(expected_bias, elf->eh_frame_hdr_section_bias()); |
| 1182 | EXPECT_EQ(0x800U, elf->eh_frame_hdr_size()); |
| 1183 | } |
| 1184 | |
| 1185 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_hdr_section_bias_zero_32) { |
| 1186 | InitSectionHeadersOffsetsEhFrameHdrSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(0x9000, |
| 1187 | 0x9000, 0); |
| 1188 | } |
| 1189 | |
| 1190 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_hdr_section_bias_zero_64) { |
| 1191 | InitSectionHeadersOffsetsEhFrameHdrSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(0xa000, |
| 1192 | 0xa000, 0); |
| 1193 | } |
| 1194 | |
| 1195 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_hdr_section_bias_positive_32) { |
| 1196 | InitSectionHeadersOffsetsEhFrameHdrSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>( |
| 1197 | 0x9000, 0x4000, 0x5000); |
| 1198 | } |
| 1199 | |
| 1200 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_hdr_section_bias_positive_64) { |
| 1201 | InitSectionHeadersOffsetsEhFrameHdrSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>( |
| 1202 | 0x6000, 0x1000, 0x5000); |
| 1203 | } |
| 1204 | |
| 1205 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_hdr_section_bias_negative_32) { |
| 1206 | InitSectionHeadersOffsetsEhFrameHdrSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>( |
| 1207 | 0x3000, 0x5000, -0x2000); |
| 1208 | } |
| 1209 | |
| 1210 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_eh_frame_hdr_section_bias_negative_64) { |
| 1211 | InitSectionHeadersOffsetsEhFrameHdrSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>( |
| 1212 | 0x5000, 0x9000, -0x4000); |
| 1213 | } |
| 1214 | |
| 1215 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> |
| 1216 | void ElfInterfaceTest::InitSectionHeadersOffsetsDebugFrameSectionBias(uint64_t addr, |
| 1217 | uint64_t offset, |
| 1218 | int64_t expected_bias) { |
| 1219 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 1220 | |
| 1221 | uint64_t elf_offset = 0x2000; |
| 1222 | |
| 1223 | Ehdr ehdr = {}; |
| 1224 | ehdr.e_shoff = elf_offset; |
| 1225 | ehdr.e_shnum = 3; |
| 1226 | ehdr.e_shentsize = sizeof(Shdr); |
| 1227 | ehdr.e_shstrndx = 2; |
| 1228 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1229 | |
| 1230 | elf_offset += ehdr.e_shentsize; |
| 1231 | |
| 1232 | Shdr shdr = {}; |
| 1233 | shdr.sh_type = SHT_PROGBITS; |
| 1234 | shdr.sh_link = 2; |
| 1235 | shdr.sh_name = 0x100; |
| 1236 | shdr.sh_addr = addr; |
| 1237 | shdr.sh_offset = offset; |
| 1238 | shdr.sh_entsize = 0x100; |
| 1239 | shdr.sh_size = 0x800; |
| 1240 | memory_.SetMemory(elf_offset, &shdr, sizeof(shdr)); |
| 1241 | elf_offset += ehdr.e_shentsize; |
| 1242 | |
| 1243 | // The string data for section header names. |
| 1244 | memset(&shdr, 0, sizeof(shdr)); |
| 1245 | shdr.sh_type = SHT_STRTAB; |
| 1246 | shdr.sh_name = 0x20000; |
| 1247 | shdr.sh_offset = 0xf000; |
| 1248 | shdr.sh_size = 0x1000; |
| 1249 | memory_.SetMemory(elf_offset, &shdr, sizeof(shdr)); |
| 1250 | |
| 1251 | memory_.SetMemory(0xf100, ".debug_frame", sizeof(".debug_frame")); |
| 1252 | |
| 1253 | int64_t load_bias = 0; |
| 1254 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 1255 | EXPECT_EQ(0, load_bias); |
| 1256 | EXPECT_EQ(offset, elf->debug_frame_offset()); |
| 1257 | EXPECT_EQ(expected_bias, elf->debug_frame_section_bias()); |
| 1258 | EXPECT_EQ(0x800U, elf->debug_frame_size()); |
| 1259 | } |
| 1260 | |
| 1261 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_debug_frame_section_bias_zero_32) { |
| 1262 | InitSectionHeadersOffsetsDebugFrameSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(0x5000, |
| 1263 | 0x5000, 0); |
| 1264 | } |
| 1265 | |
| 1266 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_debug_frame_section_bias_zero_64) { |
| 1267 | InitSectionHeadersOffsetsDebugFrameSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(0xa000, |
| 1268 | 0xa000, 0); |
| 1269 | } |
| 1270 | |
| 1271 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_debug_frame_section_bias_positive_32) { |
| 1272 | InitSectionHeadersOffsetsDebugFrameSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>( |
| 1273 | 0x5000, 0x2000, 0x3000); |
| 1274 | } |
| 1275 | |
| 1276 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_debug_frame_section_bias_positive_64) { |
| 1277 | InitSectionHeadersOffsetsDebugFrameSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>( |
| 1278 | 0x7000, 0x1000, 0x6000); |
| 1279 | } |
| 1280 | |
| 1281 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_debug_frame_section_bias_negative_32) { |
| 1282 | InitSectionHeadersOffsetsDebugFrameSectionBias<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>( |
| 1283 | 0x6000, 0x7000, -0x1000); |
| 1284 | } |
| 1285 | |
| 1286 | TEST_F(ElfInterfaceTest, init_section_headers_offsets_debug_frame_section_bias_negative_64) { |
| 1287 | InitSectionHeadersOffsetsDebugFrameSectionBias<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>( |
| 1288 | 0x3000, 0x5000, -0x2000); |
| 1289 | } |
| 1290 | |
| 1291 | template <typename Ehdr, typename Phdr, typename ElfInterfaceType> |
| 1292 | void ElfInterfaceTest::CheckGnuEhFrame(uint64_t addr, uint64_t offset, int64_t expected_bias) { |
| 1293 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 1294 | |
| 1295 | Ehdr ehdr = {}; |
| 1296 | ehdr.e_phoff = 0x100; |
| 1297 | ehdr.e_phnum = 2; |
| 1298 | ehdr.e_phentsize = sizeof(Phdr); |
| 1299 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1300 | |
| 1301 | uint64_t phdr_offset = 0x100; |
| 1302 | |
| 1303 | Phdr phdr = {}; |
| 1304 | phdr.p_type = PT_LOAD; |
| 1305 | phdr.p_memsz = 0x10000; |
| 1306 | phdr.p_flags = PF_R | PF_X; |
| 1307 | phdr.p_align = 0x1000; |
| 1308 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); |
| 1309 | phdr_offset += sizeof(phdr); |
| 1310 | |
| 1311 | memset(&phdr, 0, sizeof(phdr)); |
| 1312 | phdr.p_type = PT_GNU_EH_FRAME; |
Christopher Ferris | 5838e53 | 2019-10-21 18:59:42 -0700 | [diff] [blame^] | 1313 | phdr.p_vaddr = addr; |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1314 | phdr.p_offset = offset; |
| 1315 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); |
| 1316 | |
| 1317 | int64_t load_bias = 0; |
| 1318 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 1319 | EXPECT_EQ(0, load_bias); |
| 1320 | EXPECT_EQ(expected_bias, elf->eh_frame_hdr_section_bias()); |
| 1321 | } |
| 1322 | |
| 1323 | TEST_F(ElfInterfaceTest, eh_frame_zero_section_bias_32) { |
| 1324 | ElfInterfaceTest::CheckGnuEhFrame<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(0x4000, 0x4000, 0); |
| 1325 | } |
| 1326 | |
| 1327 | TEST_F(ElfInterfaceTest, eh_frame_zero_section_bias_64) { |
| 1328 | ElfInterfaceTest::CheckGnuEhFrame<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(0x4000, 0x4000, 0); |
| 1329 | } |
| 1330 | |
| 1331 | TEST_F(ElfInterfaceTest, eh_frame_positive_section_bias_32) { |
| 1332 | ElfInterfaceTest::CheckGnuEhFrame<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(0x4000, 0x1000, 0x3000); |
| 1333 | } |
| 1334 | |
| 1335 | TEST_F(ElfInterfaceTest, eh_frame_positive_section_bias_64) { |
| 1336 | ElfInterfaceTest::CheckGnuEhFrame<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(0x4000, 0x1000, 0x3000); |
| 1337 | } |
| 1338 | |
| 1339 | TEST_F(ElfInterfaceTest, eh_frame_negative_section_bias_32) { |
| 1340 | ElfInterfaceTest::CheckGnuEhFrame<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(0x4000, 0x5000, |
| 1341 | -0x1000); |
| 1342 | } |
| 1343 | |
| 1344 | TEST_F(ElfInterfaceTest, eh_frame_negative_section_bias_64) { |
| 1345 | ElfInterfaceTest::CheckGnuEhFrame<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(0x4000, 0x5000, |
| 1346 | -0x1000); |
| 1347 | } |
| 1348 | |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1349 | TEST_F(ElfInterfaceTest, is_valid_pc_from_pt_load) { |
| 1350 | std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_)); |
| 1351 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1352 | Elf32_Ehdr ehdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1353 | ehdr.e_phoff = 0x100; |
| 1354 | ehdr.e_phnum = 1; |
| 1355 | ehdr.e_phentsize = sizeof(Elf32_Phdr); |
| 1356 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1357 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1358 | Elf32_Phdr phdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1359 | phdr.p_type = PT_LOAD; |
| 1360 | phdr.p_vaddr = 0; |
| 1361 | phdr.p_memsz = 0x10000; |
| 1362 | phdr.p_flags = PF_R | PF_X; |
| 1363 | phdr.p_align = 0x1000; |
| 1364 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 1365 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1366 | int64_t load_bias = 0; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1367 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1368 | EXPECT_EQ(0, load_bias); |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1369 | EXPECT_TRUE(elf->IsValidPc(0)); |
| 1370 | EXPECT_TRUE(elf->IsValidPc(0x5000)); |
| 1371 | EXPECT_TRUE(elf->IsValidPc(0xffff)); |
| 1372 | EXPECT_FALSE(elf->IsValidPc(0x10000)); |
| 1373 | } |
| 1374 | |
| 1375 | TEST_F(ElfInterfaceTest, is_valid_pc_from_pt_load_non_zero_load_bias) { |
| 1376 | std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_)); |
| 1377 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1378 | Elf32_Ehdr ehdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1379 | ehdr.e_phoff = 0x100; |
| 1380 | ehdr.e_phnum = 1; |
| 1381 | ehdr.e_phentsize = sizeof(Elf32_Phdr); |
| 1382 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1383 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1384 | Elf32_Phdr phdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1385 | phdr.p_type = PT_LOAD; |
| 1386 | phdr.p_vaddr = 0x2000; |
| 1387 | phdr.p_memsz = 0x10000; |
| 1388 | phdr.p_flags = PF_R | PF_X; |
| 1389 | phdr.p_align = 0x1000; |
| 1390 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 1391 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1392 | int64_t load_bias = 0; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1393 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1394 | EXPECT_EQ(0x2000, load_bias); |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1395 | EXPECT_FALSE(elf->IsValidPc(0)); |
| 1396 | EXPECT_FALSE(elf->IsValidPc(0x1000)); |
| 1397 | EXPECT_FALSE(elf->IsValidPc(0x1fff)); |
| 1398 | EXPECT_TRUE(elf->IsValidPc(0x2000)); |
| 1399 | EXPECT_TRUE(elf->IsValidPc(0x5000)); |
| 1400 | EXPECT_TRUE(elf->IsValidPc(0x11fff)); |
| 1401 | EXPECT_FALSE(elf->IsValidPc(0x12000)); |
| 1402 | } |
| 1403 | |
| 1404 | TEST_F(ElfInterfaceTest, is_valid_pc_from_debug_frame) { |
| 1405 | std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_)); |
| 1406 | |
| 1407 | uint64_t sh_offset = 0x100; |
| 1408 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1409 | Elf32_Ehdr ehdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1410 | ehdr.e_shstrndx = 1; |
| 1411 | ehdr.e_shoff = sh_offset; |
| 1412 | ehdr.e_shentsize = sizeof(Elf32_Shdr); |
| 1413 | ehdr.e_shnum = 3; |
| 1414 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1415 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1416 | Elf32_Shdr shdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1417 | shdr.sh_type = SHT_NULL; |
| 1418 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 1419 | |
| 1420 | sh_offset += sizeof(shdr); |
| 1421 | memset(&shdr, 0, sizeof(shdr)); |
| 1422 | shdr.sh_type = SHT_STRTAB; |
| 1423 | shdr.sh_name = 1; |
| 1424 | shdr.sh_offset = 0x500; |
| 1425 | shdr.sh_size = 0x100; |
| 1426 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 1427 | memory_.SetMemory(0x500, ".debug_frame"); |
| 1428 | |
| 1429 | sh_offset += sizeof(shdr); |
| 1430 | memset(&shdr, 0, sizeof(shdr)); |
| 1431 | shdr.sh_type = SHT_PROGBITS; |
| 1432 | shdr.sh_name = 0; |
| 1433 | shdr.sh_addr = 0x600; |
| 1434 | shdr.sh_offset = 0x600; |
| 1435 | shdr.sh_size = 0x200; |
| 1436 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 1437 | |
| 1438 | // CIE 32. |
| 1439 | memory_.SetData32(0x600, 0xfc); |
| 1440 | memory_.SetData32(0x604, 0xffffffff); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 1441 | memory_.SetMemory(0x608, std::vector<uint8_t>{1, '\0', 4, 4, 1}); |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1442 | |
| 1443 | // FDE 32. |
| 1444 | memory_.SetData32(0x700, 0xfc); |
| 1445 | memory_.SetData32(0x704, 0); |
| 1446 | memory_.SetData32(0x708, 0x2100); |
| 1447 | memory_.SetData32(0x70c, 0x200); |
| 1448 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1449 | int64_t load_bias = 0; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1450 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1451 | elf->InitHeaders(); |
| 1452 | EXPECT_EQ(0, load_bias); |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1453 | EXPECT_FALSE(elf->IsValidPc(0)); |
| 1454 | EXPECT_FALSE(elf->IsValidPc(0x20ff)); |
| 1455 | EXPECT_TRUE(elf->IsValidPc(0x2100)); |
| 1456 | EXPECT_TRUE(elf->IsValidPc(0x2200)); |
| 1457 | EXPECT_TRUE(elf->IsValidPc(0x22ff)); |
| 1458 | EXPECT_FALSE(elf->IsValidPc(0x2300)); |
| 1459 | } |
| 1460 | |
| 1461 | TEST_F(ElfInterfaceTest, is_valid_pc_from_eh_frame) { |
| 1462 | std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_)); |
| 1463 | |
| 1464 | uint64_t sh_offset = 0x100; |
| 1465 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1466 | Elf32_Ehdr ehdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1467 | ehdr.e_shstrndx = 1; |
| 1468 | ehdr.e_shoff = sh_offset; |
| 1469 | ehdr.e_shentsize = sizeof(Elf32_Shdr); |
| 1470 | ehdr.e_shnum = 3; |
| 1471 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1472 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1473 | Elf32_Shdr shdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1474 | shdr.sh_type = SHT_NULL; |
| 1475 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 1476 | |
| 1477 | sh_offset += sizeof(shdr); |
| 1478 | memset(&shdr, 0, sizeof(shdr)); |
| 1479 | shdr.sh_type = SHT_STRTAB; |
| 1480 | shdr.sh_name = 1; |
| 1481 | shdr.sh_offset = 0x500; |
| 1482 | shdr.sh_size = 0x100; |
| 1483 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 1484 | memory_.SetMemory(0x500, ".eh_frame"); |
| 1485 | |
| 1486 | sh_offset += sizeof(shdr); |
| 1487 | memset(&shdr, 0, sizeof(shdr)); |
| 1488 | shdr.sh_type = SHT_PROGBITS; |
| 1489 | shdr.sh_name = 0; |
| 1490 | shdr.sh_addr = 0x600; |
| 1491 | shdr.sh_offset = 0x600; |
| 1492 | shdr.sh_size = 0x200; |
| 1493 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 1494 | |
| 1495 | // CIE 32. |
| 1496 | memory_.SetData32(0x600, 0xfc); |
| 1497 | memory_.SetData32(0x604, 0); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 1498 | memory_.SetMemory(0x608, std::vector<uint8_t>{1, '\0', 4, 4, 1}); |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1499 | |
| 1500 | // FDE 32. |
| 1501 | memory_.SetData32(0x700, 0xfc); |
| 1502 | memory_.SetData32(0x704, 0x104); |
| 1503 | memory_.SetData32(0x708, 0x20f8); |
| 1504 | memory_.SetData32(0x70c, 0x200); |
| 1505 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1506 | int64_t load_bias = 0; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1507 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1508 | elf->InitHeaders(); |
| 1509 | EXPECT_EQ(0, load_bias); |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1510 | EXPECT_FALSE(elf->IsValidPc(0)); |
| 1511 | EXPECT_FALSE(elf->IsValidPc(0x27ff)); |
| 1512 | EXPECT_TRUE(elf->IsValidPc(0x2800)); |
| 1513 | EXPECT_TRUE(elf->IsValidPc(0x2900)); |
| 1514 | EXPECT_TRUE(elf->IsValidPc(0x29ff)); |
| 1515 | EXPECT_FALSE(elf->IsValidPc(0x2a00)); |
| 1516 | } |
| 1517 | |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1518 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 1519 | void ElfInterfaceTest::BuildID() { |
| 1520 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 1521 | |
| 1522 | uint64_t offset = 0x2000; |
| 1523 | |
| 1524 | Ehdr ehdr = {}; |
| 1525 | ehdr.e_shoff = offset; |
| 1526 | ehdr.e_shnum = 3; |
| 1527 | ehdr.e_shentsize = sizeof(Shdr); |
| 1528 | ehdr.e_shstrndx = 2; |
| 1529 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1530 | |
| 1531 | offset += ehdr.e_shentsize; |
| 1532 | |
| 1533 | char note_section[128]; |
| 1534 | Nhdr note_header = {}; |
| 1535 | note_header.n_namesz = 4; // "GNU" |
Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 1536 | note_header.n_descsz = 7; // "BUILDID" |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1537 | note_header.n_type = NT_GNU_BUILD_ID; |
| 1538 | memcpy(¬e_section, ¬e_header, sizeof(note_header)); |
| 1539 | size_t note_offset = sizeof(note_header); |
Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 1540 | // The note information contains the GNU and trailing '\0'. |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1541 | memcpy(¬e_section[note_offset], "GNU", sizeof("GNU")); |
| 1542 | note_offset += sizeof("GNU"); |
Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 1543 | // This part of the note does not contain any trailing '\0'. |
| 1544 | memcpy(¬e_section[note_offset], "BUILDID", 7); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1545 | |
| 1546 | Shdr shdr = {}; |
| 1547 | shdr.sh_type = SHT_NOTE; |
| 1548 | shdr.sh_name = 0x500; |
| 1549 | shdr.sh_offset = 0xb000; |
| 1550 | shdr.sh_size = sizeof(note_section); |
| 1551 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 1552 | offset += ehdr.e_shentsize; |
| 1553 | |
| 1554 | // The string data for section header names. |
| 1555 | memset(&shdr, 0, sizeof(shdr)); |
| 1556 | shdr.sh_type = SHT_STRTAB; |
| 1557 | shdr.sh_name = 0x20000; |
| 1558 | shdr.sh_offset = 0xf000; |
| 1559 | shdr.sh_size = 0x1000; |
| 1560 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1561 | |
| 1562 | memory_.SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id")); |
| 1563 | memory_.SetMemory(0xb000, note_section, sizeof(note_section)); |
| 1564 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1565 | int64_t load_bias = 0; |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1566 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 1567 | ASSERT_EQ("BUILDID", elf->GetBuildID()); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1570 | TEST_F(ElfInterfaceTest, build_id_32) { |
| 1571 | BuildID<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr, ElfInterface32>(); |
| 1572 | } |
| 1573 | |
| 1574 | TEST_F(ElfInterfaceTest, build_id_64) { |
| 1575 | BuildID<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr, ElfInterface64>(); |
| 1576 | } |
| 1577 | |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1578 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 1579 | void ElfInterfaceTest::BuildIDTwoNotes() { |
| 1580 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 1581 | |
| 1582 | uint64_t offset = 0x2000; |
| 1583 | |
| 1584 | Ehdr ehdr = {}; |
| 1585 | ehdr.e_shoff = offset; |
| 1586 | ehdr.e_shnum = 3; |
| 1587 | ehdr.e_shentsize = sizeof(Shdr); |
| 1588 | ehdr.e_shstrndx = 2; |
| 1589 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1590 | |
| 1591 | offset += ehdr.e_shentsize; |
| 1592 | |
| 1593 | char note_section[128]; |
| 1594 | Nhdr note_header = {}; |
| 1595 | note_header.n_namesz = 8; // "WRONG" aligned to 4 |
Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 1596 | note_header.n_descsz = 7; // "BUILDID" |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1597 | note_header.n_type = NT_GNU_BUILD_ID; |
| 1598 | memcpy(¬e_section, ¬e_header, sizeof(note_header)); |
| 1599 | size_t note_offset = sizeof(note_header); |
| 1600 | memcpy(¬e_section[note_offset], "WRONG", sizeof("WRONG")); |
| 1601 | note_offset += 8; |
Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 1602 | // This part of the note does not contain any trailing '\0'. |
| 1603 | memcpy(¬e_section[note_offset], "BUILDID", 7); |
| 1604 | note_offset += 8; |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1605 | |
| 1606 | note_header.n_namesz = 4; // "GNU" |
Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 1607 | note_header.n_descsz = 7; // "BUILDID" |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1608 | note_header.n_type = NT_GNU_BUILD_ID; |
| 1609 | memcpy(¬e_section[note_offset], ¬e_header, sizeof(note_header)); |
| 1610 | note_offset += sizeof(note_header); |
Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 1611 | // The note information contains the GNU and trailing '\0'. |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1612 | memcpy(¬e_section[note_offset], "GNU", sizeof("GNU")); |
| 1613 | note_offset += sizeof("GNU"); |
Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 1614 | // This part of the note does not contain any trailing '\0'. |
| 1615 | memcpy(¬e_section[note_offset], "BUILDID", 7); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1616 | |
| 1617 | Shdr shdr = {}; |
| 1618 | shdr.sh_type = SHT_NOTE; |
| 1619 | shdr.sh_name = 0x500; |
| 1620 | shdr.sh_offset = 0xb000; |
| 1621 | shdr.sh_size = sizeof(note_section); |
| 1622 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 1623 | offset += ehdr.e_shentsize; |
| 1624 | |
| 1625 | // The string data for section header names. |
| 1626 | memset(&shdr, 0, sizeof(shdr)); |
| 1627 | shdr.sh_type = SHT_STRTAB; |
| 1628 | shdr.sh_name = 0x20000; |
| 1629 | shdr.sh_offset = 0xf000; |
| 1630 | shdr.sh_size = 0x1000; |
| 1631 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1632 | |
| 1633 | memory_.SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id")); |
| 1634 | memory_.SetMemory(0xb000, note_section, sizeof(note_section)); |
| 1635 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1636 | int64_t load_bias = 0; |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1637 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 1638 | ASSERT_EQ("BUILDID", elf->GetBuildID()); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1641 | TEST_F(ElfInterfaceTest, build_id_two_notes_32) { |
| 1642 | BuildIDTwoNotes<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr, ElfInterface32>(); |
| 1643 | } |
| 1644 | |
| 1645 | TEST_F(ElfInterfaceTest, build_id_two_notes_64) { |
| 1646 | BuildIDTwoNotes<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr, ElfInterface64>(); |
| 1647 | } |
| 1648 | |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1649 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 1650 | void ElfInterfaceTest::BuildIDSectionTooSmallForName () { |
| 1651 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 1652 | |
| 1653 | uint64_t offset = 0x2000; |
| 1654 | |
| 1655 | Ehdr ehdr = {}; |
| 1656 | ehdr.e_shoff = offset; |
| 1657 | ehdr.e_shnum = 3; |
| 1658 | ehdr.e_shentsize = sizeof(Shdr); |
| 1659 | ehdr.e_shstrndx = 2; |
| 1660 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1661 | |
| 1662 | offset += ehdr.e_shentsize; |
| 1663 | |
| 1664 | char note_section[128]; |
| 1665 | Nhdr note_header = {}; |
| 1666 | note_header.n_namesz = 4; // "GNU" |
Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 1667 | note_header.n_descsz = 7; // "BUILDID" |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1668 | note_header.n_type = NT_GNU_BUILD_ID; |
| 1669 | memcpy(¬e_section, ¬e_header, sizeof(note_header)); |
| 1670 | size_t note_offset = sizeof(note_header); |
Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 1671 | // The note information contains the GNU and trailing '\0'. |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1672 | memcpy(¬e_section[note_offset], "GNU", sizeof("GNU")); |
| 1673 | note_offset += sizeof("GNU"); |
Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 1674 | // This part of the note does not contain any trailing '\0'. |
| 1675 | memcpy(¬e_section[note_offset], "BUILDID", 7); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1676 | |
| 1677 | Shdr shdr = {}; |
| 1678 | shdr.sh_type = SHT_NOTE; |
| 1679 | shdr.sh_name = 0x500; |
| 1680 | shdr.sh_offset = 0xb000; |
| 1681 | shdr.sh_size = sizeof(note_header) + 1; |
| 1682 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 1683 | offset += ehdr.e_shentsize; |
| 1684 | |
| 1685 | // The string data for section header names. |
| 1686 | memset(&shdr, 0, sizeof(shdr)); |
| 1687 | shdr.sh_type = SHT_STRTAB; |
| 1688 | shdr.sh_name = 0x20000; |
| 1689 | shdr.sh_offset = 0xf000; |
| 1690 | shdr.sh_size = 0x1000; |
| 1691 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1692 | |
| 1693 | memory_.SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id")); |
| 1694 | memory_.SetMemory(0xb000, note_section, sizeof(note_section)); |
| 1695 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1696 | int64_t load_bias = 0; |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1697 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 1698 | ASSERT_EQ("", elf->GetBuildID()); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1701 | TEST_F(ElfInterfaceTest, build_id_section_too_small_for_name_32) { |
| 1702 | BuildIDSectionTooSmallForName<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr, ElfInterface32>(); |
| 1703 | } |
| 1704 | |
| 1705 | TEST_F(ElfInterfaceTest, build_id_section_too_small_for_name_64) { |
| 1706 | BuildIDSectionTooSmallForName<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr, ElfInterface64>(); |
| 1707 | } |
| 1708 | |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1709 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 1710 | void ElfInterfaceTest::BuildIDSectionTooSmallForDesc () { |
| 1711 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 1712 | |
| 1713 | uint64_t offset = 0x2000; |
| 1714 | |
| 1715 | Ehdr ehdr = {}; |
| 1716 | ehdr.e_shoff = offset; |
| 1717 | ehdr.e_shnum = 3; |
| 1718 | ehdr.e_shentsize = sizeof(Shdr); |
| 1719 | ehdr.e_shstrndx = 2; |
| 1720 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1721 | |
| 1722 | offset += ehdr.e_shentsize; |
| 1723 | |
| 1724 | char note_section[128]; |
| 1725 | Nhdr note_header = {}; |
| 1726 | note_header.n_namesz = 4; // "GNU" |
Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 1727 | note_header.n_descsz = 7; // "BUILDID" |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1728 | note_header.n_type = NT_GNU_BUILD_ID; |
| 1729 | memcpy(¬e_section, ¬e_header, sizeof(note_header)); |
| 1730 | size_t note_offset = sizeof(note_header); |
Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 1731 | // The note information contains the GNU and trailing '\0'. |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1732 | memcpy(¬e_section[note_offset], "GNU", sizeof("GNU")); |
| 1733 | note_offset += sizeof("GNU"); |
Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 1734 | // This part of the note does not contain any trailing '\0'. |
| 1735 | memcpy(¬e_section[note_offset], "BUILDID", 7); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1736 | |
| 1737 | Shdr shdr = {}; |
| 1738 | shdr.sh_type = SHT_NOTE; |
| 1739 | shdr.sh_name = 0x500; |
| 1740 | shdr.sh_offset = 0xb000; |
| 1741 | shdr.sh_size = sizeof(note_header) + sizeof("GNU") + 1; |
| 1742 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 1743 | offset += ehdr.e_shentsize; |
| 1744 | |
| 1745 | // The string data for section header names. |
| 1746 | memset(&shdr, 0, sizeof(shdr)); |
| 1747 | shdr.sh_type = SHT_STRTAB; |
| 1748 | shdr.sh_name = 0x20000; |
| 1749 | shdr.sh_offset = 0xf000; |
| 1750 | shdr.sh_size = 0x1000; |
| 1751 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1752 | |
| 1753 | memory_.SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id")); |
| 1754 | memory_.SetMemory(0xb000, note_section, sizeof(note_section)); |
| 1755 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1756 | int64_t load_bias = 0; |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1757 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 1758 | ASSERT_EQ("", elf->GetBuildID()); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1761 | TEST_F(ElfInterfaceTest, build_id_section_too_small_for_desc_32) { |
| 1762 | BuildIDSectionTooSmallForDesc<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr, ElfInterface32>(); |
| 1763 | } |
| 1764 | |
| 1765 | TEST_F(ElfInterfaceTest, build_id_section_too_small_for_desc_64) { |
| 1766 | BuildIDSectionTooSmallForDesc<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr, ElfInterface64>(); |
| 1767 | } |
| 1768 | |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1769 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 1770 | void ElfInterfaceTest::BuildIDSectionTooSmallForHeader () { |
| 1771 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 1772 | |
| 1773 | uint64_t offset = 0x2000; |
| 1774 | |
| 1775 | Ehdr ehdr = {}; |
| 1776 | ehdr.e_shoff = offset; |
| 1777 | ehdr.e_shnum = 3; |
| 1778 | ehdr.e_shentsize = sizeof(Shdr); |
| 1779 | ehdr.e_shstrndx = 2; |
| 1780 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1781 | |
| 1782 | offset += ehdr.e_shentsize; |
| 1783 | |
| 1784 | char note_section[128]; |
| 1785 | Nhdr note_header = {}; |
| 1786 | note_header.n_namesz = 4; // "GNU" |
Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 1787 | note_header.n_descsz = 7; // "BUILDID" |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1788 | note_header.n_type = NT_GNU_BUILD_ID; |
| 1789 | memcpy(¬e_section, ¬e_header, sizeof(note_header)); |
| 1790 | size_t note_offset = sizeof(note_header); |
Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 1791 | // The note information contains the GNU and trailing '\0'. |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1792 | memcpy(¬e_section[note_offset], "GNU", sizeof("GNU")); |
| 1793 | note_offset += sizeof("GNU"); |
Christopher Ferris | 1760b45 | 2019-04-03 14:14:30 -0700 | [diff] [blame] | 1794 | // This part of the note does not contain any trailing '\0'. |
| 1795 | memcpy(¬e_section[note_offset], "BUILDID", 7); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1796 | |
| 1797 | Shdr shdr = {}; |
| 1798 | shdr.sh_type = SHT_NOTE; |
| 1799 | shdr.sh_name = 0x500; |
| 1800 | shdr.sh_offset = 0xb000; |
| 1801 | shdr.sh_size = sizeof(note_header) - 1; |
| 1802 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 1803 | offset += ehdr.e_shentsize; |
| 1804 | |
| 1805 | // The string data for section header names. |
| 1806 | memset(&shdr, 0, sizeof(shdr)); |
| 1807 | shdr.sh_type = SHT_STRTAB; |
| 1808 | shdr.sh_name = 0x20000; |
| 1809 | shdr.sh_offset = 0xf000; |
| 1810 | shdr.sh_size = 0x1000; |
| 1811 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1812 | |
| 1813 | memory_.SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id")); |
| 1814 | memory_.SetMemory(0xb000, note_section, sizeof(note_section)); |
| 1815 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1816 | int64_t load_bias = 0; |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1817 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 1818 | ASSERT_EQ("", elf->GetBuildID()); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1821 | TEST_F(ElfInterfaceTest, build_id_section_too_small_for_header_32) { |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1822 | BuildIDSectionTooSmallForHeader<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr, ElfInterface32>(); |
| 1823 | } |
| 1824 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1825 | TEST_F(ElfInterfaceTest, build_id_section_too_small_for_header_64) { |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1826 | BuildIDSectionTooSmallForHeader<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr, ElfInterface64>(); |
| 1827 | } |
| 1828 | |
Christopher Ferris | 6c8ac56 | 2019-10-02 17:41:16 -0700 | [diff] [blame] | 1829 | template <typename Ehdr, typename Phdr, typename ElfInterfaceType> |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1830 | void ElfInterfaceTest::CheckLoadBiasInFirstPhdr(int64_t load_bias) { |
Christopher Ferris | 6c8ac56 | 2019-10-02 17:41:16 -0700 | [diff] [blame] | 1831 | Ehdr ehdr = {}; |
| 1832 | ehdr.e_phoff = 0x100; |
| 1833 | ehdr.e_phnum = 2; |
| 1834 | ehdr.e_phentsize = sizeof(Phdr); |
| 1835 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1836 | |
| 1837 | Phdr phdr = {}; |
| 1838 | phdr.p_type = PT_LOAD; |
| 1839 | phdr.p_offset = 0; |
| 1840 | phdr.p_vaddr = load_bias; |
| 1841 | phdr.p_memsz = 0x10000; |
| 1842 | phdr.p_flags = PF_R | PF_X; |
| 1843 | phdr.p_align = 0x1000; |
| 1844 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 1845 | |
| 1846 | memset(&phdr, 0, sizeof(phdr)); |
| 1847 | phdr.p_type = PT_LOAD; |
| 1848 | phdr.p_offset = 0x1000; |
| 1849 | phdr.p_memsz = 0x2000; |
| 1850 | phdr.p_flags = PF_R | PF_X; |
| 1851 | phdr.p_align = 0x1000; |
| 1852 | memory_.SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr)); |
| 1853 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1854 | int64_t static_load_bias = ElfInterface::GetLoadBias<Ehdr, Phdr>(&memory_); |
Christopher Ferris | 6c8ac56 | 2019-10-02 17:41:16 -0700 | [diff] [blame] | 1855 | ASSERT_EQ(load_bias, static_load_bias); |
| 1856 | |
| 1857 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1858 | int64_t init_load_bias = 0; |
Christopher Ferris | 6c8ac56 | 2019-10-02 17:41:16 -0700 | [diff] [blame] | 1859 | ASSERT_TRUE(elf->Init(&init_load_bias)); |
| 1860 | ASSERT_EQ(init_load_bias, static_load_bias); |
| 1861 | } |
| 1862 | |
| 1863 | TEST_F(ElfInterfaceTest, get_load_bias_zero_32) { |
| 1864 | CheckLoadBiasInFirstPhdr<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(0); |
| 1865 | } |
| 1866 | |
| 1867 | TEST_F(ElfInterfaceTest, get_load_bias_zero_64) { |
| 1868 | CheckLoadBiasInFirstPhdr<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(0); |
| 1869 | } |
| 1870 | |
| 1871 | TEST_F(ElfInterfaceTest, get_load_bias_non_zero_32) { |
| 1872 | CheckLoadBiasInFirstPhdr<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(0x1000); |
| 1873 | } |
| 1874 | |
| 1875 | TEST_F(ElfInterfaceTest, get_load_bias_non_zero_64) { |
| 1876 | CheckLoadBiasInFirstPhdr<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(0x1000); |
| 1877 | } |
| 1878 | |
| 1879 | template <typename Ehdr, typename Phdr, typename ElfInterfaceType> |
| 1880 | void ElfInterfaceTest::CheckLoadBiasInFirstExecPhdr(uint64_t offset, uint64_t vaddr, |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1881 | int64_t load_bias) { |
Christopher Ferris | 6c8ac56 | 2019-10-02 17:41:16 -0700 | [diff] [blame] | 1882 | Ehdr ehdr = {}; |
| 1883 | ehdr.e_phoff = 0x100; |
| 1884 | ehdr.e_phnum = 3; |
| 1885 | ehdr.e_phentsize = sizeof(Phdr); |
| 1886 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1887 | |
| 1888 | Phdr phdr = {}; |
| 1889 | phdr.p_type = PT_LOAD; |
| 1890 | phdr.p_memsz = 0x10000; |
| 1891 | phdr.p_flags = PF_R; |
| 1892 | phdr.p_align = 0x1000; |
| 1893 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 1894 | |
| 1895 | memset(&phdr, 0, sizeof(phdr)); |
| 1896 | phdr.p_type = PT_LOAD; |
| 1897 | phdr.p_offset = offset; |
| 1898 | phdr.p_vaddr = vaddr; |
| 1899 | phdr.p_memsz = 0x2000; |
| 1900 | phdr.p_flags = PF_R | PF_X; |
| 1901 | phdr.p_align = 0x1000; |
| 1902 | memory_.SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr)); |
| 1903 | |
| 1904 | // Second executable load should be ignored for load bias computation. |
| 1905 | memset(&phdr, 0, sizeof(phdr)); |
| 1906 | phdr.p_type = PT_LOAD; |
| 1907 | phdr.p_offset = 0x1234; |
| 1908 | phdr.p_vaddr = 0x2000; |
| 1909 | phdr.p_memsz = 0x2000; |
| 1910 | phdr.p_flags = PF_R | PF_X; |
| 1911 | phdr.p_align = 0x1000; |
| 1912 | memory_.SetMemory(0x200 + sizeof(phdr), &phdr, sizeof(phdr)); |
| 1913 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1914 | int64_t static_load_bias = ElfInterface::GetLoadBias<Ehdr, Phdr>(&memory_); |
Christopher Ferris | 6c8ac56 | 2019-10-02 17:41:16 -0700 | [diff] [blame] | 1915 | ASSERT_EQ(load_bias, static_load_bias); |
| 1916 | |
| 1917 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1918 | int64_t init_load_bias = 0; |
Christopher Ferris | 6c8ac56 | 2019-10-02 17:41:16 -0700 | [diff] [blame] | 1919 | ASSERT_TRUE(elf->Init(&init_load_bias)); |
| 1920 | ASSERT_EQ(init_load_bias, static_load_bias); |
| 1921 | } |
| 1922 | |
| 1923 | TEST_F(ElfInterfaceTest, get_load_bias_exec_zero_32) { |
| 1924 | CheckLoadBiasInFirstExecPhdr<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(0x1000, 0x1000, 0); |
| 1925 | } |
| 1926 | |
| 1927 | TEST_F(ElfInterfaceTest, get_load_bias_exec_zero_64) { |
| 1928 | CheckLoadBiasInFirstExecPhdr<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(0x1000, 0x1000, 0); |
| 1929 | } |
| 1930 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1931 | TEST_F(ElfInterfaceTest, get_load_bias_exec_positive_32) { |
Christopher Ferris | 6c8ac56 | 2019-10-02 17:41:16 -0700 | [diff] [blame] | 1932 | CheckLoadBiasInFirstExecPhdr<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(0x1000, 0x4000, 0x3000); |
| 1933 | } |
| 1934 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1935 | TEST_F(ElfInterfaceTest, get_load_bias_exec_positive_64) { |
Christopher Ferris | 6c8ac56 | 2019-10-02 17:41:16 -0700 | [diff] [blame] | 1936 | CheckLoadBiasInFirstExecPhdr<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(0x1000, 0x4000, 0x3000); |
| 1937 | } |
| 1938 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1939 | TEST_F(ElfInterfaceTest, get_load_bias_exec_negative_32) { |
| 1940 | CheckLoadBiasInFirstExecPhdr<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(0x5000, 0x1000, -0x4000); |
Christopher Ferris | 6c8ac56 | 2019-10-02 17:41:16 -0700 | [diff] [blame] | 1941 | } |
| 1942 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 1943 | TEST_F(ElfInterfaceTest, get_load_bias_exec_negative_64) { |
| 1944 | CheckLoadBiasInFirstExecPhdr<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(0x5000, 0x1000, -0x4000); |
Christopher Ferris | 6c8ac56 | 2019-10-02 17:41:16 -0700 | [diff] [blame] | 1945 | } |
| 1946 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 1947 | } // namespace unwindstack |