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 | #include <string.h> |
| 19 | |
| 20 | #include <memory> |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 21 | #include <mutex> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 22 | #include <string> |
Christopher Ferris | d9575b6 | 2018-02-16 13:48:19 -0800 | [diff] [blame] | 23 | #include <utility> |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 24 | |
| 25 | #define LOG_TAG "unwind" |
| 26 | #include <log/log.h> |
| 27 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 28 | #include <unwindstack/Elf.h> |
| 29 | #include <unwindstack/ElfInterface.h> |
| 30 | #include <unwindstack/MapInfo.h> |
| 31 | #include <unwindstack/Memory.h> |
| 32 | #include <unwindstack/Regs.h> |
| 33 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 34 | #include "ElfInterfaceArm.h" |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 35 | #include "Symbols.h" |
| 36 | |
| 37 | namespace unwindstack { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 38 | |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 39 | bool Elf::cache_enabled_; |
Christopher Ferris | d9575b6 | 2018-02-16 13:48:19 -0800 | [diff] [blame] | 40 | std::unordered_map<std::string, std::pair<std::shared_ptr<Elf>, bool>>* Elf::cache_; |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 41 | std::mutex* Elf::cache_lock_; |
| 42 | |
Christopher Ferris | e8c4ecf | 2018-10-23 12:04:26 -0700 | [diff] [blame] | 43 | bool Elf::Init() { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 44 | load_bias_ = 0; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 45 | if (!memory_) { |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | interface_.reset(CreateInterfaceFromMemory(memory_.get())); |
| 50 | if (!interface_) { |
| 51 | return false; |
| 52 | } |
| 53 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 54 | valid_ = interface_->Init(&load_bias_); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 55 | if (valid_) { |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 56 | interface_->InitHeaders(); |
Christopher Ferris | e8c4ecf | 2018-10-23 12:04:26 -0700 | [diff] [blame] | 57 | InitGnuDebugdata(); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 58 | } else { |
| 59 | interface_.reset(nullptr); |
| 60 | } |
| 61 | return valid_; |
| 62 | } |
| 63 | |
Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 64 | // It is expensive to initialize the .gnu_debugdata section. Provide a method |
| 65 | // to initialize this data separately. |
| 66 | void Elf::InitGnuDebugdata() { |
| 67 | if (!valid_ || interface_->gnu_debugdata_offset() == 0) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | gnu_debugdata_memory_.reset(interface_->CreateGnuDebugdataMemory()); |
| 72 | gnu_debugdata_interface_.reset(CreateInterfaceFromMemory(gnu_debugdata_memory_.get())); |
| 73 | ElfInterface* gnu = gnu_debugdata_interface_.get(); |
| 74 | if (gnu == nullptr) { |
| 75 | return; |
| 76 | } |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 77 | |
| 78 | // Ignore the load_bias from the compressed section, the correct load bias |
| 79 | // is in the uncompressed data. |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 80 | int64_t load_bias; |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 81 | if (gnu->Init(&load_bias)) { |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 82 | gnu->InitHeaders(); |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 83 | interface_->SetGnuDebugdataInterface(gnu); |
Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 84 | } else { |
| 85 | // Free all of the memory associated with the gnu_debugdata section. |
| 86 | gnu_debugdata_memory_.reset(nullptr); |
| 87 | gnu_debugdata_interface_.reset(nullptr); |
| 88 | } |
| 89 | } |
| 90 | |
Christopher Ferris | 4568f4b | 2018-10-23 17:42:41 -0700 | [diff] [blame] | 91 | void Elf::Invalidate() { |
| 92 | interface_.reset(nullptr); |
| 93 | valid_ = false; |
| 94 | } |
| 95 | |
Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 96 | std::string Elf::GetSoname() { |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 97 | std::lock_guard<std::mutex> guard(lock_); |
Christopher Ferris | 02a6c44 | 2019-03-11 14:43:33 -0700 | [diff] [blame] | 98 | if (!valid_) { |
| 99 | return ""; |
| 100 | } |
| 101 | return interface_->GetSoname(); |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | uint64_t Elf::GetRelPc(uint64_t pc, const MapInfo* map_info) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 105 | return pc - map_info->start + load_bias_ + map_info->elf_offset; |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | bool Elf::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) { |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 109 | std::lock_guard<std::mutex> guard(lock_); |
Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 110 | return valid_ && (interface_->GetFunctionName(addr, name, func_offset) || |
| 111 | (gnu_debugdata_interface_ && |
| 112 | gnu_debugdata_interface_->GetFunctionName(addr, name, func_offset))); |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 115 | bool Elf::GetGlobalVariableOffset(const std::string& name, uint64_t* memory_offset) { |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 116 | if (!valid_) { |
| 117 | return false; |
| 118 | } |
| 119 | |
Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 120 | uint64_t vaddr; |
| 121 | if (!interface_->GetGlobalVariable(name, &vaddr) && |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 122 | (gnu_debugdata_interface_ == nullptr || |
Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 123 | !gnu_debugdata_interface_->GetGlobalVariable(name, &vaddr))) { |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 127 | // Check the .data section. |
| 128 | uint64_t vaddr_start = interface_->data_vaddr_start(); |
| 129 | if (vaddr >= vaddr_start && vaddr < interface_->data_vaddr_end()) { |
| 130 | *memory_offset = vaddr - vaddr_start + interface_->data_offset(); |
| 131 | return true; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 134 | // Check the .dynamic section. |
| 135 | vaddr_start = interface_->dynamic_vaddr_start(); |
| 136 | if (vaddr >= vaddr_start && vaddr < interface_->dynamic_vaddr_end()) { |
| 137 | *memory_offset = vaddr - vaddr_start + interface_->dynamic_offset(); |
| 138 | return true; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 139 | } |
Christopher Ferris | df683b7 | 2019-12-03 17:13:49 -0800 | [diff] [blame] | 140 | |
| 141 | return false; |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 144 | std::string Elf::GetBuildID() { |
| 145 | if (!valid_) { |
| 146 | return ""; |
| 147 | } |
| 148 | return interface_->GetBuildID(); |
Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 151 | void Elf::GetLastError(ErrorData* data) { |
| 152 | if (valid_) { |
| 153 | *data = interface_->last_error(); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | ErrorCode Elf::GetLastErrorCode() { |
| 158 | if (valid_) { |
| 159 | return interface_->LastErrorCode(); |
| 160 | } |
Christopher Ferris | d11ed86 | 2019-04-11 19:45:35 -0700 | [diff] [blame] | 161 | return ERROR_INVALID_ELF; |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | uint64_t Elf::GetLastErrorAddress() { |
| 165 | if (valid_) { |
| 166 | return interface_->LastErrorAddress(); |
| 167 | } |
| 168 | return 0; |
| 169 | } |
| 170 | |
Christopher Ferris | d11ed86 | 2019-04-11 19:45:35 -0700 | [diff] [blame] | 171 | // The relative pc expectd by this function is relative to the start of the elf. |
| 172 | bool Elf::StepIfSignalHandler(uint64_t rel_pc, Regs* regs, Memory* process_memory) { |
| 173 | if (!valid_) { |
| 174 | return false; |
| 175 | } |
Christopher Ferris | f0c82e7 | 2019-12-04 13:37:11 -0800 | [diff] [blame] | 176 | |
| 177 | // Convert the rel_pc to an elf_offset. |
| 178 | if (rel_pc < static_cast<uint64_t>(load_bias_)) { |
| 179 | return false; |
| 180 | } |
| 181 | return regs->StepIfSignalHandler(rel_pc - load_bias_, this, process_memory); |
Christopher Ferris | d11ed86 | 2019-04-11 19:45:35 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 184 | // The relative pc is always relative to the start of the map from which it comes. |
Christopher Ferris | d11ed86 | 2019-04-11 19:45:35 -0700 | [diff] [blame] | 185 | bool Elf::Step(uint64_t rel_pc, Regs* regs, Memory* process_memory, bool* finished) { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 186 | if (!valid_) { |
| 187 | return false; |
| 188 | } |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 189 | |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 190 | // Lock during the step which can update information in the object. |
| 191 | std::lock_guard<std::mutex> guard(lock_); |
Christopher Ferris | d11ed86 | 2019-04-11 19:45:35 -0700 | [diff] [blame] | 192 | return interface_->Step(rel_pc, regs, process_memory, finished); |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 195 | bool Elf::IsValidElf(Memory* memory) { |
| 196 | if (memory == nullptr) { |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | // Verify that this is a valid elf file. |
| 201 | uint8_t e_ident[SELFMAG + 1]; |
Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 202 | if (!memory->ReadFully(0, e_ident, SELFMAG)) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 203 | return false; |
| 204 | } |
| 205 | |
| 206 | if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) { |
| 207 | return false; |
| 208 | } |
| 209 | return true; |
| 210 | } |
| 211 | |
Christopher Ferris | a2f38f1 | 2018-10-09 18:49:11 -0700 | [diff] [blame] | 212 | bool Elf::GetInfo(Memory* memory, uint64_t* size) { |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 213 | if (!IsValidElf(memory)) { |
Christopher Ferris | a2f38f1 | 2018-10-09 18:49:11 -0700 | [diff] [blame] | 214 | return false; |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 215 | } |
| 216 | *size = 0; |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 217 | |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 218 | uint8_t class_type; |
Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 219 | if (!memory->ReadFully(EI_CLASS, &class_type, 1)) { |
Christopher Ferris | a2f38f1 | 2018-10-09 18:49:11 -0700 | [diff] [blame] | 220 | return false; |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 221 | } |
Christopher Ferris | a2f38f1 | 2018-10-09 18:49:11 -0700 | [diff] [blame] | 222 | |
| 223 | // Get the maximum size of the elf data from the header. |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 224 | if (class_type == ELFCLASS32) { |
| 225 | ElfInterface32::GetMaxSize(memory, size); |
| 226 | } else if (class_type == ELFCLASS64) { |
| 227 | ElfInterface64::GetMaxSize(memory, size); |
| 228 | } else { |
Christopher Ferris | a2f38f1 | 2018-10-09 18:49:11 -0700 | [diff] [blame] | 229 | return false; |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 230 | } |
Christopher Ferris | a2f38f1 | 2018-10-09 18:49:11 -0700 | [diff] [blame] | 231 | return true; |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 234 | bool Elf::IsValidPc(uint64_t pc) { |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 235 | if (!valid_ || (load_bias_ > 0 && pc < static_cast<uint64_t>(load_bias_))) { |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 236 | return false; |
| 237 | } |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 238 | |
| 239 | if (interface_->IsValidPc(pc)) { |
| 240 | return true; |
| 241 | } |
| 242 | |
| 243 | if (gnu_debugdata_interface_ != nullptr && gnu_debugdata_interface_->IsValidPc(pc)) { |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | return false; |
| 248 | } |
| 249 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 250 | ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) { |
| 251 | if (!IsValidElf(memory)) { |
| 252 | return nullptr; |
| 253 | } |
| 254 | |
| 255 | std::unique_ptr<ElfInterface> interface; |
Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 256 | if (!memory->ReadFully(EI_CLASS, &class_type_, 1)) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 257 | return nullptr; |
| 258 | } |
| 259 | if (class_type_ == ELFCLASS32) { |
| 260 | Elf32_Half e_machine; |
Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 261 | if (!memory->ReadFully(EI_NIDENT + sizeof(Elf32_Half), &e_machine, sizeof(e_machine))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 262 | return nullptr; |
| 263 | } |
| 264 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 265 | machine_type_ = e_machine; |
| 266 | if (e_machine == EM_ARM) { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 267 | arch_ = ARCH_ARM; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 268 | interface.reset(new ElfInterfaceArm(memory)); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 269 | } else if (e_machine == EM_386) { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 270 | arch_ = ARCH_X86; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 271 | interface.reset(new ElfInterface32(memory)); |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 272 | } else if (e_machine == EM_MIPS) { |
| 273 | arch_ = ARCH_MIPS; |
| 274 | interface.reset(new ElfInterface32(memory)); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 275 | } else { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 276 | // Unsupported. |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 277 | ALOGI("32 bit elf that is neither arm nor x86 nor mips: e_machine = %d\n", e_machine); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 278 | return nullptr; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 279 | } |
| 280 | } else if (class_type_ == ELFCLASS64) { |
| 281 | Elf64_Half e_machine; |
Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 282 | if (!memory->ReadFully(EI_NIDENT + sizeof(Elf64_Half), &e_machine, sizeof(e_machine))) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 283 | return nullptr; |
| 284 | } |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 285 | |
| 286 | machine_type_ = e_machine; |
| 287 | if (e_machine == EM_AARCH64) { |
| 288 | arch_ = ARCH_ARM64; |
| 289 | } else if (e_machine == EM_X86_64) { |
| 290 | arch_ = ARCH_X86_64; |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 291 | } else if (e_machine == EM_MIPS) { |
| 292 | arch_ = ARCH_MIPS64; |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 293 | } else { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 294 | // Unsupported. |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 295 | ALOGI("64 bit elf that is neither aarch64 nor x86_64 nor mips64: e_machine = %d\n", |
| 296 | e_machine); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 297 | return nullptr; |
| 298 | } |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 299 | interface.reset(new ElfInterface64(memory)); |
| 300 | } |
| 301 | |
| 302 | return interface.release(); |
| 303 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 304 | |
Christopher Ferris | 819f131 | 2019-10-03 13:35:48 -0700 | [diff] [blame] | 305 | int64_t Elf::GetLoadBias(Memory* memory) { |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 306 | if (!IsValidElf(memory)) { |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | uint8_t class_type; |
| 311 | if (!memory->Read(EI_CLASS, &class_type, 1)) { |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | if (class_type == ELFCLASS32) { |
| 316 | return ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(memory); |
| 317 | } else if (class_type == ELFCLASS64) { |
| 318 | return ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(memory); |
| 319 | } |
| 320 | return 0; |
| 321 | } |
| 322 | |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 323 | void Elf::SetCachingEnabled(bool enable) { |
| 324 | if (!cache_enabled_ && enable) { |
| 325 | cache_enabled_ = true; |
Christopher Ferris | d9575b6 | 2018-02-16 13:48:19 -0800 | [diff] [blame] | 326 | cache_ = new std::unordered_map<std::string, std::pair<std::shared_ptr<Elf>, bool>>; |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 327 | cache_lock_ = new std::mutex; |
| 328 | } else if (cache_enabled_ && !enable) { |
| 329 | cache_enabled_ = false; |
| 330 | delete cache_; |
| 331 | delete cache_lock_; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void Elf::CacheLock() { |
| 336 | cache_lock_->lock(); |
| 337 | } |
| 338 | |
| 339 | void Elf::CacheUnlock() { |
| 340 | cache_lock_->unlock(); |
| 341 | } |
| 342 | |
| 343 | void Elf::CacheAdd(MapInfo* info) { |
Christopher Ferris | d9575b6 | 2018-02-16 13:48:19 -0800 | [diff] [blame] | 344 | // If elf_offset != 0, then cache both name:offset and name. |
| 345 | // The cached name is used to do lookups if multiple maps for the same |
| 346 | // named elf file exist. |
| 347 | // For example, if there are two maps boot.odex:1000 and boot.odex:2000 |
| 348 | // where each reference the entire boot.odex, the cache will properly |
| 349 | // use the same cached elf object. |
| 350 | |
| 351 | if (info->offset == 0 || info->elf_offset != 0) { |
| 352 | (*cache_)[info->name] = std::make_pair(info->elf, true); |
| 353 | } |
| 354 | |
| 355 | if (info->offset != 0) { |
| 356 | // The second element in the pair indicates whether elf_offset should |
| 357 | // be set to offset when getting out of the cache. |
| 358 | (*cache_)[info->name + ':' + std::to_string(info->offset)] = |
| 359 | std::make_pair(info->elf, info->elf_offset != 0); |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
Christopher Ferris | d9575b6 | 2018-02-16 13:48:19 -0800 | [diff] [blame] | 363 | bool Elf::CacheAfterCreateMemory(MapInfo* info) { |
| 364 | if (info->name.empty() || info->offset == 0 || info->elf_offset == 0) { |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | auto entry = cache_->find(info->name); |
| 369 | if (entry == cache_->end()) { |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | // In this case, the whole file is the elf, and the name has already |
| 374 | // been cached. Add an entry at name:offset to get this directly out |
| 375 | // of the cache next time. |
| 376 | info->elf = entry->second.first; |
| 377 | (*cache_)[info->name + ':' + std::to_string(info->offset)] = std::make_pair(info->elf, true); |
| 378 | return true; |
| 379 | } |
| 380 | |
| 381 | bool Elf::CacheGet(MapInfo* info) { |
| 382 | std::string name(info->name); |
| 383 | if (info->offset != 0) { |
| 384 | name += ':' + std::to_string(info->offset); |
| 385 | } |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 386 | auto entry = cache_->find(name); |
| 387 | if (entry != cache_->end()) { |
Christopher Ferris | d9575b6 | 2018-02-16 13:48:19 -0800 | [diff] [blame] | 388 | info->elf = entry->second.first; |
| 389 | if (entry->second.second) { |
| 390 | info->elf_offset = info->offset; |
| 391 | } |
Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 392 | return true; |
| 393 | } |
| 394 | return false; |
| 395 | } |
| 396 | |
Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 397 | std::string Elf::GetBuildID(Memory* memory) { |
| 398 | if (!IsValidElf(memory)) { |
| 399 | return ""; |
| 400 | } |
| 401 | |
| 402 | uint8_t class_type; |
| 403 | if (!memory->Read(EI_CLASS, &class_type, 1)) { |
| 404 | return ""; |
| 405 | } |
| 406 | |
| 407 | if (class_type == ELFCLASS32) { |
| 408 | return ElfInterface::ReadBuildIDFromMemory<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr>(memory); |
| 409 | } else if (class_type == ELFCLASS64) { |
| 410 | return ElfInterface::ReadBuildIDFromMemory<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr>(memory); |
| 411 | } |
| 412 | return ""; |
| 413 | } |
| 414 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 415 | } // namespace unwindstack |