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 | |
| 115 | template <typename Sym> |
| 116 | void InitSym(uint64_t offset, uint32_t value, uint32_t size, uint32_t name_offset, |
| 117 | uint64_t sym_offset, const char* name); |
| 118 | |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 119 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 120 | void BuildID(); |
| 121 | |
| 122 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 123 | void BuildIDTwoNotes(); |
| 124 | |
| 125 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 126 | void BuildIDSectionTooSmallForName(); |
| 127 | |
| 128 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 129 | void BuildIDSectionTooSmallForDesc(); |
| 130 | |
| 131 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 132 | void BuildIDSectionTooSmallForHeader(); |
| 133 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 134 | MemoryFake memory_; |
| 135 | }; |
| 136 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 137 | template <typename Sym> |
| 138 | void ElfInterfaceTest::InitSym(uint64_t offset, uint32_t value, uint32_t size, uint32_t name_offset, |
| 139 | uint64_t sym_offset, const char* name) { |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 140 | Sym sym = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 141 | sym.st_info = STT_FUNC; |
| 142 | sym.st_value = value; |
| 143 | sym.st_size = size; |
| 144 | sym.st_name = name_offset; |
| 145 | sym.st_shndx = SHN_COMMON; |
| 146 | |
| 147 | memory_.SetMemory(offset, &sym, sizeof(sym)); |
| 148 | memory_.SetMemory(sym_offset + name_offset, name, strlen(name) + 1); |
| 149 | } |
| 150 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 151 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> |
| 152 | void ElfInterfaceTest::SinglePtLoad() { |
| 153 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 154 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 155 | Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 156 | ehdr.e_phoff = 0x100; |
| 157 | ehdr.e_phnum = 1; |
| 158 | ehdr.e_phentsize = sizeof(Phdr); |
| 159 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 160 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 161 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 162 | phdr.p_type = PT_LOAD; |
| 163 | phdr.p_vaddr = 0x2000; |
| 164 | phdr.p_memsz = 0x10000; |
| 165 | phdr.p_flags = PF_R | PF_X; |
| 166 | phdr.p_align = 0x1000; |
| 167 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 168 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 169 | uint64_t load_bias = 0; |
| 170 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 171 | EXPECT_EQ(0x2000U, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 172 | |
| 173 | const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads(); |
| 174 | ASSERT_EQ(1U, pt_loads.size()); |
| 175 | LoadInfo load_data = pt_loads.at(0); |
| 176 | ASSERT_EQ(0U, load_data.offset); |
| 177 | ASSERT_EQ(0x2000U, load_data.table_offset); |
| 178 | ASSERT_EQ(0x10000U, load_data.table_size); |
| 179 | } |
| 180 | |
| 181 | TEST_F(ElfInterfaceTest, elf32_single_pt_load) { |
| 182 | SinglePtLoad<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); |
| 183 | } |
| 184 | |
| 185 | TEST_F(ElfInterfaceTest, elf64_single_pt_load) { |
| 186 | SinglePtLoad<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); |
| 187 | } |
| 188 | |
| 189 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> |
| 190 | void ElfInterfaceTest::MultipleExecutablePtLoads() { |
| 191 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 192 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 193 | Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 194 | ehdr.e_phoff = 0x100; |
| 195 | ehdr.e_phnum = 3; |
| 196 | ehdr.e_phentsize = sizeof(Phdr); |
| 197 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 198 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 199 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 200 | phdr.p_type = PT_LOAD; |
| 201 | phdr.p_vaddr = 0x2000; |
| 202 | phdr.p_memsz = 0x10000; |
| 203 | phdr.p_flags = PF_R | PF_X; |
| 204 | phdr.p_align = 0x1000; |
| 205 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 206 | |
| 207 | memset(&phdr, 0, sizeof(phdr)); |
| 208 | phdr.p_type = PT_LOAD; |
| 209 | phdr.p_offset = 0x1000; |
| 210 | phdr.p_vaddr = 0x2001; |
| 211 | phdr.p_memsz = 0x10001; |
| 212 | phdr.p_flags = PF_R | PF_X; |
| 213 | phdr.p_align = 0x1001; |
| 214 | memory_.SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr)); |
| 215 | |
| 216 | memset(&phdr, 0, sizeof(phdr)); |
| 217 | phdr.p_type = PT_LOAD; |
| 218 | phdr.p_offset = 0x2000; |
| 219 | phdr.p_vaddr = 0x2002; |
| 220 | phdr.p_memsz = 0x10002; |
| 221 | phdr.p_flags = PF_R | PF_X; |
| 222 | phdr.p_align = 0x1002; |
| 223 | memory_.SetMemory(0x100 + 2 * sizeof(phdr), &phdr, sizeof(phdr)); |
| 224 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 225 | uint64_t load_bias = 0; |
| 226 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 227 | EXPECT_EQ(0x2000U, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 228 | |
| 229 | const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads(); |
| 230 | ASSERT_EQ(3U, pt_loads.size()); |
| 231 | |
| 232 | LoadInfo load_data = pt_loads.at(0); |
| 233 | ASSERT_EQ(0U, load_data.offset); |
| 234 | ASSERT_EQ(0x2000U, load_data.table_offset); |
| 235 | ASSERT_EQ(0x10000U, load_data.table_size); |
| 236 | |
| 237 | load_data = pt_loads.at(0x1000); |
| 238 | ASSERT_EQ(0x1000U, load_data.offset); |
| 239 | ASSERT_EQ(0x2001U, load_data.table_offset); |
| 240 | ASSERT_EQ(0x10001U, load_data.table_size); |
| 241 | |
| 242 | load_data = pt_loads.at(0x2000); |
| 243 | ASSERT_EQ(0x2000U, load_data.offset); |
| 244 | ASSERT_EQ(0x2002U, load_data.table_offset); |
| 245 | ASSERT_EQ(0x10002U, load_data.table_size); |
| 246 | } |
| 247 | |
| 248 | TEST_F(ElfInterfaceTest, elf32_multiple_executable_pt_loads) { |
| 249 | MultipleExecutablePtLoads<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); |
| 250 | } |
| 251 | |
| 252 | TEST_F(ElfInterfaceTest, elf64_multiple_executable_pt_loads) { |
| 253 | MultipleExecutablePtLoads<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); |
| 254 | } |
| 255 | |
| 256 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> |
| 257 | void ElfInterfaceTest::MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr() { |
| 258 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 259 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 260 | Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 261 | ehdr.e_phoff = 0x100; |
| 262 | ehdr.e_phnum = 3; |
| 263 | ehdr.e_phentsize = sizeof(Phdr) + 100; |
| 264 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 265 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 266 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 267 | phdr.p_type = PT_LOAD; |
| 268 | phdr.p_vaddr = 0x2000; |
| 269 | phdr.p_memsz = 0x10000; |
| 270 | phdr.p_flags = PF_R | PF_X; |
| 271 | phdr.p_align = 0x1000; |
| 272 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 273 | |
| 274 | memset(&phdr, 0, sizeof(phdr)); |
| 275 | phdr.p_type = PT_LOAD; |
| 276 | phdr.p_offset = 0x1000; |
| 277 | phdr.p_vaddr = 0x2001; |
| 278 | phdr.p_memsz = 0x10001; |
| 279 | phdr.p_flags = PF_R | PF_X; |
| 280 | phdr.p_align = 0x1001; |
| 281 | memory_.SetMemory(0x100 + sizeof(phdr) + 100, &phdr, sizeof(phdr)); |
| 282 | |
| 283 | memset(&phdr, 0, sizeof(phdr)); |
| 284 | phdr.p_type = PT_LOAD; |
| 285 | phdr.p_offset = 0x2000; |
| 286 | phdr.p_vaddr = 0x2002; |
| 287 | phdr.p_memsz = 0x10002; |
| 288 | phdr.p_flags = PF_R | PF_X; |
| 289 | phdr.p_align = 0x1002; |
| 290 | memory_.SetMemory(0x100 + 2 * (sizeof(phdr) + 100), &phdr, sizeof(phdr)); |
| 291 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 292 | uint64_t load_bias = 0; |
| 293 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 294 | EXPECT_EQ(0x2000U, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 295 | |
| 296 | const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads(); |
| 297 | ASSERT_EQ(3U, pt_loads.size()); |
| 298 | |
| 299 | LoadInfo load_data = pt_loads.at(0); |
| 300 | ASSERT_EQ(0U, load_data.offset); |
| 301 | ASSERT_EQ(0x2000U, load_data.table_offset); |
| 302 | ASSERT_EQ(0x10000U, load_data.table_size); |
| 303 | |
| 304 | load_data = pt_loads.at(0x1000); |
| 305 | ASSERT_EQ(0x1000U, load_data.offset); |
| 306 | ASSERT_EQ(0x2001U, load_data.table_offset); |
| 307 | ASSERT_EQ(0x10001U, load_data.table_size); |
| 308 | |
| 309 | load_data = pt_loads.at(0x2000); |
| 310 | ASSERT_EQ(0x2000U, load_data.offset); |
| 311 | ASSERT_EQ(0x2002U, load_data.table_offset); |
| 312 | ASSERT_EQ(0x10002U, load_data.table_size); |
| 313 | } |
| 314 | |
| 315 | TEST_F(ElfInterfaceTest, elf32_multiple_executable_pt_loads_increments_not_size_of_phdr) { |
| 316 | MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, |
| 317 | ElfInterface32>(); |
| 318 | } |
| 319 | |
| 320 | TEST_F(ElfInterfaceTest, elf64_multiple_executable_pt_loads_increments_not_size_of_phdr) { |
| 321 | MultipleExecutablePtLoadsIncrementsNotSizeOfPhdr<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, |
| 322 | ElfInterface64>(); |
| 323 | } |
| 324 | |
| 325 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> |
| 326 | void ElfInterfaceTest::NonExecutablePtLoads() { |
| 327 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 328 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 329 | Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 330 | ehdr.e_phoff = 0x100; |
| 331 | ehdr.e_phnum = 3; |
| 332 | ehdr.e_phentsize = sizeof(Phdr); |
| 333 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 334 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 335 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 336 | phdr.p_type = PT_LOAD; |
| 337 | phdr.p_vaddr = 0x2000; |
| 338 | phdr.p_memsz = 0x10000; |
| 339 | phdr.p_flags = PF_R; |
| 340 | phdr.p_align = 0x1000; |
| 341 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 342 | |
| 343 | memset(&phdr, 0, sizeof(phdr)); |
| 344 | phdr.p_type = PT_LOAD; |
| 345 | phdr.p_offset = 0x1000; |
| 346 | phdr.p_vaddr = 0x2001; |
| 347 | phdr.p_memsz = 0x10001; |
| 348 | phdr.p_flags = PF_R | PF_X; |
| 349 | phdr.p_align = 0x1001; |
| 350 | memory_.SetMemory(0x100 + sizeof(phdr), &phdr, sizeof(phdr)); |
| 351 | |
| 352 | memset(&phdr, 0, sizeof(phdr)); |
| 353 | phdr.p_type = PT_LOAD; |
| 354 | phdr.p_offset = 0x2000; |
| 355 | phdr.p_vaddr = 0x2002; |
| 356 | phdr.p_memsz = 0x10002; |
| 357 | phdr.p_flags = PF_R; |
| 358 | phdr.p_align = 0x1002; |
| 359 | memory_.SetMemory(0x100 + 2 * sizeof(phdr), &phdr, sizeof(phdr)); |
| 360 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 361 | uint64_t load_bias = 0; |
| 362 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 363 | EXPECT_EQ(0U, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 364 | |
| 365 | const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads(); |
| 366 | ASSERT_EQ(1U, pt_loads.size()); |
| 367 | |
| 368 | LoadInfo load_data = pt_loads.at(0x1000); |
| 369 | ASSERT_EQ(0x1000U, load_data.offset); |
| 370 | ASSERT_EQ(0x2001U, load_data.table_offset); |
| 371 | ASSERT_EQ(0x10001U, load_data.table_size); |
| 372 | } |
| 373 | |
| 374 | TEST_F(ElfInterfaceTest, elf32_non_executable_pt_loads) { |
| 375 | NonExecutablePtLoads<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); |
| 376 | } |
| 377 | |
| 378 | TEST_F(ElfInterfaceTest, elf64_non_executable_pt_loads) { |
| 379 | NonExecutablePtLoads<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); |
| 380 | } |
| 381 | |
| 382 | template <typename Ehdr, typename Phdr, typename Dyn, typename ElfInterfaceType> |
| 383 | void ElfInterfaceTest::ManyPhdrs() { |
| 384 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 385 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 386 | Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 387 | ehdr.e_phoff = 0x100; |
| 388 | ehdr.e_phnum = 7; |
| 389 | ehdr.e_phentsize = sizeof(Phdr); |
| 390 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 391 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 392 | uint64_t phdr_offset = 0x100; |
| 393 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 394 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 395 | phdr.p_type = PT_LOAD; |
| 396 | phdr.p_vaddr = 0x2000; |
| 397 | phdr.p_memsz = 0x10000; |
| 398 | phdr.p_flags = PF_R | PF_X; |
| 399 | phdr.p_align = 0x1000; |
| 400 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); |
| 401 | phdr_offset += sizeof(phdr); |
| 402 | |
| 403 | memset(&phdr, 0, sizeof(phdr)); |
| 404 | phdr.p_type = PT_GNU_EH_FRAME; |
| 405 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); |
| 406 | phdr_offset += sizeof(phdr); |
| 407 | |
| 408 | memset(&phdr, 0, sizeof(phdr)); |
| 409 | phdr.p_type = PT_DYNAMIC; |
| 410 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); |
| 411 | phdr_offset += sizeof(phdr); |
| 412 | |
| 413 | memset(&phdr, 0, sizeof(phdr)); |
| 414 | phdr.p_type = PT_INTERP; |
| 415 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); |
| 416 | phdr_offset += sizeof(phdr); |
| 417 | |
| 418 | memset(&phdr, 0, sizeof(phdr)); |
| 419 | phdr.p_type = PT_NOTE; |
| 420 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); |
| 421 | phdr_offset += sizeof(phdr); |
| 422 | |
| 423 | memset(&phdr, 0, sizeof(phdr)); |
| 424 | phdr.p_type = PT_SHLIB; |
| 425 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); |
| 426 | phdr_offset += sizeof(phdr); |
| 427 | |
| 428 | memset(&phdr, 0, sizeof(phdr)); |
| 429 | phdr.p_type = PT_GNU_EH_FRAME; |
| 430 | memory_.SetMemory(phdr_offset, &phdr, sizeof(phdr)); |
| 431 | phdr_offset += sizeof(phdr); |
| 432 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 433 | uint64_t load_bias = 0; |
| 434 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 435 | EXPECT_EQ(0x2000U, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 436 | |
| 437 | const std::unordered_map<uint64_t, LoadInfo>& pt_loads = elf->pt_loads(); |
| 438 | ASSERT_EQ(1U, pt_loads.size()); |
| 439 | |
| 440 | LoadInfo load_data = pt_loads.at(0); |
| 441 | ASSERT_EQ(0U, load_data.offset); |
| 442 | ASSERT_EQ(0x2000U, load_data.table_offset); |
| 443 | ASSERT_EQ(0x10000U, load_data.table_size); |
| 444 | } |
| 445 | |
| 446 | TEST_F(ElfInterfaceTest, elf32_many_phdrs) { |
| 447 | ElfInterfaceTest::ManyPhdrs<Elf32_Ehdr, Elf32_Phdr, Elf32_Dyn, ElfInterface32>(); |
| 448 | } |
| 449 | |
| 450 | TEST_F(ElfInterfaceTest, elf64_many_phdrs) { |
| 451 | ElfInterfaceTest::ManyPhdrs<Elf64_Ehdr, Elf64_Phdr, Elf64_Dyn, ElfInterface64>(); |
| 452 | } |
| 453 | |
| 454 | TEST_F(ElfInterfaceTest, elf32_arm) { |
| 455 | ElfInterfaceArm elf_arm(&memory_); |
| 456 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 457 | Elf32_Ehdr ehdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 458 | ehdr.e_phoff = 0x100; |
| 459 | ehdr.e_phnum = 1; |
| 460 | ehdr.e_phentsize = sizeof(Elf32_Phdr); |
| 461 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 462 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 463 | Elf32_Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 464 | phdr.p_type = PT_ARM_EXIDX; |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 465 | phdr.p_offset = 0x2000; |
| 466 | phdr.p_filesz = 16; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 467 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 468 | |
| 469 | // Add arm exidx entries. |
| 470 | memory_.SetData32(0x2000, 0x1000); |
| 471 | memory_.SetData32(0x2008, 0x1000); |
| 472 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 473 | uint64_t load_bias = 0; |
| 474 | ASSERT_TRUE(elf_arm.Init(&load_bias)); |
| 475 | EXPECT_EQ(0U, load_bias); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 476 | |
| 477 | std::vector<uint32_t> entries; |
| 478 | for (auto addr : elf_arm) { |
| 479 | entries.push_back(addr); |
| 480 | } |
| 481 | ASSERT_EQ(2U, entries.size()); |
| 482 | ASSERT_EQ(0x3000U, entries[0]); |
| 483 | ASSERT_EQ(0x3008U, entries[1]); |
| 484 | |
| 485 | ASSERT_EQ(0x2000U, elf_arm.start_offset()); |
| 486 | ASSERT_EQ(2U, elf_arm.total_entries()); |
| 487 | } |
| 488 | |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 489 | template <typename Ehdr, typename Phdr, typename Shdr, typename Dyn> |
| 490 | void ElfInterfaceTest::SonameInit(SonameTestEnum test_type) { |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 491 | Ehdr ehdr = {}; |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 492 | ehdr.e_shoff = 0x200; |
| 493 | ehdr.e_shnum = 2; |
| 494 | ehdr.e_shentsize = sizeof(Shdr); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 495 | ehdr.e_phoff = 0x100; |
| 496 | ehdr.e_phnum = 1; |
| 497 | ehdr.e_phentsize = sizeof(Phdr); |
| 498 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 499 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 500 | Shdr shdr = {}; |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 501 | shdr.sh_type = SHT_STRTAB; |
| 502 | if (test_type == SONAME_MISSING_MAP) { |
| 503 | shdr.sh_addr = 0x20100; |
| 504 | } else { |
| 505 | shdr.sh_addr = 0x10100; |
| 506 | } |
| 507 | shdr.sh_offset = 0x10000; |
| 508 | memory_.SetMemory(0x200 + sizeof(shdr), &shdr, sizeof(shdr)); |
| 509 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 510 | Phdr phdr = {}; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 511 | phdr.p_type = PT_DYNAMIC; |
| 512 | phdr.p_offset = 0x2000; |
| 513 | phdr.p_memsz = sizeof(Dyn) * 3; |
| 514 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 515 | |
| 516 | uint64_t offset = 0x2000; |
| 517 | Dyn dyn; |
| 518 | |
| 519 | dyn.d_tag = DT_STRTAB; |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 520 | dyn.d_un.d_ptr = 0x10100; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 521 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); |
| 522 | offset += sizeof(dyn); |
| 523 | |
| 524 | dyn.d_tag = DT_STRSZ; |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 525 | if (test_type == SONAME_DTSIZE_SMALL) { |
| 526 | dyn.d_un.d_val = 0x10; |
| 527 | } else { |
| 528 | dyn.d_un.d_val = 0x1000; |
| 529 | } |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 530 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); |
| 531 | offset += sizeof(dyn); |
| 532 | |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 533 | if (test_type == SONAME_DTNULL_AFTER) { |
| 534 | dyn.d_tag = DT_NULL; |
| 535 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); |
| 536 | offset += sizeof(dyn); |
| 537 | } |
| 538 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 539 | dyn.d_tag = DT_SONAME; |
| 540 | dyn.d_un.d_val = 0x10; |
| 541 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); |
| 542 | offset += sizeof(dyn); |
| 543 | |
| 544 | dyn.d_tag = DT_NULL; |
| 545 | memory_.SetMemory(offset, &dyn, sizeof(dyn)); |
| 546 | |
| 547 | SetStringMemory(0x10010, "fake_soname.so"); |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | template <typename ElfInterfaceType> |
| 551 | void ElfInterfaceTest::Soname() { |
| 552 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 553 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 554 | uint64_t load_bias = 0; |
| 555 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 556 | EXPECT_EQ(0U, load_bias); |
| 557 | |
Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame^] | 558 | ASSERT_EQ("fake_soname.so", elf->GetSoname()); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | TEST_F(ElfInterfaceTest, elf32_soname) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 562 | SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(); |
| 563 | Soname<ElfInterface32>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | TEST_F(ElfInterfaceTest, elf64_soname) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 567 | SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(); |
| 568 | Soname<ElfInterface64>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 569 | } |
| 570 | |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 571 | template <typename ElfInterfaceType> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 572 | void ElfInterfaceTest::SonameAfterDtNull() { |
| 573 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 574 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 575 | uint64_t load_bias = 0; |
| 576 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 577 | EXPECT_EQ(0U, load_bias); |
| 578 | |
Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame^] | 579 | ASSERT_EQ("", elf->GetSoname()); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | TEST_F(ElfInterfaceTest, elf32_soname_after_dt_null) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 583 | SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_DTNULL_AFTER); |
| 584 | SonameAfterDtNull<ElfInterface32>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | TEST_F(ElfInterfaceTest, elf64_soname_after_dt_null) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 588 | SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(SONAME_DTNULL_AFTER); |
| 589 | SonameAfterDtNull<ElfInterface64>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 590 | } |
| 591 | |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 592 | template <typename ElfInterfaceType> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 593 | void ElfInterfaceTest::SonameSize() { |
| 594 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 595 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 596 | uint64_t load_bias = 0; |
| 597 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 598 | EXPECT_EQ(0U, load_bias); |
| 599 | |
Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame^] | 600 | ASSERT_EQ("", elf->GetSoname()); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | TEST_F(ElfInterfaceTest, elf32_soname_size) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 604 | SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_DTSIZE_SMALL); |
| 605 | SonameSize<ElfInterface32>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | TEST_F(ElfInterfaceTest, elf64_soname_size) { |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 609 | SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(SONAME_DTSIZE_SMALL); |
| 610 | SonameSize<ElfInterface64>(); |
| 611 | } |
| 612 | |
| 613 | // Verify that there is no map from STRTAB in the dynamic section to a |
| 614 | // STRTAB entry in the section headers. |
| 615 | template <typename ElfInterfaceType> |
| 616 | void ElfInterfaceTest::SonameMissingMap() { |
| 617 | std::unique_ptr<ElfInterface> elf(new ElfInterfaceType(&memory_)); |
| 618 | |
| 619 | uint64_t load_bias = 0; |
| 620 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 621 | EXPECT_EQ(0U, load_bias); |
| 622 | |
Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame^] | 623 | ASSERT_EQ("", elf->GetSoname()); |
Christopher Ferris | beae42b | 2018-02-15 17:36:33 -0800 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | TEST_F(ElfInterfaceTest, elf32_soname_missing_map) { |
| 627 | SonameInit<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Dyn>(SONAME_MISSING_MAP); |
| 628 | SonameMissingMap<ElfInterface32>(); |
| 629 | } |
| 630 | |
| 631 | TEST_F(ElfInterfaceTest, elf64_soname_missing_map) { |
| 632 | SonameInit<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Dyn>(SONAME_MISSING_MAP); |
| 633 | SonameMissingMap<ElfInterface64>(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 634 | } |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 635 | |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 636 | template <typename ElfType> |
| 637 | void ElfInterfaceTest::InitHeadersEhFrameTest() { |
| 638 | ElfType elf(&memory_); |
| 639 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 640 | elf.FakeSetEhFrameOffset(0x10000); |
| 641 | elf.FakeSetEhFrameSize(0); |
| 642 | elf.FakeSetDebugFrameOffset(0); |
| 643 | elf.FakeSetDebugFrameSize(0); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 644 | |
| 645 | memory_.SetMemory(0x10000, |
| 646 | std::vector<uint8_t>{0x1, DW_EH_PE_udata2, DW_EH_PE_udata2, DW_EH_PE_udata2}); |
| 647 | memory_.SetData32(0x10004, 0x500); |
| 648 | memory_.SetData32(0x10008, 250); |
| 649 | |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 650 | elf.InitHeaders(0); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 651 | |
| 652 | EXPECT_FALSE(elf.eh_frame() == nullptr); |
| 653 | EXPECT_TRUE(elf.debug_frame() == nullptr); |
| 654 | } |
| 655 | |
| 656 | TEST_F(ElfInterfaceTest, init_headers_eh_frame32) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 657 | InitHeadersEhFrameTest<ElfInterface32Fake>(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | TEST_F(ElfInterfaceTest, init_headers_eh_frame64) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 661 | InitHeadersEhFrameTest<ElfInterface64Fake>(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | template <typename ElfType> |
| 665 | void ElfInterfaceTest::InitHeadersDebugFrame() { |
| 666 | ElfType elf(&memory_); |
| 667 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 668 | elf.FakeSetEhFrameOffset(0); |
| 669 | elf.FakeSetEhFrameSize(0); |
| 670 | elf.FakeSetDebugFrameOffset(0x5000); |
| 671 | elf.FakeSetDebugFrameSize(0x200); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 672 | |
| 673 | memory_.SetData32(0x5000, 0xfc); |
| 674 | memory_.SetData32(0x5004, 0xffffffff); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 675 | memory_.SetMemory(0x5008, std::vector<uint8_t>{1, '\0', 4, 8, 2}); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 676 | |
| 677 | memory_.SetData32(0x5100, 0xfc); |
| 678 | memory_.SetData32(0x5104, 0); |
| 679 | memory_.SetData32(0x5108, 0x1500); |
| 680 | memory_.SetData32(0x510c, 0x200); |
| 681 | |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 682 | elf.InitHeaders(0); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 683 | |
| 684 | EXPECT_TRUE(elf.eh_frame() == nullptr); |
| 685 | EXPECT_FALSE(elf.debug_frame() == nullptr); |
| 686 | } |
| 687 | |
| 688 | TEST_F(ElfInterfaceTest, init_headers_debug_frame32) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 689 | InitHeadersDebugFrame<ElfInterface32Fake>(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | TEST_F(ElfInterfaceTest, init_headers_debug_frame64) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 693 | InitHeadersDebugFrame<ElfInterface64Fake>(); |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 694 | } |
| 695 | |
Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 696 | template <typename Ehdr, typename Phdr, typename ElfInterfaceType> |
| 697 | void ElfInterfaceTest::InitProgramHeadersMalformed() { |
| 698 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 699 | |
| 700 | Ehdr ehdr = {}; |
| 701 | ehdr.e_phoff = 0x100; |
| 702 | ehdr.e_phnum = 3; |
| 703 | ehdr.e_phentsize = sizeof(Phdr); |
| 704 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 705 | |
| 706 | uint64_t load_bias = 0; |
| 707 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 708 | EXPECT_EQ(0U, load_bias); |
| 709 | } |
| 710 | |
| 711 | TEST_F(ElfInterfaceTest, init_program_headers_malformed32) { |
| 712 | InitProgramHeadersMalformed<Elf32_Ehdr, Elf32_Phdr, ElfInterface32>(); |
| 713 | } |
| 714 | |
| 715 | TEST_F(ElfInterfaceTest, init_program_headers_malformed64) { |
| 716 | InitProgramHeadersMalformed<Elf64_Ehdr, Elf64_Phdr, ElfInterface64>(); |
| 717 | } |
| 718 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 719 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> |
| 720 | void ElfInterfaceTest::InitSectionHeadersMalformed() { |
| 721 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 722 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 723 | Ehdr ehdr = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 724 | ehdr.e_shoff = 0x1000; |
| 725 | ehdr.e_shnum = 10; |
| 726 | ehdr.e_shentsize = sizeof(Shdr); |
| 727 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 728 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 729 | uint64_t load_bias = 0; |
| 730 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 731 | EXPECT_EQ(0U, load_bias); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | TEST_F(ElfInterfaceTest, init_section_headers_malformed32) { |
| 735 | InitSectionHeadersMalformed<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(); |
| 736 | } |
| 737 | |
| 738 | TEST_F(ElfInterfaceTest, init_section_headers_malformed64) { |
| 739 | InitSectionHeadersMalformed<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(); |
| 740 | } |
| 741 | |
Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 742 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> |
| 743 | void ElfInterfaceTest::InitSectionHeadersMalformedSymData() { |
| 744 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 745 | |
| 746 | uint64_t offset = 0x1000; |
| 747 | |
| 748 | Ehdr ehdr = {}; |
| 749 | ehdr.e_shoff = offset; |
| 750 | ehdr.e_shnum = 5; |
| 751 | ehdr.e_shentsize = sizeof(Shdr); |
| 752 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 753 | |
| 754 | offset += ehdr.e_shentsize; |
| 755 | |
| 756 | Shdr shdr = {}; |
| 757 | shdr.sh_type = SHT_SYMTAB; |
| 758 | shdr.sh_link = 4; |
| 759 | shdr.sh_addr = 0x5000; |
| 760 | shdr.sh_offset = 0x5000; |
| 761 | shdr.sh_entsize = 0x100; |
| 762 | shdr.sh_size = shdr.sh_entsize * 10; |
| 763 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 764 | offset += ehdr.e_shentsize; |
| 765 | |
| 766 | memset(&shdr, 0, sizeof(shdr)); |
| 767 | shdr.sh_type = SHT_DYNSYM; |
| 768 | shdr.sh_link = 10; |
| 769 | shdr.sh_addr = 0x6000; |
| 770 | shdr.sh_offset = 0x6000; |
| 771 | shdr.sh_entsize = 0x100; |
| 772 | shdr.sh_size = shdr.sh_entsize * 10; |
| 773 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 774 | offset += ehdr.e_shentsize; |
| 775 | |
| 776 | memset(&shdr, 0, sizeof(shdr)); |
| 777 | shdr.sh_type = SHT_DYNSYM; |
| 778 | shdr.sh_link = 2; |
| 779 | shdr.sh_addr = 0x6000; |
| 780 | shdr.sh_offset = 0x6000; |
| 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 | // The string data for the entries. |
| 787 | memset(&shdr, 0, sizeof(shdr)); |
| 788 | shdr.sh_type = SHT_STRTAB; |
| 789 | shdr.sh_name = 0x20000; |
| 790 | shdr.sh_offset = 0xf000; |
| 791 | shdr.sh_size = 0x1000; |
| 792 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 793 | offset += ehdr.e_shentsize; |
| 794 | |
| 795 | uint64_t load_bias = 0; |
| 796 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 797 | EXPECT_EQ(0U, load_bias); |
| 798 | EXPECT_EQ(0U, elf->debug_frame_offset()); |
| 799 | EXPECT_EQ(0U, elf->debug_frame_size()); |
| 800 | EXPECT_EQ(0U, elf->gnu_debugdata_offset()); |
| 801 | EXPECT_EQ(0U, elf->gnu_debugdata_size()); |
| 802 | |
| 803 | std::string name; |
| 804 | uint64_t name_offset; |
| 805 | ASSERT_FALSE(elf->GetFunctionName(0x90010, &name, &name_offset)); |
| 806 | } |
| 807 | |
| 808 | TEST_F(ElfInterfaceTest, init_section_headers_malformed_symdata32) { |
| 809 | InitSectionHeadersMalformedSymData<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(); |
| 810 | } |
| 811 | |
| 812 | TEST_F(ElfInterfaceTest, init_section_headers_malformed_symdata64) { |
| 813 | InitSectionHeadersMalformedSymData<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(); |
| 814 | } |
| 815 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 816 | template <typename Ehdr, typename Shdr, typename Sym, typename ElfInterfaceType> |
| 817 | void ElfInterfaceTest::InitSectionHeaders(uint64_t entry_size) { |
| 818 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 819 | |
| 820 | uint64_t offset = 0x1000; |
| 821 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 822 | Ehdr ehdr = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 823 | ehdr.e_shoff = offset; |
Christopher Ferris | 5acf069 | 2018-08-01 13:10:46 -0700 | [diff] [blame] | 824 | ehdr.e_shnum = 5; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 825 | ehdr.e_shentsize = entry_size; |
| 826 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 827 | |
| 828 | offset += ehdr.e_shentsize; |
| 829 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 830 | Shdr shdr = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 831 | shdr.sh_type = SHT_SYMTAB; |
| 832 | shdr.sh_link = 4; |
| 833 | shdr.sh_addr = 0x5000; |
| 834 | shdr.sh_offset = 0x5000; |
| 835 | shdr.sh_entsize = sizeof(Sym); |
| 836 | shdr.sh_size = shdr.sh_entsize * 10; |
| 837 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 838 | offset += ehdr.e_shentsize; |
| 839 | |
| 840 | memset(&shdr, 0, sizeof(shdr)); |
| 841 | shdr.sh_type = SHT_DYNSYM; |
| 842 | shdr.sh_link = 4; |
| 843 | shdr.sh_addr = 0x6000; |
| 844 | shdr.sh_offset = 0x6000; |
| 845 | shdr.sh_entsize = sizeof(Sym); |
| 846 | shdr.sh_size = shdr.sh_entsize * 10; |
| 847 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 848 | offset += ehdr.e_shentsize; |
| 849 | |
| 850 | memset(&shdr, 0, sizeof(shdr)); |
| 851 | shdr.sh_type = SHT_PROGBITS; |
| 852 | shdr.sh_name = 0xa000; |
| 853 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 854 | offset += ehdr.e_shentsize; |
| 855 | |
| 856 | // The string data for the entries. |
| 857 | memset(&shdr, 0, sizeof(shdr)); |
| 858 | shdr.sh_type = SHT_STRTAB; |
| 859 | shdr.sh_name = 0x20000; |
| 860 | shdr.sh_offset = 0xf000; |
| 861 | shdr.sh_size = 0x1000; |
| 862 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 863 | offset += ehdr.e_shentsize; |
| 864 | |
| 865 | InitSym<Sym>(0x5000, 0x90000, 0x1000, 0x100, 0xf000, "function_one"); |
| 866 | InitSym<Sym>(0x6000, 0xd0000, 0x1000, 0x300, 0xf000, "function_two"); |
| 867 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 868 | uint64_t load_bias = 0; |
| 869 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 870 | EXPECT_EQ(0U, load_bias); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 871 | EXPECT_EQ(0U, elf->debug_frame_offset()); |
| 872 | EXPECT_EQ(0U, elf->debug_frame_size()); |
| 873 | EXPECT_EQ(0U, elf->gnu_debugdata_offset()); |
| 874 | EXPECT_EQ(0U, elf->gnu_debugdata_size()); |
| 875 | |
| 876 | // Look in the first symbol table. |
| 877 | std::string name; |
| 878 | uint64_t name_offset; |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 879 | ASSERT_TRUE(elf->GetFunctionName(0x90010, &name, &name_offset)); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 880 | EXPECT_EQ("function_one", name); |
| 881 | EXPECT_EQ(16U, name_offset); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 882 | ASSERT_TRUE(elf->GetFunctionName(0xd0020, &name, &name_offset)); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 883 | EXPECT_EQ("function_two", name); |
| 884 | EXPECT_EQ(32U, name_offset); |
| 885 | } |
| 886 | |
| 887 | TEST_F(ElfInterfaceTest, init_section_headers32) { |
| 888 | InitSectionHeaders<Elf32_Ehdr, Elf32_Shdr, Elf32_Sym, ElfInterface32>(sizeof(Elf32_Shdr)); |
| 889 | } |
| 890 | |
| 891 | TEST_F(ElfInterfaceTest, init_section_headers64) { |
| 892 | InitSectionHeaders<Elf64_Ehdr, Elf64_Shdr, Elf64_Sym, ElfInterface64>(sizeof(Elf64_Shdr)); |
| 893 | } |
| 894 | |
| 895 | TEST_F(ElfInterfaceTest, init_section_headers_non_std_entry_size32) { |
| 896 | InitSectionHeaders<Elf32_Ehdr, Elf32_Shdr, Elf32_Sym, ElfInterface32>(0x100); |
| 897 | } |
| 898 | |
| 899 | TEST_F(ElfInterfaceTest, init_section_headers_non_std_entry_size64) { |
| 900 | InitSectionHeaders<Elf64_Ehdr, Elf64_Shdr, Elf64_Sym, ElfInterface64>(0x100); |
| 901 | } |
| 902 | |
| 903 | template <typename Ehdr, typename Shdr, typename ElfInterfaceType> |
| 904 | void ElfInterfaceTest::InitSectionHeadersOffsets() { |
| 905 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 906 | |
| 907 | uint64_t offset = 0x2000; |
| 908 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 909 | Ehdr ehdr = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 910 | ehdr.e_shoff = offset; |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 911 | ehdr.e_shnum = 7; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 912 | ehdr.e_shentsize = sizeof(Shdr); |
| 913 | ehdr.e_shstrndx = 2; |
| 914 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 915 | |
| 916 | offset += ehdr.e_shentsize; |
| 917 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 918 | Shdr shdr = {}; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 919 | shdr.sh_type = SHT_PROGBITS; |
| 920 | shdr.sh_link = 2; |
| 921 | shdr.sh_name = 0x200; |
| 922 | shdr.sh_addr = 0x5000; |
| 923 | shdr.sh_offset = 0x5000; |
| 924 | shdr.sh_entsize = 0x100; |
| 925 | shdr.sh_size = 0x800; |
| 926 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 927 | offset += ehdr.e_shentsize; |
| 928 | |
| 929 | // The string data for section header names. |
| 930 | memset(&shdr, 0, sizeof(shdr)); |
| 931 | shdr.sh_type = SHT_STRTAB; |
| 932 | shdr.sh_name = 0x20000; |
| 933 | shdr.sh_offset = 0xf000; |
| 934 | shdr.sh_size = 0x1000; |
| 935 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 936 | offset += ehdr.e_shentsize; |
| 937 | |
| 938 | memset(&shdr, 0, sizeof(shdr)); |
| 939 | shdr.sh_type = SHT_PROGBITS; |
| 940 | shdr.sh_link = 2; |
| 941 | shdr.sh_name = 0x100; |
| 942 | shdr.sh_addr = 0x6000; |
| 943 | shdr.sh_offset = 0x6000; |
| 944 | shdr.sh_entsize = 0x100; |
| 945 | shdr.sh_size = 0x500; |
| 946 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 947 | offset += ehdr.e_shentsize; |
| 948 | |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 949 | memset(&shdr, 0, sizeof(shdr)); |
| 950 | shdr.sh_type = SHT_PROGBITS; |
| 951 | shdr.sh_link = 2; |
| 952 | shdr.sh_name = 0x300; |
| 953 | shdr.sh_addr = 0x7000; |
| 954 | shdr.sh_offset = 0x7000; |
| 955 | shdr.sh_entsize = 0x100; |
| 956 | shdr.sh_size = 0x800; |
| 957 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 958 | offset += ehdr.e_shentsize; |
| 959 | |
| 960 | memset(&shdr, 0, sizeof(shdr)); |
| 961 | shdr.sh_type = SHT_PROGBITS; |
| 962 | shdr.sh_link = 2; |
| 963 | shdr.sh_name = 0x400; |
| 964 | shdr.sh_addr = 0x6000; |
| 965 | shdr.sh_offset = 0xa000; |
| 966 | shdr.sh_entsize = 0x100; |
| 967 | shdr.sh_size = 0xf00; |
| 968 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 969 | offset += ehdr.e_shentsize; |
| 970 | |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 971 | memset(&shdr, 0, sizeof(shdr)); |
| 972 | shdr.sh_type = SHT_NOTE; |
| 973 | shdr.sh_name = 0x500; |
| 974 | shdr.sh_offset = 0xb000; |
| 975 | shdr.sh_size = 0xf00; |
| 976 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 977 | offset += ehdr.e_shentsize; |
| 978 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 979 | memory_.SetMemory(0xf100, ".debug_frame", sizeof(".debug_frame")); |
| 980 | memory_.SetMemory(0xf200, ".gnu_debugdata", sizeof(".gnu_debugdata")); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 981 | memory_.SetMemory(0xf300, ".eh_frame", sizeof(".eh_frame")); |
| 982 | memory_.SetMemory(0xf400, ".eh_frame_hdr", sizeof(".eh_frame_hdr")); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 983 | memory_.SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id")); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 984 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 985 | uint64_t load_bias = 0; |
| 986 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 987 | EXPECT_EQ(0U, load_bias); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 988 | EXPECT_EQ(0x6000U, elf->debug_frame_offset()); |
| 989 | EXPECT_EQ(0x500U, elf->debug_frame_size()); |
| 990 | EXPECT_EQ(0x5000U, elf->gnu_debugdata_offset()); |
| 991 | EXPECT_EQ(0x800U, elf->gnu_debugdata_size()); |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 992 | EXPECT_EQ(0x7000U, elf->eh_frame_offset()); |
| 993 | EXPECT_EQ(0x800U, elf->eh_frame_size()); |
| 994 | EXPECT_EQ(0xa000U, elf->eh_frame_hdr_offset()); |
| 995 | EXPECT_EQ(0xf00U, elf->eh_frame_hdr_size()); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 996 | EXPECT_EQ(0xb000U, elf->gnu_build_id_offset()); |
| 997 | EXPECT_EQ(0xf00U, elf->gnu_build_id_size()); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | TEST_F(ElfInterfaceTest, init_section_headers_offsets32) { |
| 1001 | InitSectionHeadersOffsets<Elf32_Ehdr, Elf32_Shdr, ElfInterface32>(); |
| 1002 | } |
| 1003 | |
| 1004 | TEST_F(ElfInterfaceTest, init_section_headers_offsets64) { |
| 1005 | InitSectionHeadersOffsets<Elf64_Ehdr, Elf64_Shdr, ElfInterface64>(); |
| 1006 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 1007 | |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1008 | TEST_F(ElfInterfaceTest, is_valid_pc_from_pt_load) { |
| 1009 | std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_)); |
| 1010 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1011 | Elf32_Ehdr ehdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1012 | ehdr.e_phoff = 0x100; |
| 1013 | ehdr.e_phnum = 1; |
| 1014 | ehdr.e_phentsize = sizeof(Elf32_Phdr); |
| 1015 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1016 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1017 | Elf32_Phdr phdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1018 | phdr.p_type = PT_LOAD; |
| 1019 | phdr.p_vaddr = 0; |
| 1020 | phdr.p_memsz = 0x10000; |
| 1021 | phdr.p_flags = PF_R | PF_X; |
| 1022 | phdr.p_align = 0x1000; |
| 1023 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 1024 | |
| 1025 | uint64_t load_bias = 0; |
| 1026 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 1027 | EXPECT_EQ(0U, load_bias); |
| 1028 | EXPECT_TRUE(elf->IsValidPc(0)); |
| 1029 | EXPECT_TRUE(elf->IsValidPc(0x5000)); |
| 1030 | EXPECT_TRUE(elf->IsValidPc(0xffff)); |
| 1031 | EXPECT_FALSE(elf->IsValidPc(0x10000)); |
| 1032 | } |
| 1033 | |
| 1034 | TEST_F(ElfInterfaceTest, is_valid_pc_from_pt_load_non_zero_load_bias) { |
| 1035 | std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_)); |
| 1036 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1037 | Elf32_Ehdr ehdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1038 | ehdr.e_phoff = 0x100; |
| 1039 | ehdr.e_phnum = 1; |
| 1040 | ehdr.e_phentsize = sizeof(Elf32_Phdr); |
| 1041 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1042 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1043 | Elf32_Phdr phdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1044 | phdr.p_type = PT_LOAD; |
| 1045 | phdr.p_vaddr = 0x2000; |
| 1046 | phdr.p_memsz = 0x10000; |
| 1047 | phdr.p_flags = PF_R | PF_X; |
| 1048 | phdr.p_align = 0x1000; |
| 1049 | memory_.SetMemory(0x100, &phdr, sizeof(phdr)); |
| 1050 | |
| 1051 | uint64_t load_bias = 0; |
| 1052 | ASSERT_TRUE(elf->Init(&load_bias)); |
| 1053 | EXPECT_EQ(0x2000U, load_bias); |
| 1054 | EXPECT_FALSE(elf->IsValidPc(0)); |
| 1055 | EXPECT_FALSE(elf->IsValidPc(0x1000)); |
| 1056 | EXPECT_FALSE(elf->IsValidPc(0x1fff)); |
| 1057 | EXPECT_TRUE(elf->IsValidPc(0x2000)); |
| 1058 | EXPECT_TRUE(elf->IsValidPc(0x5000)); |
| 1059 | EXPECT_TRUE(elf->IsValidPc(0x11fff)); |
| 1060 | EXPECT_FALSE(elf->IsValidPc(0x12000)); |
| 1061 | } |
| 1062 | |
| 1063 | TEST_F(ElfInterfaceTest, is_valid_pc_from_debug_frame) { |
| 1064 | std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_)); |
| 1065 | |
| 1066 | uint64_t sh_offset = 0x100; |
| 1067 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1068 | Elf32_Ehdr ehdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1069 | ehdr.e_shstrndx = 1; |
| 1070 | ehdr.e_shoff = sh_offset; |
| 1071 | ehdr.e_shentsize = sizeof(Elf32_Shdr); |
| 1072 | ehdr.e_shnum = 3; |
| 1073 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1074 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1075 | Elf32_Shdr shdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1076 | shdr.sh_type = SHT_NULL; |
| 1077 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 1078 | |
| 1079 | sh_offset += sizeof(shdr); |
| 1080 | memset(&shdr, 0, sizeof(shdr)); |
| 1081 | shdr.sh_type = SHT_STRTAB; |
| 1082 | shdr.sh_name = 1; |
| 1083 | shdr.sh_offset = 0x500; |
| 1084 | shdr.sh_size = 0x100; |
| 1085 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 1086 | memory_.SetMemory(0x500, ".debug_frame"); |
| 1087 | |
| 1088 | sh_offset += sizeof(shdr); |
| 1089 | memset(&shdr, 0, sizeof(shdr)); |
| 1090 | shdr.sh_type = SHT_PROGBITS; |
| 1091 | shdr.sh_name = 0; |
| 1092 | shdr.sh_addr = 0x600; |
| 1093 | shdr.sh_offset = 0x600; |
| 1094 | shdr.sh_size = 0x200; |
| 1095 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 1096 | |
| 1097 | // CIE 32. |
| 1098 | memory_.SetData32(0x600, 0xfc); |
| 1099 | memory_.SetData32(0x604, 0xffffffff); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 1100 | memory_.SetMemory(0x608, std::vector<uint8_t>{1, '\0', 4, 4, 1}); |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1101 | |
| 1102 | // FDE 32. |
| 1103 | memory_.SetData32(0x700, 0xfc); |
| 1104 | memory_.SetData32(0x704, 0); |
| 1105 | memory_.SetData32(0x708, 0x2100); |
| 1106 | memory_.SetData32(0x70c, 0x200); |
| 1107 | |
| 1108 | uint64_t load_bias = 0; |
| 1109 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 1110 | elf->InitHeaders(0); |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1111 | EXPECT_EQ(0U, load_bias); |
| 1112 | EXPECT_FALSE(elf->IsValidPc(0)); |
| 1113 | EXPECT_FALSE(elf->IsValidPc(0x20ff)); |
| 1114 | EXPECT_TRUE(elf->IsValidPc(0x2100)); |
| 1115 | EXPECT_TRUE(elf->IsValidPc(0x2200)); |
| 1116 | EXPECT_TRUE(elf->IsValidPc(0x22ff)); |
| 1117 | EXPECT_FALSE(elf->IsValidPc(0x2300)); |
| 1118 | } |
| 1119 | |
| 1120 | TEST_F(ElfInterfaceTest, is_valid_pc_from_eh_frame) { |
| 1121 | std::unique_ptr<ElfInterface> elf(new ElfInterface32(&memory_)); |
| 1122 | |
| 1123 | uint64_t sh_offset = 0x100; |
| 1124 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1125 | Elf32_Ehdr ehdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1126 | ehdr.e_shstrndx = 1; |
| 1127 | ehdr.e_shoff = sh_offset; |
| 1128 | ehdr.e_shentsize = sizeof(Elf32_Shdr); |
| 1129 | ehdr.e_shnum = 3; |
| 1130 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1131 | |
Christopher Ferris | f882a38 | 2018-06-22 16:48:02 -0700 | [diff] [blame] | 1132 | Elf32_Shdr shdr = {}; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1133 | shdr.sh_type = SHT_NULL; |
| 1134 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 1135 | |
| 1136 | sh_offset += sizeof(shdr); |
| 1137 | memset(&shdr, 0, sizeof(shdr)); |
| 1138 | shdr.sh_type = SHT_STRTAB; |
| 1139 | shdr.sh_name = 1; |
| 1140 | shdr.sh_offset = 0x500; |
| 1141 | shdr.sh_size = 0x100; |
| 1142 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 1143 | memory_.SetMemory(0x500, ".eh_frame"); |
| 1144 | |
| 1145 | sh_offset += sizeof(shdr); |
| 1146 | memset(&shdr, 0, sizeof(shdr)); |
| 1147 | shdr.sh_type = SHT_PROGBITS; |
| 1148 | shdr.sh_name = 0; |
| 1149 | shdr.sh_addr = 0x600; |
| 1150 | shdr.sh_offset = 0x600; |
| 1151 | shdr.sh_size = 0x200; |
| 1152 | memory_.SetMemory(sh_offset, &shdr, sizeof(shdr)); |
| 1153 | |
| 1154 | // CIE 32. |
| 1155 | memory_.SetData32(0x600, 0xfc); |
| 1156 | memory_.SetData32(0x604, 0); |
Christopher Ferris | 92acaac | 2018-06-21 10:44:02 -0700 | [diff] [blame] | 1157 | memory_.SetMemory(0x608, std::vector<uint8_t>{1, '\0', 4, 4, 1}); |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1158 | |
| 1159 | // FDE 32. |
| 1160 | memory_.SetData32(0x700, 0xfc); |
| 1161 | memory_.SetData32(0x704, 0x104); |
| 1162 | memory_.SetData32(0x708, 0x20f8); |
| 1163 | memory_.SetData32(0x70c, 0x200); |
| 1164 | |
| 1165 | uint64_t load_bias = 0; |
| 1166 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 1167 | elf->InitHeaders(0); |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 1168 | EXPECT_EQ(0U, load_bias); |
| 1169 | EXPECT_FALSE(elf->IsValidPc(0)); |
| 1170 | EXPECT_FALSE(elf->IsValidPc(0x27ff)); |
| 1171 | EXPECT_TRUE(elf->IsValidPc(0x2800)); |
| 1172 | EXPECT_TRUE(elf->IsValidPc(0x2900)); |
| 1173 | EXPECT_TRUE(elf->IsValidPc(0x29ff)); |
| 1174 | EXPECT_FALSE(elf->IsValidPc(0x2a00)); |
| 1175 | } |
| 1176 | |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1177 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 1178 | void ElfInterfaceTest::BuildID() { |
| 1179 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 1180 | |
| 1181 | uint64_t offset = 0x2000; |
| 1182 | |
| 1183 | Ehdr ehdr = {}; |
| 1184 | ehdr.e_shoff = offset; |
| 1185 | ehdr.e_shnum = 3; |
| 1186 | ehdr.e_shentsize = sizeof(Shdr); |
| 1187 | ehdr.e_shstrndx = 2; |
| 1188 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1189 | |
| 1190 | offset += ehdr.e_shentsize; |
| 1191 | |
| 1192 | char note_section[128]; |
| 1193 | Nhdr note_header = {}; |
| 1194 | note_header.n_namesz = 4; // "GNU" |
| 1195 | note_header.n_descsz = 8; // "BUILDID" |
| 1196 | note_header.n_type = NT_GNU_BUILD_ID; |
| 1197 | memcpy(¬e_section, ¬e_header, sizeof(note_header)); |
| 1198 | size_t note_offset = sizeof(note_header); |
| 1199 | memcpy(¬e_section[note_offset], "GNU", sizeof("GNU")); |
| 1200 | note_offset += sizeof("GNU"); |
| 1201 | memcpy(¬e_section[note_offset], "BUILDID", sizeof("BUILDID")); |
| 1202 | note_offset += sizeof("BUILDID"); |
| 1203 | |
| 1204 | Shdr shdr = {}; |
| 1205 | shdr.sh_type = SHT_NOTE; |
| 1206 | shdr.sh_name = 0x500; |
| 1207 | shdr.sh_offset = 0xb000; |
| 1208 | shdr.sh_size = sizeof(note_section); |
| 1209 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 1210 | offset += ehdr.e_shentsize; |
| 1211 | |
| 1212 | // The string data for section header names. |
| 1213 | memset(&shdr, 0, sizeof(shdr)); |
| 1214 | shdr.sh_type = SHT_STRTAB; |
| 1215 | shdr.sh_name = 0x20000; |
| 1216 | shdr.sh_offset = 0xf000; |
| 1217 | shdr.sh_size = 0x1000; |
| 1218 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 1219 | offset += ehdr.e_shentsize; |
| 1220 | |
| 1221 | memory_.SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id")); |
| 1222 | memory_.SetMemory(0xb000, note_section, sizeof(note_section)); |
| 1223 | |
| 1224 | uint64_t load_bias = 0; |
| 1225 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 1226 | ASSERT_EQ("BUILDID", elf->GetBuildID()); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 1230 | void ElfInterfaceTest::BuildIDTwoNotes() { |
| 1231 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 1232 | |
| 1233 | uint64_t offset = 0x2000; |
| 1234 | |
| 1235 | Ehdr ehdr = {}; |
| 1236 | ehdr.e_shoff = offset; |
| 1237 | ehdr.e_shnum = 3; |
| 1238 | ehdr.e_shentsize = sizeof(Shdr); |
| 1239 | ehdr.e_shstrndx = 2; |
| 1240 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1241 | |
| 1242 | offset += ehdr.e_shentsize; |
| 1243 | |
| 1244 | char note_section[128]; |
| 1245 | Nhdr note_header = {}; |
| 1246 | note_header.n_namesz = 8; // "WRONG" aligned to 4 |
| 1247 | note_header.n_descsz = 8; // "BUILDID" |
| 1248 | note_header.n_type = NT_GNU_BUILD_ID; |
| 1249 | memcpy(¬e_section, ¬e_header, sizeof(note_header)); |
| 1250 | size_t note_offset = sizeof(note_header); |
| 1251 | memcpy(¬e_section[note_offset], "WRONG", sizeof("WRONG")); |
| 1252 | note_offset += 8; |
| 1253 | memcpy(¬e_section[note_offset], "BUILDID", sizeof("BUILDID")); |
| 1254 | note_offset += sizeof("BUILDID"); |
| 1255 | |
| 1256 | note_header.n_namesz = 4; // "GNU" |
| 1257 | note_header.n_descsz = 8; // "BUILDID" |
| 1258 | note_header.n_type = NT_GNU_BUILD_ID; |
| 1259 | memcpy(¬e_section[note_offset], ¬e_header, sizeof(note_header)); |
| 1260 | note_offset += sizeof(note_header); |
| 1261 | memcpy(¬e_section[note_offset], "GNU", sizeof("GNU")); |
| 1262 | note_offset += sizeof("GNU"); |
| 1263 | memcpy(¬e_section[note_offset], "BUILDID", sizeof("BUILDID")); |
| 1264 | note_offset += sizeof("BUILDID"); |
| 1265 | |
| 1266 | Shdr shdr = {}; |
| 1267 | shdr.sh_type = SHT_NOTE; |
| 1268 | shdr.sh_name = 0x500; |
| 1269 | shdr.sh_offset = 0xb000; |
| 1270 | shdr.sh_size = sizeof(note_section); |
| 1271 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 1272 | offset += ehdr.e_shentsize; |
| 1273 | |
| 1274 | // The string data for section header names. |
| 1275 | memset(&shdr, 0, sizeof(shdr)); |
| 1276 | shdr.sh_type = SHT_STRTAB; |
| 1277 | shdr.sh_name = 0x20000; |
| 1278 | shdr.sh_offset = 0xf000; |
| 1279 | shdr.sh_size = 0x1000; |
| 1280 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 1281 | offset += ehdr.e_shentsize; |
| 1282 | |
| 1283 | memory_.SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id")); |
| 1284 | memory_.SetMemory(0xb000, note_section, sizeof(note_section)); |
| 1285 | |
| 1286 | uint64_t load_bias = 0; |
| 1287 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 1288 | ASSERT_EQ("BUILDID", elf->GetBuildID()); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 1292 | void ElfInterfaceTest::BuildIDSectionTooSmallForName () { |
| 1293 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 1294 | |
| 1295 | uint64_t offset = 0x2000; |
| 1296 | |
| 1297 | Ehdr ehdr = {}; |
| 1298 | ehdr.e_shoff = offset; |
| 1299 | ehdr.e_shnum = 3; |
| 1300 | ehdr.e_shentsize = sizeof(Shdr); |
| 1301 | ehdr.e_shstrndx = 2; |
| 1302 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1303 | |
| 1304 | offset += ehdr.e_shentsize; |
| 1305 | |
| 1306 | char note_section[128]; |
| 1307 | Nhdr note_header = {}; |
| 1308 | note_header.n_namesz = 4; // "GNU" |
| 1309 | note_header.n_descsz = 8; // "BUILDID" |
| 1310 | note_header.n_type = NT_GNU_BUILD_ID; |
| 1311 | memcpy(¬e_section, ¬e_header, sizeof(note_header)); |
| 1312 | size_t note_offset = sizeof(note_header); |
| 1313 | memcpy(¬e_section[note_offset], "GNU", sizeof("GNU")); |
| 1314 | note_offset += sizeof("GNU"); |
| 1315 | memcpy(¬e_section[note_offset], "BUILDID", sizeof("BUILDID")); |
| 1316 | note_offset += sizeof("BUILDID"); |
| 1317 | |
| 1318 | Shdr shdr = {}; |
| 1319 | shdr.sh_type = SHT_NOTE; |
| 1320 | shdr.sh_name = 0x500; |
| 1321 | shdr.sh_offset = 0xb000; |
| 1322 | shdr.sh_size = sizeof(note_header) + 1; |
| 1323 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 1324 | offset += ehdr.e_shentsize; |
| 1325 | |
| 1326 | // The string data for section header names. |
| 1327 | memset(&shdr, 0, sizeof(shdr)); |
| 1328 | shdr.sh_type = SHT_STRTAB; |
| 1329 | shdr.sh_name = 0x20000; |
| 1330 | shdr.sh_offset = 0xf000; |
| 1331 | shdr.sh_size = 0x1000; |
| 1332 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 1333 | offset += ehdr.e_shentsize; |
| 1334 | |
| 1335 | memory_.SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id")); |
| 1336 | memory_.SetMemory(0xb000, note_section, sizeof(note_section)); |
| 1337 | |
| 1338 | uint64_t load_bias = 0; |
| 1339 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 1340 | ASSERT_EQ("", elf->GetBuildID()); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 1344 | void ElfInterfaceTest::BuildIDSectionTooSmallForDesc () { |
| 1345 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 1346 | |
| 1347 | uint64_t offset = 0x2000; |
| 1348 | |
| 1349 | Ehdr ehdr = {}; |
| 1350 | ehdr.e_shoff = offset; |
| 1351 | ehdr.e_shnum = 3; |
| 1352 | ehdr.e_shentsize = sizeof(Shdr); |
| 1353 | ehdr.e_shstrndx = 2; |
| 1354 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1355 | |
| 1356 | offset += ehdr.e_shentsize; |
| 1357 | |
| 1358 | char note_section[128]; |
| 1359 | Nhdr note_header = {}; |
| 1360 | note_header.n_namesz = 4; // "GNU" |
| 1361 | note_header.n_descsz = 8; // "BUILDID" |
| 1362 | note_header.n_type = NT_GNU_BUILD_ID; |
| 1363 | memcpy(¬e_section, ¬e_header, sizeof(note_header)); |
| 1364 | size_t note_offset = sizeof(note_header); |
| 1365 | memcpy(¬e_section[note_offset], "GNU", sizeof("GNU")); |
| 1366 | note_offset += sizeof("GNU"); |
| 1367 | memcpy(¬e_section[note_offset], "BUILDID", sizeof("BUILDID")); |
| 1368 | note_offset += sizeof("BUILDID"); |
| 1369 | |
| 1370 | Shdr shdr = {}; |
| 1371 | shdr.sh_type = SHT_NOTE; |
| 1372 | shdr.sh_name = 0x500; |
| 1373 | shdr.sh_offset = 0xb000; |
| 1374 | shdr.sh_size = sizeof(note_header) + sizeof("GNU") + 1; |
| 1375 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 1376 | offset += ehdr.e_shentsize; |
| 1377 | |
| 1378 | // The string data for section header names. |
| 1379 | memset(&shdr, 0, sizeof(shdr)); |
| 1380 | shdr.sh_type = SHT_STRTAB; |
| 1381 | shdr.sh_name = 0x20000; |
| 1382 | shdr.sh_offset = 0xf000; |
| 1383 | shdr.sh_size = 0x1000; |
| 1384 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 1385 | offset += ehdr.e_shentsize; |
| 1386 | |
| 1387 | memory_.SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id")); |
| 1388 | memory_.SetMemory(0xb000, note_section, sizeof(note_section)); |
| 1389 | |
| 1390 | uint64_t load_bias = 0; |
| 1391 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 1392 | ASSERT_EQ("", elf->GetBuildID()); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | template <typename Ehdr, typename Shdr, typename Nhdr, typename ElfInterfaceType> |
| 1396 | void ElfInterfaceTest::BuildIDSectionTooSmallForHeader () { |
| 1397 | std::unique_ptr<ElfInterfaceType> elf(new ElfInterfaceType(&memory_)); |
| 1398 | |
| 1399 | uint64_t offset = 0x2000; |
| 1400 | |
| 1401 | Ehdr ehdr = {}; |
| 1402 | ehdr.e_shoff = offset; |
| 1403 | ehdr.e_shnum = 3; |
| 1404 | ehdr.e_shentsize = sizeof(Shdr); |
| 1405 | ehdr.e_shstrndx = 2; |
| 1406 | memory_.SetMemory(0, &ehdr, sizeof(ehdr)); |
| 1407 | |
| 1408 | offset += ehdr.e_shentsize; |
| 1409 | |
| 1410 | char note_section[128]; |
| 1411 | Nhdr note_header = {}; |
| 1412 | note_header.n_namesz = 4; // "GNU" |
| 1413 | note_header.n_descsz = 8; // "BUILDID" |
| 1414 | note_header.n_type = NT_GNU_BUILD_ID; |
| 1415 | memcpy(¬e_section, ¬e_header, sizeof(note_header)); |
| 1416 | size_t note_offset = sizeof(note_header); |
| 1417 | memcpy(¬e_section[note_offset], "GNU", sizeof("GNU")); |
| 1418 | note_offset += sizeof("GNU"); |
| 1419 | memcpy(¬e_section[note_offset], "BUILDID", sizeof("BUILDID")); |
| 1420 | note_offset += sizeof("BUILDID"); |
| 1421 | |
| 1422 | Shdr shdr = {}; |
| 1423 | shdr.sh_type = SHT_NOTE; |
| 1424 | shdr.sh_name = 0x500; |
| 1425 | shdr.sh_offset = 0xb000; |
| 1426 | shdr.sh_size = sizeof(note_header) - 1; |
| 1427 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 1428 | offset += ehdr.e_shentsize; |
| 1429 | |
| 1430 | // The string data for section header names. |
| 1431 | memset(&shdr, 0, sizeof(shdr)); |
| 1432 | shdr.sh_type = SHT_STRTAB; |
| 1433 | shdr.sh_name = 0x20000; |
| 1434 | shdr.sh_offset = 0xf000; |
| 1435 | shdr.sh_size = 0x1000; |
| 1436 | memory_.SetMemory(offset, &shdr, sizeof(shdr)); |
| 1437 | offset += ehdr.e_shentsize; |
| 1438 | |
| 1439 | memory_.SetMemory(0xf500, ".note.gnu.build-id", sizeof(".note.gnu.build-id")); |
| 1440 | memory_.SetMemory(0xb000, note_section, sizeof(note_section)); |
| 1441 | |
| 1442 | uint64_t load_bias = 0; |
| 1443 | ASSERT_TRUE(elf->Init(&load_bias)); |
Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 1444 | ASSERT_EQ("", elf->GetBuildID()); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | TEST_F(ElfInterfaceTest, build_id32) { |
| 1448 | BuildID<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr, ElfInterface32>(); |
| 1449 | } |
| 1450 | |
| 1451 | TEST_F(ElfInterfaceTest, build_id64) { |
| 1452 | BuildID<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr, ElfInterface64>(); |
| 1453 | } |
| 1454 | |
| 1455 | TEST_F(ElfInterfaceTest, build_id_two_notes32) { |
| 1456 | BuildIDTwoNotes<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr, ElfInterface32>(); |
| 1457 | } |
| 1458 | |
| 1459 | TEST_F(ElfInterfaceTest, build_id_two_notes64) { |
| 1460 | BuildIDTwoNotes<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr, ElfInterface64>(); |
| 1461 | } |
| 1462 | |
| 1463 | TEST_F(ElfInterfaceTest, build_id_section_too_small_for_name32) { |
| 1464 | BuildIDSectionTooSmallForName<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr, ElfInterface32>(); |
| 1465 | } |
| 1466 | |
| 1467 | TEST_F(ElfInterfaceTest, build_id_section_too_small_for_name64) { |
| 1468 | BuildIDSectionTooSmallForName<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr, ElfInterface64>(); |
| 1469 | } |
| 1470 | |
| 1471 | TEST_F(ElfInterfaceTest, build_id_section_too_small_for_desc32) { |
| 1472 | BuildIDSectionTooSmallForDesc<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr, ElfInterface32>(); |
| 1473 | } |
| 1474 | |
| 1475 | TEST_F(ElfInterfaceTest, build_id_section_too_small_for_desc64) { |
| 1476 | BuildIDSectionTooSmallForDesc<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr, ElfInterface64>(); |
| 1477 | } |
| 1478 | |
| 1479 | TEST_F(ElfInterfaceTest, build_id_section_too_small_for_header32) { |
| 1480 | BuildIDSectionTooSmallForHeader<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr, ElfInterface32>(); |
| 1481 | } |
| 1482 | |
| 1483 | TEST_F(ElfInterfaceTest, build_id_section_too_small_for_header64) { |
| 1484 | BuildIDSectionTooSmallForHeader<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr, ElfInterface64>(); |
| 1485 | } |
| 1486 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 1487 | } // namespace unwindstack |