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