Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #include <stdint.h> |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <string> |
| 22 | |
Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 23 | #include <7zCrc.h> |
| 24 | #include <Xz.h> |
| 25 | #include <XzCrc64.h> |
| 26 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 27 | #include <unwindstack/DwarfSection.h> |
| 28 | #include <unwindstack/ElfInterface.h> |
| 29 | #include <unwindstack/Log.h> |
| 30 | #include <unwindstack/Memory.h> |
| 31 | #include <unwindstack/Regs.h> |
| 32 | |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 33 | #include "DwarfDebugFrame.h" |
| 34 | #include "DwarfEhFrame.h" |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 35 | #include "DwarfEhFrameWithHdr.h" |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 36 | #include "Symbols.h" |
| 37 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 38 | namespace unwindstack { |
| 39 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 40 | ElfInterface::~ElfInterface() { |
| 41 | for (auto symbol : symbols_) { |
| 42 | delete symbol; |
| 43 | } |
| 44 | } |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 45 | |
Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 46 | Memory* ElfInterface::CreateGnuDebugdataMemory() { |
| 47 | if (gnu_debugdata_offset_ == 0 || gnu_debugdata_size_ == 0) { |
| 48 | return nullptr; |
| 49 | } |
| 50 | |
| 51 | // TODO: Only call these initialization functions once. |
| 52 | CrcGenerateTable(); |
| 53 | Crc64GenerateTable(); |
| 54 | |
| 55 | std::vector<uint8_t> src(gnu_debugdata_size_); |
| 56 | if (!memory_->Read(gnu_debugdata_offset_, src.data(), gnu_debugdata_size_)) { |
| 57 | gnu_debugdata_offset_ = 0; |
| 58 | gnu_debugdata_size_ = static_cast<uint64_t>(-1); |
| 59 | return nullptr; |
| 60 | } |
| 61 | |
| 62 | ISzAlloc alloc; |
| 63 | CXzUnpacker state; |
| 64 | alloc.Alloc = [](void*, size_t size) { return malloc(size); }; |
| 65 | alloc.Free = [](void*, void* ptr) { return free(ptr); }; |
| 66 | |
| 67 | XzUnpacker_Construct(&state, &alloc); |
| 68 | |
| 69 | std::unique_ptr<MemoryBuffer> dst(new MemoryBuffer); |
| 70 | int return_val; |
| 71 | size_t src_offset = 0; |
| 72 | size_t dst_offset = 0; |
| 73 | ECoderStatus status; |
| 74 | dst->Resize(5 * gnu_debugdata_size_); |
| 75 | do { |
| 76 | size_t src_remaining = src.size() - src_offset; |
| 77 | size_t dst_remaining = dst->Size() - dst_offset; |
| 78 | if (dst_remaining < 2 * gnu_debugdata_size_) { |
| 79 | dst->Resize(dst->Size() + 2 * gnu_debugdata_size_); |
| 80 | dst_remaining += 2 * gnu_debugdata_size_; |
| 81 | } |
| 82 | return_val = XzUnpacker_Code(&state, dst->GetPtr(dst_offset), &dst_remaining, &src[src_offset], |
| 83 | &src_remaining, CODER_FINISH_ANY, &status); |
| 84 | src_offset += src_remaining; |
| 85 | dst_offset += dst_remaining; |
| 86 | } while (return_val == SZ_OK && status == CODER_STATUS_NOT_FINISHED); |
| 87 | XzUnpacker_Free(&state); |
| 88 | if (return_val != SZ_OK || !XzUnpacker_IsStreamWasFinished(&state)) { |
| 89 | gnu_debugdata_offset_ = 0; |
| 90 | gnu_debugdata_size_ = static_cast<uint64_t>(-1); |
| 91 | return nullptr; |
| 92 | } |
| 93 | |
| 94 | // Shrink back down to the exact size. |
| 95 | dst->Resize(dst_offset); |
| 96 | |
| 97 | return dst.release(); |
| 98 | } |
| 99 | |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 100 | template <typename AddressType> |
| 101 | void ElfInterface::InitHeadersWithTemplate() { |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 102 | if (eh_frame_hdr_offset_ != 0) { |
| 103 | eh_frame_.reset(new DwarfEhFrameWithHdr<AddressType>(memory_)); |
| 104 | if (!eh_frame_->Init(eh_frame_hdr_offset_, eh_frame_hdr_size_)) { |
| 105 | // Even if the eh_frame_offset_ is non-zero, do not bother |
| 106 | // trying to read that since something has gone wrong. |
| 107 | eh_frame_.reset(nullptr); |
| 108 | eh_frame_hdr_offset_ = 0; |
| 109 | eh_frame_hdr_size_ = static_cast<uint64_t>(-1); |
| 110 | } |
| 111 | } else if (eh_frame_offset_ != 0) { |
| 112 | // If there is a eh_frame section without a eh_frame_hdr section. |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 113 | eh_frame_.reset(new DwarfEhFrame<AddressType>(memory_)); |
| 114 | if (!eh_frame_->Init(eh_frame_offset_, eh_frame_size_)) { |
| 115 | eh_frame_.reset(nullptr); |
| 116 | eh_frame_offset_ = 0; |
| 117 | eh_frame_size_ = static_cast<uint64_t>(-1); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if (debug_frame_offset_ != 0) { |
| 122 | debug_frame_.reset(new DwarfDebugFrame<AddressType>(memory_)); |
| 123 | if (!debug_frame_->Init(debug_frame_offset_, debug_frame_size_)) { |
| 124 | debug_frame_.reset(nullptr); |
| 125 | debug_frame_offset_ = 0; |
| 126 | debug_frame_size_ = static_cast<uint64_t>(-1); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 131 | template <typename EhdrType, typename PhdrType, typename ShdrType> |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 132 | bool ElfInterface::ReadAllHeaders(uint64_t* load_bias) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 133 | EhdrType ehdr; |
| 134 | if (!memory_->Read(0, &ehdr, sizeof(ehdr))) { |
| 135 | return false; |
| 136 | } |
| 137 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 138 | if (!ReadProgramHeaders<EhdrType, PhdrType>(ehdr, load_bias)) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 139 | return false; |
| 140 | } |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 141 | |
| 142 | // We could still potentially unwind without the section header |
| 143 | // information, so ignore any errors. |
| 144 | if (!ReadSectionHeaders<EhdrType, ShdrType>(ehdr)) { |
| 145 | log(0, "Malformed section header found, ignoring..."); |
| 146 | } |
| 147 | return true; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | template <typename EhdrType, typename PhdrType> |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 151 | bool ElfInterface::ReadProgramHeaders(const EhdrType& ehdr, uint64_t* load_bias) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 152 | uint64_t offset = ehdr.e_phoff; |
| 153 | for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) { |
| 154 | PhdrType phdr; |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 155 | if (!memory_->ReadField(offset, &phdr, &phdr.p_type, sizeof(phdr.p_type))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 156 | return false; |
| 157 | } |
| 158 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 159 | if (HandleType(offset, phdr.p_type, *load_bias)) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 160 | continue; |
| 161 | } |
| 162 | |
| 163 | switch (phdr.p_type) { |
| 164 | case PT_LOAD: |
| 165 | { |
| 166 | // Get the flags first, if this isn't an executable header, ignore it. |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 167 | if (!memory_->ReadField(offset, &phdr, &phdr.p_flags, sizeof(phdr.p_flags))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | if ((phdr.p_flags & PF_X) == 0) { |
| 171 | continue; |
| 172 | } |
| 173 | |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 174 | if (!memory_->ReadField(offset, &phdr, &phdr.p_vaddr, sizeof(phdr.p_vaddr))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 175 | return false; |
| 176 | } |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 177 | if (!memory_->ReadField(offset, &phdr, &phdr.p_offset, sizeof(phdr.p_offset))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 178 | return false; |
| 179 | } |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 180 | if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 181 | return false; |
| 182 | } |
| 183 | pt_loads_[phdr.p_offset] = LoadInfo{phdr.p_offset, phdr.p_vaddr, |
| 184 | static_cast<size_t>(phdr.p_memsz)}; |
| 185 | if (phdr.p_offset == 0) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 186 | *load_bias = phdr.p_vaddr; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 187 | } |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | case PT_GNU_EH_FRAME: |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 192 | if (!memory_->ReadField(offset, &phdr, &phdr.p_offset, sizeof(phdr.p_offset))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 193 | return false; |
| 194 | } |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 195 | // This is really the pointer to the .eh_frame_hdr section. |
| 196 | eh_frame_hdr_offset_ = phdr.p_offset; |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 197 | if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 198 | return false; |
| 199 | } |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 200 | eh_frame_hdr_size_ = phdr.p_memsz; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 201 | break; |
| 202 | |
| 203 | case PT_DYNAMIC: |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 204 | if (!memory_->ReadField(offset, &phdr, &phdr.p_offset, sizeof(phdr.p_offset))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 205 | return false; |
| 206 | } |
| 207 | dynamic_offset_ = phdr.p_offset; |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 208 | if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | dynamic_size_ = phdr.p_memsz; |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | template <typename EhdrType, typename ShdrType> |
| 219 | bool ElfInterface::ReadSectionHeaders(const EhdrType& ehdr) { |
| 220 | uint64_t offset = ehdr.e_shoff; |
| 221 | uint64_t sec_offset = 0; |
| 222 | uint64_t sec_size = 0; |
| 223 | |
| 224 | // Get the location of the section header names. |
| 225 | // If something is malformed in the header table data, we aren't going |
| 226 | // to terminate, we'll simply ignore this part. |
| 227 | ShdrType shdr; |
| 228 | if (ehdr.e_shstrndx < ehdr.e_shnum) { |
| 229 | uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize; |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 230 | if (memory_->ReadField(sh_offset, &shdr, &shdr.sh_offset, sizeof(shdr.sh_offset)) && |
| 231 | memory_->ReadField(sh_offset, &shdr, &shdr.sh_size, sizeof(shdr.sh_size))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 232 | sec_offset = shdr.sh_offset; |
| 233 | sec_size = shdr.sh_size; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // Skip the first header, it's always going to be NULL. |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 238 | offset += ehdr.e_shentsize; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 239 | for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) { |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 240 | if (!memory_->ReadField(offset, &shdr, &shdr.sh_type, sizeof(shdr.sh_type))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 244 | if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) { |
| 245 | if (!memory_->Read(offset, &shdr, sizeof(shdr))) { |
| 246 | return false; |
| 247 | } |
| 248 | // Need to go get the information about the section that contains |
| 249 | // the string terminated names. |
| 250 | ShdrType str_shdr; |
| 251 | if (shdr.sh_link >= ehdr.e_shnum) { |
| 252 | return false; |
| 253 | } |
| 254 | uint64_t str_offset = ehdr.e_shoff + shdr.sh_link * ehdr.e_shentsize; |
| 255 | if (!memory_->ReadField(str_offset, &str_shdr, &str_shdr.sh_type, sizeof(str_shdr.sh_type))) { |
| 256 | return false; |
| 257 | } |
| 258 | if (str_shdr.sh_type != SHT_STRTAB) { |
| 259 | return false; |
| 260 | } |
| 261 | if (!memory_->ReadField(str_offset, &str_shdr, &str_shdr.sh_offset, |
| 262 | sizeof(str_shdr.sh_offset))) { |
| 263 | return false; |
| 264 | } |
| 265 | if (!memory_->ReadField(str_offset, &str_shdr, &str_shdr.sh_size, sizeof(str_shdr.sh_size))) { |
| 266 | return false; |
| 267 | } |
| 268 | symbols_.push_back(new Symbols(shdr.sh_offset, shdr.sh_size, shdr.sh_entsize, |
| 269 | str_shdr.sh_offset, str_shdr.sh_size)); |
| 270 | } else if (shdr.sh_type == SHT_PROGBITS && sec_size != 0) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 271 | // Look for the .debug_frame and .gnu_debugdata. |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 272 | if (!memory_->ReadField(offset, &shdr, &shdr.sh_name, sizeof(shdr.sh_name))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 273 | return false; |
| 274 | } |
| 275 | if (shdr.sh_name < sec_size) { |
| 276 | std::string name; |
| 277 | if (memory_->ReadString(sec_offset + shdr.sh_name, &name)) { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 278 | uint64_t* offset_ptr = nullptr; |
| 279 | uint64_t* size_ptr = nullptr; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 280 | if (name == ".debug_frame") { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 281 | offset_ptr = &debug_frame_offset_; |
| 282 | size_ptr = &debug_frame_size_; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 283 | } else if (name == ".gnu_debugdata") { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 284 | offset_ptr = &gnu_debugdata_offset_; |
| 285 | size_ptr = &gnu_debugdata_size_; |
Christopher Ferris | c9dee84 | 2017-11-03 14:50:27 -0700 | [diff] [blame] | 286 | } else if (name == ".eh_frame") { |
| 287 | offset_ptr = &eh_frame_offset_; |
| 288 | size_ptr = &eh_frame_size_; |
| 289 | } else if (eh_frame_hdr_offset_ == 0 && name == ".eh_frame_hdr") { |
| 290 | offset_ptr = &eh_frame_hdr_offset_; |
| 291 | size_ptr = &eh_frame_hdr_size_; |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 292 | } |
| 293 | if (offset_ptr != nullptr && |
| 294 | memory_->ReadField(offset, &shdr, &shdr.sh_offset, sizeof(shdr.sh_offset)) && |
| 295 | memory_->ReadField(offset, &shdr, &shdr.sh_size, sizeof(shdr.sh_size))) { |
| 296 | *offset_ptr = shdr.sh_offset; |
| 297 | *size_ptr = shdr.sh_size; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | template <typename DynType> |
| 307 | bool ElfInterface::GetSonameWithTemplate(std::string* soname) { |
| 308 | if (soname_type_ == SONAME_INVALID) { |
| 309 | return false; |
| 310 | } |
| 311 | if (soname_type_ == SONAME_VALID) { |
| 312 | *soname = soname_; |
| 313 | return true; |
| 314 | } |
| 315 | |
| 316 | soname_type_ = SONAME_INVALID; |
| 317 | |
| 318 | uint64_t soname_offset = 0; |
| 319 | uint64_t strtab_offset = 0; |
| 320 | uint64_t strtab_size = 0; |
| 321 | |
| 322 | // Find the soname location from the dynamic headers section. |
| 323 | DynType dyn; |
| 324 | uint64_t offset = dynamic_offset_; |
| 325 | uint64_t max_offset = offset + dynamic_size_; |
| 326 | for (uint64_t offset = dynamic_offset_; offset < max_offset; offset += sizeof(DynType)) { |
| 327 | if (!memory_->Read(offset, &dyn, sizeof(dyn))) { |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | if (dyn.d_tag == DT_STRTAB) { |
| 332 | strtab_offset = dyn.d_un.d_ptr; |
| 333 | } else if (dyn.d_tag == DT_STRSZ) { |
| 334 | strtab_size = dyn.d_un.d_val; |
| 335 | } else if (dyn.d_tag == DT_SONAME) { |
| 336 | soname_offset = dyn.d_un.d_val; |
| 337 | } else if (dyn.d_tag == DT_NULL) { |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | soname_offset += strtab_offset; |
| 343 | if (soname_offset >= strtab_offset + strtab_size) { |
| 344 | return false; |
| 345 | } |
| 346 | if (!memory_->ReadString(soname_offset, &soname_)) { |
| 347 | return false; |
| 348 | } |
| 349 | soname_type_ = SONAME_VALID; |
| 350 | *soname = soname_; |
| 351 | return true; |
| 352 | } |
| 353 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 354 | template <typename SymType> |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 355 | bool ElfInterface::GetFunctionNameWithTemplate(uint64_t addr, uint64_t load_bias, std::string* name, |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 356 | uint64_t* func_offset) { |
| 357 | if (symbols_.empty()) { |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | for (const auto symbol : symbols_) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 362 | if (symbol->GetName<SymType>(addr, load_bias, memory_, name, func_offset)) { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 363 | return true; |
| 364 | } |
| 365 | } |
| 366 | return false; |
| 367 | } |
| 368 | |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 369 | bool ElfInterface::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 370 | // Try the eh_frame first. |
| 371 | DwarfSection* eh_frame = eh_frame_.get(); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 372 | if (eh_frame != nullptr && eh_frame->Step(pc, regs, process_memory, finished)) { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 373 | return true; |
| 374 | } |
| 375 | |
| 376 | // Try the debug_frame next. |
| 377 | DwarfSection* debug_frame = debug_frame_.get(); |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 378 | if (debug_frame != nullptr && debug_frame->Step(pc, regs, process_memory, finished)) { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 379 | return true; |
| 380 | } |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 381 | return false; |
| 382 | } |
| 383 | |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 384 | // This is an estimation of the size of the elf file using the location |
| 385 | // of the section headers and size. This assumes that the section headers |
| 386 | // are at the end of the elf file. If the elf has a load bias, the size |
| 387 | // will be too large, but this is acceptable. |
| 388 | template <typename EhdrType> |
| 389 | void ElfInterface::GetMaxSizeWithTemplate(Memory* memory, uint64_t* size) { |
| 390 | EhdrType ehdr; |
| 391 | if (!memory->Read(0, &ehdr, sizeof(ehdr))) { |
| 392 | return; |
| 393 | } |
| 394 | if (ehdr.e_shnum == 0) { |
| 395 | return; |
| 396 | } |
| 397 | *size = ehdr.e_shoff + ehdr.e_shentsize * ehdr.e_shnum; |
| 398 | } |
| 399 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 400 | // Instantiate all of the needed template functions. |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 401 | template void ElfInterface::InitHeadersWithTemplate<uint32_t>(); |
| 402 | template void ElfInterface::InitHeadersWithTemplate<uint64_t>(); |
| 403 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 404 | template bool ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(uint64_t*); |
| 405 | template bool ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(uint64_t*); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 406 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 407 | template bool ElfInterface::ReadProgramHeaders<Elf32_Ehdr, Elf32_Phdr>(const Elf32_Ehdr&, uint64_t*); |
| 408 | template bool ElfInterface::ReadProgramHeaders<Elf64_Ehdr, Elf64_Phdr>(const Elf64_Ehdr&, uint64_t*); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 409 | |
| 410 | template bool ElfInterface::ReadSectionHeaders<Elf32_Ehdr, Elf32_Shdr>(const Elf32_Ehdr&); |
| 411 | template bool ElfInterface::ReadSectionHeaders<Elf64_Ehdr, Elf64_Shdr>(const Elf64_Ehdr&); |
| 412 | |
| 413 | template bool ElfInterface::GetSonameWithTemplate<Elf32_Dyn>(std::string*); |
| 414 | template bool ElfInterface::GetSonameWithTemplate<Elf64_Dyn>(std::string*); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 415 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 416 | template bool ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(uint64_t, uint64_t, std::string*, |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 417 | uint64_t*); |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 418 | template bool ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(uint64_t, uint64_t, std::string*, |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame] | 419 | uint64_t*); |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 420 | |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 421 | template void ElfInterface::GetMaxSizeWithTemplate<Elf32_Ehdr>(Memory*, uint64_t*); |
| 422 | template void ElfInterface::GetMaxSizeWithTemplate<Elf64_Ehdr>(Memory*, uint64_t*); |
| 423 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 424 | } // namespace unwindstack |