| 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 | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 56 | interface_->InitHeaders(load_bias_); | 
| 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. | 
|  | 80 | uint64_t load_bias; | 
|  | 81 | if (gnu->Init(&load_bias)) { | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 82 | gnu->InitHeaders(load_bias); | 
| 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 | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 115 | bool Elf::GetGlobalVariable(const std::string& name, uint64_t* memory_address) { | 
|  | 116 | if (!valid_) { | 
|  | 117 | return false; | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | if (!interface_->GetGlobalVariable(name, memory_address) && | 
|  | 121 | (gnu_debugdata_interface_ == nullptr || | 
|  | 122 | !gnu_debugdata_interface_->GetGlobalVariable(name, memory_address))) { | 
|  | 123 | return false; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | // Adjust by the load bias. | 
|  | 127 | if (*memory_address < load_bias_) { | 
|  | 128 | return false; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | *memory_address -= load_bias_; | 
|  | 132 |  | 
|  | 133 | // If this winds up in the dynamic section, then we might need to adjust | 
|  | 134 | // the address. | 
|  | 135 | uint64_t dynamic_end = interface_->dynamic_vaddr() + interface_->dynamic_size(); | 
|  | 136 | if (*memory_address >= interface_->dynamic_vaddr() && *memory_address < dynamic_end) { | 
|  | 137 | if (interface_->dynamic_vaddr() > interface_->dynamic_offset()) { | 
|  | 138 | *memory_address -= interface_->dynamic_vaddr() - interface_->dynamic_offset(); | 
|  | 139 | } else { | 
|  | 140 | *memory_address += interface_->dynamic_offset() - interface_->dynamic_vaddr(); | 
|  | 141 | } | 
|  | 142 | } | 
|  | 143 | return true; | 
|  | 144 | } | 
|  | 145 |  | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 146 | std::string Elf::GetBuildID() { | 
|  | 147 | if (!valid_) { | 
|  | 148 | return ""; | 
|  | 149 | } | 
|  | 150 | return interface_->GetBuildID(); | 
| Florian Mayer | da459e5 | 2018-11-23 16:56:17 +0000 | [diff] [blame] | 151 | } | 
|  | 152 |  | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 153 | void Elf::GetLastError(ErrorData* data) { | 
|  | 154 | if (valid_) { | 
|  | 155 | *data = interface_->last_error(); | 
|  | 156 | } | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | ErrorCode Elf::GetLastErrorCode() { | 
|  | 160 | if (valid_) { | 
|  | 161 | return interface_->LastErrorCode(); | 
|  | 162 | } | 
|  | 163 | return ERROR_NONE; | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | uint64_t Elf::GetLastErrorAddress() { | 
|  | 167 | if (valid_) { | 
|  | 168 | return interface_->LastErrorAddress(); | 
|  | 169 | } | 
|  | 170 | return 0; | 
|  | 171 | } | 
|  | 172 |  | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 173 | // 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] | 174 | bool Elf::Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, Regs* regs, Memory* process_memory, | 
|  | 175 | bool* finished) { | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 176 | if (!valid_) { | 
|  | 177 | return false; | 
|  | 178 | } | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 179 |  | 
|  | 180 | // 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] | 181 | if (regs->StepIfSignalHandler(rel_pc, this, process_memory)) { | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 182 | *finished = false; | 
|  | 183 | return true; | 
|  | 184 | } | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 185 |  | 
| Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 186 | // Lock during the step which can update information in the object. | 
|  | 187 | std::lock_guard<std::mutex> guard(lock_); | 
| Christopher Ferris | 4cc36d2 | 2018-06-06 14:47:31 -0700 | [diff] [blame] | 188 | return interface_->Step(adjusted_rel_pc, regs, process_memory, finished); | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 189 | } | 
|  | 190 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 191 | bool Elf::IsValidElf(Memory* memory) { | 
|  | 192 | if (memory == nullptr) { | 
|  | 193 | return false; | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | // Verify that this is a valid elf file. | 
|  | 197 | uint8_t e_ident[SELFMAG + 1]; | 
| Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 198 | if (!memory->ReadFully(0, e_ident, SELFMAG)) { | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 199 | return false; | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) { | 
|  | 203 | return false; | 
|  | 204 | } | 
|  | 205 | return true; | 
|  | 206 | } | 
|  | 207 |  | 
| Christopher Ferris | a2f38f1 | 2018-10-09 18:49:11 -0700 | [diff] [blame] | 208 | bool Elf::GetInfo(Memory* memory, uint64_t* size) { | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 209 | if (!IsValidElf(memory)) { | 
| Christopher Ferris | a2f38f1 | 2018-10-09 18:49:11 -0700 | [diff] [blame] | 210 | return false; | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 211 | } | 
|  | 212 | *size = 0; | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 213 |  | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 214 | uint8_t class_type; | 
| Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 215 | if (!memory->ReadFully(EI_CLASS, &class_type, 1)) { | 
| Christopher Ferris | a2f38f1 | 2018-10-09 18:49:11 -0700 | [diff] [blame] | 216 | return false; | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 217 | } | 
| Christopher Ferris | a2f38f1 | 2018-10-09 18:49:11 -0700 | [diff] [blame] | 218 |  | 
|  | 219 | // Get the maximum size of the elf data from the header. | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 220 | if (class_type == ELFCLASS32) { | 
|  | 221 | ElfInterface32::GetMaxSize(memory, size); | 
|  | 222 | } else if (class_type == ELFCLASS64) { | 
|  | 223 | ElfInterface64::GetMaxSize(memory, size); | 
|  | 224 | } else { | 
| Christopher Ferris | a2f38f1 | 2018-10-09 18:49:11 -0700 | [diff] [blame] | 225 | return false; | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 226 | } | 
| Christopher Ferris | a2f38f1 | 2018-10-09 18:49:11 -0700 | [diff] [blame] | 227 | return true; | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 228 | } | 
|  | 229 |  | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 230 | bool Elf::IsValidPc(uint64_t pc) { | 
|  | 231 | if (!valid_ || pc < load_bias_) { | 
|  | 232 | return false; | 
|  | 233 | } | 
| Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 234 |  | 
|  | 235 | if (interface_->IsValidPc(pc)) { | 
|  | 236 | return true; | 
|  | 237 | } | 
|  | 238 |  | 
|  | 239 | if (gnu_debugdata_interface_ != nullptr && gnu_debugdata_interface_->IsValidPc(pc)) { | 
|  | 240 | return true; | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | return false; | 
|  | 244 | } | 
|  | 245 |  | 
| David Srbecky | 85b5fec | 2018-02-23 18:06:13 +0000 | [diff] [blame] | 246 | bool Elf::GetTextRange(uint64_t* addr, uint64_t* size) { | 
|  | 247 | if (!valid_) { | 
|  | 248 | return false; | 
|  | 249 | } | 
|  | 250 |  | 
|  | 251 | if (interface_->GetTextRange(addr, size)) { | 
|  | 252 | *addr += load_bias_; | 
|  | 253 | return true; | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | if (gnu_debugdata_interface_ != nullptr && gnu_debugdata_interface_->GetTextRange(addr, size)) { | 
|  | 257 | *addr += load_bias_; | 
|  | 258 | return true; | 
|  | 259 | } | 
|  | 260 |  | 
|  | 261 | return false; | 
|  | 262 | } | 
|  | 263 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 264 | ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) { | 
|  | 265 | if (!IsValidElf(memory)) { | 
|  | 266 | return nullptr; | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | std::unique_ptr<ElfInterface> interface; | 
| Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 270 | if (!memory->ReadFully(EI_CLASS, &class_type_, 1)) { | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 271 | return nullptr; | 
|  | 272 | } | 
|  | 273 | if (class_type_ == ELFCLASS32) { | 
|  | 274 | Elf32_Half e_machine; | 
| Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 275 | 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] | 276 | return nullptr; | 
|  | 277 | } | 
|  | 278 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 279 | machine_type_ = e_machine; | 
|  | 280 | if (e_machine == EM_ARM) { | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 281 | arch_ = ARCH_ARM; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 282 | interface.reset(new ElfInterfaceArm(memory)); | 
| Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 283 | } else if (e_machine == EM_386) { | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 284 | arch_ = ARCH_X86; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 285 | interface.reset(new ElfInterface32(memory)); | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 286 | } else if (e_machine == EM_MIPS) { | 
|  | 287 | arch_ = ARCH_MIPS; | 
|  | 288 | interface.reset(new ElfInterface32(memory)); | 
| Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 289 | } else { | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 290 | // Unsupported. | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 291 | 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] | 292 | return nullptr; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 293 | } | 
|  | 294 | } else if (class_type_ == ELFCLASS64) { | 
|  | 295 | Elf64_Half e_machine; | 
| Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 296 | 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] | 297 | return nullptr; | 
|  | 298 | } | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 299 |  | 
|  | 300 | machine_type_ = e_machine; | 
|  | 301 | if (e_machine == EM_AARCH64) { | 
|  | 302 | arch_ = ARCH_ARM64; | 
|  | 303 | } else if (e_machine == EM_X86_64) { | 
|  | 304 | arch_ = ARCH_X86_64; | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 305 | } else if (e_machine == EM_MIPS) { | 
|  | 306 | arch_ = ARCH_MIPS64; | 
| Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 307 | } else { | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 308 | // Unsupported. | 
| Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 309 | ALOGI("64 bit elf that is neither aarch64 nor x86_64 nor mips64: e_machine = %d\n", | 
|  | 310 | e_machine); | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 311 | return nullptr; | 
|  | 312 | } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 313 | interface.reset(new ElfInterface64(memory)); | 
|  | 314 | } | 
|  | 315 |  | 
|  | 316 | return interface.release(); | 
|  | 317 | } | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 318 |  | 
| Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 319 | uint64_t Elf::GetLoadBias(Memory* memory) { | 
|  | 320 | if (!IsValidElf(memory)) { | 
|  | 321 | return 0; | 
|  | 322 | } | 
|  | 323 |  | 
|  | 324 | uint8_t class_type; | 
|  | 325 | if (!memory->Read(EI_CLASS, &class_type, 1)) { | 
|  | 326 | return 0; | 
|  | 327 | } | 
|  | 328 |  | 
|  | 329 | if (class_type == ELFCLASS32) { | 
|  | 330 | return ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(memory); | 
|  | 331 | } else if (class_type == ELFCLASS64) { | 
|  | 332 | return ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(memory); | 
|  | 333 | } | 
|  | 334 | return 0; | 
|  | 335 | } | 
|  | 336 |  | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 337 | void Elf::SetCachingEnabled(bool enable) { | 
|  | 338 | if (!cache_enabled_ && enable) { | 
|  | 339 | cache_enabled_ = true; | 
| Christopher Ferris | d9575b6 | 2018-02-16 13:48:19 -0800 | [diff] [blame] | 340 | 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] | 341 | cache_lock_ = new std::mutex; | 
|  | 342 | } else if (cache_enabled_ && !enable) { | 
|  | 343 | cache_enabled_ = false; | 
|  | 344 | delete cache_; | 
|  | 345 | delete cache_lock_; | 
|  | 346 | } | 
|  | 347 | } | 
|  | 348 |  | 
|  | 349 | void Elf::CacheLock() { | 
|  | 350 | cache_lock_->lock(); | 
|  | 351 | } | 
|  | 352 |  | 
|  | 353 | void Elf::CacheUnlock() { | 
|  | 354 | cache_lock_->unlock(); | 
|  | 355 | } | 
|  | 356 |  | 
|  | 357 | void Elf::CacheAdd(MapInfo* info) { | 
| Christopher Ferris | d9575b6 | 2018-02-16 13:48:19 -0800 | [diff] [blame] | 358 | // If elf_offset != 0, then cache both name:offset and name. | 
|  | 359 | // The cached name is used to do lookups if multiple maps for the same | 
|  | 360 | // named elf file exist. | 
|  | 361 | // For example, if there are two maps boot.odex:1000 and boot.odex:2000 | 
|  | 362 | // where each reference the entire boot.odex, the cache will properly | 
|  | 363 | // use the same cached elf object. | 
|  | 364 |  | 
|  | 365 | if (info->offset == 0 || info->elf_offset != 0) { | 
|  | 366 | (*cache_)[info->name] = std::make_pair(info->elf, true); | 
|  | 367 | } | 
|  | 368 |  | 
|  | 369 | if (info->offset != 0) { | 
|  | 370 | // The second element in the pair indicates whether elf_offset should | 
|  | 371 | // be set to offset when getting out of the cache. | 
|  | 372 | (*cache_)[info->name + ':' + std::to_string(info->offset)] = | 
|  | 373 | std::make_pair(info->elf, info->elf_offset != 0); | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 374 | } | 
|  | 375 | } | 
|  | 376 |  | 
| Christopher Ferris | d9575b6 | 2018-02-16 13:48:19 -0800 | [diff] [blame] | 377 | bool Elf::CacheAfterCreateMemory(MapInfo* info) { | 
|  | 378 | if (info->name.empty() || info->offset == 0 || info->elf_offset == 0) { | 
|  | 379 | return false; | 
|  | 380 | } | 
|  | 381 |  | 
|  | 382 | auto entry = cache_->find(info->name); | 
|  | 383 | if (entry == cache_->end()) { | 
|  | 384 | return false; | 
|  | 385 | } | 
|  | 386 |  | 
|  | 387 | // In this case, the whole file is the elf, and the name has already | 
|  | 388 | // been cached. Add an entry at name:offset to get this directly out | 
|  | 389 | // of the cache next time. | 
|  | 390 | info->elf = entry->second.first; | 
|  | 391 | (*cache_)[info->name + ':' + std::to_string(info->offset)] = std::make_pair(info->elf, true); | 
|  | 392 | return true; | 
|  | 393 | } | 
|  | 394 |  | 
|  | 395 | bool Elf::CacheGet(MapInfo* info) { | 
|  | 396 | std::string name(info->name); | 
|  | 397 | if (info->offset != 0) { | 
|  | 398 | name += ':' + std::to_string(info->offset); | 
|  | 399 | } | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 400 | auto entry = cache_->find(name); | 
|  | 401 | if (entry != cache_->end()) { | 
| Christopher Ferris | d9575b6 | 2018-02-16 13:48:19 -0800 | [diff] [blame] | 402 | info->elf = entry->second.first; | 
|  | 403 | if (entry->second.second) { | 
|  | 404 | info->elf_offset = info->offset; | 
|  | 405 | } | 
| Christopher Ferris | 0b79ae1 | 2018-01-25 12:15:56 -0800 | [diff] [blame] | 406 | return true; | 
|  | 407 | } | 
|  | 408 | return false; | 
|  | 409 | } | 
|  | 410 |  | 
| Christopher Ferris | bf373ed | 2019-01-16 17:23:39 -0800 | [diff] [blame] | 411 | std::string Elf::GetBuildID(Memory* memory) { | 
|  | 412 | if (!IsValidElf(memory)) { | 
|  | 413 | return ""; | 
|  | 414 | } | 
|  | 415 |  | 
|  | 416 | uint8_t class_type; | 
|  | 417 | if (!memory->Read(EI_CLASS, &class_type, 1)) { | 
|  | 418 | return ""; | 
|  | 419 | } | 
|  | 420 |  | 
|  | 421 | if (class_type == ELFCLASS32) { | 
|  | 422 | return ElfInterface::ReadBuildIDFromMemory<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr>(memory); | 
|  | 423 | } else if (class_type == ELFCLASS64) { | 
|  | 424 | return ElfInterface::ReadBuildIDFromMemory<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr>(memory); | 
|  | 425 | } | 
|  | 426 | return ""; | 
|  | 427 | } | 
|  | 428 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 429 | }  // namespace unwindstack |