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 | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 23 | #include "DwarfDebugFrame.h" |
| 24 | #include "DwarfEhFrame.h" |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame^] | 25 | #include "DwarfSection.h" |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 26 | #include "ElfInterface.h" |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame^] | 27 | #include "Log.h" |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 28 | #include "Memory.h" |
| 29 | #include "Regs.h" |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame^] | 30 | #include "Symbols.h" |
| 31 | |
| 32 | ElfInterface::~ElfInterface() { |
| 33 | for (auto symbol : symbols_) { |
| 34 | delete symbol; |
| 35 | } |
| 36 | } |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 37 | |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 38 | template <typename AddressType> |
| 39 | void ElfInterface::InitHeadersWithTemplate() { |
| 40 | if (eh_frame_offset_ != 0) { |
| 41 | eh_frame_.reset(new DwarfEhFrame<AddressType>(memory_)); |
| 42 | if (!eh_frame_->Init(eh_frame_offset_, eh_frame_size_)) { |
| 43 | eh_frame_.reset(nullptr); |
| 44 | eh_frame_offset_ = 0; |
| 45 | eh_frame_size_ = static_cast<uint64_t>(-1); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | if (debug_frame_offset_ != 0) { |
| 50 | debug_frame_.reset(new DwarfDebugFrame<AddressType>(memory_)); |
| 51 | if (!debug_frame_->Init(debug_frame_offset_, debug_frame_size_)) { |
| 52 | debug_frame_.reset(nullptr); |
| 53 | debug_frame_offset_ = 0; |
| 54 | debug_frame_size_ = static_cast<uint64_t>(-1); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 59 | template <typename EhdrType, typename PhdrType, typename ShdrType> |
| 60 | bool ElfInterface::ReadAllHeaders() { |
| 61 | EhdrType ehdr; |
| 62 | if (!memory_->Read(0, &ehdr, sizeof(ehdr))) { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | if (!ReadProgramHeaders<EhdrType, PhdrType>(ehdr)) { |
| 67 | return false; |
| 68 | } |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame^] | 69 | |
| 70 | // We could still potentially unwind without the section header |
| 71 | // information, so ignore any errors. |
| 72 | if (!ReadSectionHeaders<EhdrType, ShdrType>(ehdr)) { |
| 73 | log(0, "Malformed section header found, ignoring..."); |
| 74 | } |
| 75 | return true; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | template <typename EhdrType, typename PhdrType> |
| 79 | bool ElfInterface::ReadProgramHeaders(const EhdrType& ehdr) { |
| 80 | uint64_t offset = ehdr.e_phoff; |
| 81 | for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) { |
| 82 | PhdrType phdr; |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 83 | if (!memory_->ReadField(offset, &phdr, &phdr.p_type, sizeof(phdr.p_type))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 84 | return false; |
| 85 | } |
| 86 | |
| 87 | if (HandleType(offset, phdr.p_type)) { |
| 88 | continue; |
| 89 | } |
| 90 | |
| 91 | switch (phdr.p_type) { |
| 92 | case PT_LOAD: |
| 93 | { |
| 94 | // 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] | 95 | if (!memory_->ReadField(offset, &phdr, &phdr.p_flags, sizeof(phdr.p_flags))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | if ((phdr.p_flags & PF_X) == 0) { |
| 99 | continue; |
| 100 | } |
| 101 | |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 102 | if (!memory_->ReadField(offset, &phdr, &phdr.p_vaddr, sizeof(phdr.p_vaddr))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 103 | return false; |
| 104 | } |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 105 | if (!memory_->ReadField(offset, &phdr, &phdr.p_offset, sizeof(phdr.p_offset))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 106 | return false; |
| 107 | } |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 108 | if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 109 | return false; |
| 110 | } |
| 111 | pt_loads_[phdr.p_offset] = LoadInfo{phdr.p_offset, phdr.p_vaddr, |
| 112 | static_cast<size_t>(phdr.p_memsz)}; |
| 113 | if (phdr.p_offset == 0) { |
| 114 | load_bias_ = phdr.p_vaddr; |
| 115 | } |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | case PT_GNU_EH_FRAME: |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 120 | if (!memory_->ReadField(offset, &phdr, &phdr.p_offset, sizeof(phdr.p_offset))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | eh_frame_offset_ = phdr.p_offset; |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 124 | if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | eh_frame_size_ = phdr.p_memsz; |
| 128 | break; |
| 129 | |
| 130 | case PT_DYNAMIC: |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 131 | if (!memory_->ReadField(offset, &phdr, &phdr.p_offset, sizeof(phdr.p_offset))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 132 | return false; |
| 133 | } |
| 134 | dynamic_offset_ = phdr.p_offset; |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 135 | if (!memory_->ReadField(offset, &phdr, &phdr.p_memsz, sizeof(phdr.p_memsz))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | dynamic_size_ = phdr.p_memsz; |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | template <typename EhdrType, typename ShdrType> |
| 146 | bool ElfInterface::ReadSectionHeaders(const EhdrType& ehdr) { |
| 147 | uint64_t offset = ehdr.e_shoff; |
| 148 | uint64_t sec_offset = 0; |
| 149 | uint64_t sec_size = 0; |
| 150 | |
| 151 | // Get the location of the section header names. |
| 152 | // If something is malformed in the header table data, we aren't going |
| 153 | // to terminate, we'll simply ignore this part. |
| 154 | ShdrType shdr; |
| 155 | if (ehdr.e_shstrndx < ehdr.e_shnum) { |
| 156 | uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize; |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 157 | if (memory_->ReadField(sh_offset, &shdr, &shdr.sh_offset, sizeof(shdr.sh_offset)) && |
| 158 | memory_->ReadField(sh_offset, &shdr, &shdr.sh_size, sizeof(shdr.sh_size))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 159 | sec_offset = shdr.sh_offset; |
| 160 | sec_size = shdr.sh_size; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Skip the first header, it's always going to be NULL. |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame^] | 165 | offset += ehdr.e_shentsize; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 166 | 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] | 167 | if (!memory_->ReadField(offset, &shdr, &shdr.sh_type, sizeof(shdr.sh_type))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 168 | return false; |
| 169 | } |
| 170 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame^] | 171 | if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) { |
| 172 | if (!memory_->Read(offset, &shdr, sizeof(shdr))) { |
| 173 | return false; |
| 174 | } |
| 175 | // Need to go get the information about the section that contains |
| 176 | // the string terminated names. |
| 177 | ShdrType str_shdr; |
| 178 | if (shdr.sh_link >= ehdr.e_shnum) { |
| 179 | return false; |
| 180 | } |
| 181 | uint64_t str_offset = ehdr.e_shoff + shdr.sh_link * ehdr.e_shentsize; |
| 182 | if (!memory_->ReadField(str_offset, &str_shdr, &str_shdr.sh_type, sizeof(str_shdr.sh_type))) { |
| 183 | return false; |
| 184 | } |
| 185 | if (str_shdr.sh_type != SHT_STRTAB) { |
| 186 | return false; |
| 187 | } |
| 188 | if (!memory_->ReadField(str_offset, &str_shdr, &str_shdr.sh_offset, |
| 189 | sizeof(str_shdr.sh_offset))) { |
| 190 | return false; |
| 191 | } |
| 192 | if (!memory_->ReadField(str_offset, &str_shdr, &str_shdr.sh_size, sizeof(str_shdr.sh_size))) { |
| 193 | return false; |
| 194 | } |
| 195 | symbols_.push_back(new Symbols(shdr.sh_offset, shdr.sh_size, shdr.sh_entsize, |
| 196 | str_shdr.sh_offset, str_shdr.sh_size)); |
| 197 | } else if (shdr.sh_type == SHT_PROGBITS && sec_size != 0) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 198 | // Look for the .debug_frame and .gnu_debugdata. |
Christopher Ferris | f447c8e | 2017-04-03 12:39:47 -0700 | [diff] [blame] | 199 | if (!memory_->ReadField(offset, &shdr, &shdr.sh_name, sizeof(shdr.sh_name))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 200 | return false; |
| 201 | } |
| 202 | if (shdr.sh_name < sec_size) { |
| 203 | std::string name; |
| 204 | if (memory_->ReadString(sec_offset + shdr.sh_name, &name)) { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame^] | 205 | uint64_t* offset_ptr = nullptr; |
| 206 | uint64_t* size_ptr = nullptr; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 207 | if (name == ".debug_frame") { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame^] | 208 | offset_ptr = &debug_frame_offset_; |
| 209 | size_ptr = &debug_frame_size_; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 210 | } else if (name == ".gnu_debugdata") { |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame^] | 211 | offset_ptr = &gnu_debugdata_offset_; |
| 212 | size_ptr = &gnu_debugdata_size_; |
| 213 | } |
| 214 | if (offset_ptr != nullptr && |
| 215 | memory_->ReadField(offset, &shdr, &shdr.sh_offset, sizeof(shdr.sh_offset)) && |
| 216 | memory_->ReadField(offset, &shdr, &shdr.sh_size, sizeof(shdr.sh_size))) { |
| 217 | *offset_ptr = shdr.sh_offset; |
| 218 | *size_ptr = shdr.sh_size; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | template <typename DynType> |
| 228 | bool ElfInterface::GetSonameWithTemplate(std::string* soname) { |
| 229 | if (soname_type_ == SONAME_INVALID) { |
| 230 | return false; |
| 231 | } |
| 232 | if (soname_type_ == SONAME_VALID) { |
| 233 | *soname = soname_; |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | soname_type_ = SONAME_INVALID; |
| 238 | |
| 239 | uint64_t soname_offset = 0; |
| 240 | uint64_t strtab_offset = 0; |
| 241 | uint64_t strtab_size = 0; |
| 242 | |
| 243 | // Find the soname location from the dynamic headers section. |
| 244 | DynType dyn; |
| 245 | uint64_t offset = dynamic_offset_; |
| 246 | uint64_t max_offset = offset + dynamic_size_; |
| 247 | for (uint64_t offset = dynamic_offset_; offset < max_offset; offset += sizeof(DynType)) { |
| 248 | if (!memory_->Read(offset, &dyn, sizeof(dyn))) { |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | if (dyn.d_tag == DT_STRTAB) { |
| 253 | strtab_offset = dyn.d_un.d_ptr; |
| 254 | } else if (dyn.d_tag == DT_STRSZ) { |
| 255 | strtab_size = dyn.d_un.d_val; |
| 256 | } else if (dyn.d_tag == DT_SONAME) { |
| 257 | soname_offset = dyn.d_un.d_val; |
| 258 | } else if (dyn.d_tag == DT_NULL) { |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | soname_offset += strtab_offset; |
| 264 | if (soname_offset >= strtab_offset + strtab_size) { |
| 265 | return false; |
| 266 | } |
| 267 | if (!memory_->ReadString(soname_offset, &soname_)) { |
| 268 | return false; |
| 269 | } |
| 270 | soname_type_ = SONAME_VALID; |
| 271 | *soname = soname_; |
| 272 | return true; |
| 273 | } |
| 274 | |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame^] | 275 | template <typename SymType> |
| 276 | bool ElfInterface::GetFunctionNameWithTemplate(uint64_t addr, std::string* name, |
| 277 | uint64_t* func_offset) { |
| 278 | if (symbols_.empty()) { |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | for (const auto symbol : symbols_) { |
| 283 | if (symbol->GetName<SymType>(addr, load_bias_, memory_, name, func_offset)) { |
| 284 | return true; |
| 285 | } |
| 286 | } |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | bool ElfInterface::Step(uint64_t pc, Regs* regs, Memory* process_memory) { |
| 291 | // Need to subtract off the load_bias to get the correct pc. |
| 292 | if (pc < load_bias_) { |
| 293 | return false; |
| 294 | } |
| 295 | pc -= load_bias_; |
| 296 | |
| 297 | // Try the eh_frame first. |
| 298 | DwarfSection* eh_frame = eh_frame_.get(); |
| 299 | if (eh_frame != nullptr && eh_frame->Step(pc, regs, process_memory)) { |
| 300 | return true; |
| 301 | } |
| 302 | |
| 303 | // Try the debug_frame next. |
| 304 | DwarfSection* debug_frame = debug_frame_.get(); |
| 305 | if (debug_frame != nullptr && debug_frame->Step(pc, regs, process_memory)) { |
| 306 | return true; |
| 307 | } |
| 308 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 309 | return false; |
| 310 | } |
| 311 | |
| 312 | // Instantiate all of the needed template functions. |
Christopher Ferris | 61d4097 | 2017-06-12 19:14:20 -0700 | [diff] [blame] | 313 | template void ElfInterface::InitHeadersWithTemplate<uint32_t>(); |
| 314 | template void ElfInterface::InitHeadersWithTemplate<uint64_t>(); |
| 315 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 316 | template bool ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(); |
| 317 | template bool ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(); |
| 318 | |
| 319 | template bool ElfInterface::ReadProgramHeaders<Elf32_Ehdr, Elf32_Phdr>(const Elf32_Ehdr&); |
| 320 | template bool ElfInterface::ReadProgramHeaders<Elf64_Ehdr, Elf64_Phdr>(const Elf64_Ehdr&); |
| 321 | |
| 322 | template bool ElfInterface::ReadSectionHeaders<Elf32_Ehdr, Elf32_Shdr>(const Elf32_Ehdr&); |
| 323 | template bool ElfInterface::ReadSectionHeaders<Elf64_Ehdr, Elf64_Shdr>(const Elf64_Ehdr&); |
| 324 | |
| 325 | template bool ElfInterface::GetSonameWithTemplate<Elf32_Dyn>(std::string*); |
| 326 | template bool ElfInterface::GetSonameWithTemplate<Elf64_Dyn>(std::string*); |
Christopher Ferris | 8098b1c | 2017-06-20 13:54:08 -0700 | [diff] [blame^] | 327 | |
| 328 | template bool ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(uint64_t, std::string*, |
| 329 | uint64_t*); |
| 330 | template bool ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(uint64_t, std::string*, |
| 331 | uint64_t*); |