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> |
| 23 | |
| 24 | #define LOG_TAG "unwind" |
| 25 | #include <log/log.h> |
| 26 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 27 | #include <unwindstack/Elf.h> |
| 28 | #include <unwindstack/ElfInterface.h> |
| 29 | #include <unwindstack/MapInfo.h> |
| 30 | #include <unwindstack/Memory.h> |
| 31 | #include <unwindstack/Regs.h> |
| 32 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 33 | #include "ElfInterfaceArm.h" |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 34 | #include "Symbols.h" |
| 35 | |
| 36 | namespace unwindstack { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 37 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 38 | bool Elf::Init(bool init_gnu_debugdata) { |
| 39 | load_bias_ = 0; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 40 | if (!memory_) { |
| 41 | return false; |
| 42 | } |
| 43 | |
| 44 | interface_.reset(CreateInterfaceFromMemory(memory_.get())); |
| 45 | if (!interface_) { |
| 46 | return false; |
| 47 | } |
| 48 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 49 | valid_ = interface_->Init(&load_bias_); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 50 | if (valid_) { |
| 51 | interface_->InitHeaders(); |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 52 | if (init_gnu_debugdata) { |
| 53 | InitGnuDebugdata(); |
| 54 | } else { |
| 55 | gnu_debugdata_interface_.reset(nullptr); |
| 56 | } |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 57 | } else { |
| 58 | interface_.reset(nullptr); |
| 59 | } |
| 60 | return valid_; |
| 61 | } |
| 62 | |
Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 63 | // It is expensive to initialize the .gnu_debugdata section. Provide a method |
| 64 | // to initialize this data separately. |
| 65 | void Elf::InitGnuDebugdata() { |
| 66 | if (!valid_ || interface_->gnu_debugdata_offset() == 0) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | gnu_debugdata_memory_.reset(interface_->CreateGnuDebugdataMemory()); |
| 71 | gnu_debugdata_interface_.reset(CreateInterfaceFromMemory(gnu_debugdata_memory_.get())); |
| 72 | ElfInterface* gnu = gnu_debugdata_interface_.get(); |
| 73 | if (gnu == nullptr) { |
| 74 | return; |
| 75 | } |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 76 | |
| 77 | // Ignore the load_bias from the compressed section, the correct load bias |
| 78 | // is in the uncompressed data. |
| 79 | uint64_t load_bias; |
| 80 | if (gnu->Init(&load_bias)) { |
Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 81 | gnu->InitHeaders(); |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 82 | interface_->SetGnuDebugdataInterface(gnu); |
Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 83 | } else { |
| 84 | // Free all of the memory associated with the gnu_debugdata section. |
| 85 | gnu_debugdata_memory_.reset(nullptr); |
| 86 | gnu_debugdata_interface_.reset(nullptr); |
| 87 | } |
| 88 | } |
| 89 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 90 | bool Elf::GetSoname(std::string* name) { |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 91 | std::lock_guard<std::mutex> guard(lock_); |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 92 | return valid_ && interface_->GetSoname(name); |
| 93 | } |
| 94 | |
| 95 | uint64_t Elf::GetRelPc(uint64_t pc, const MapInfo* map_info) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 96 | return pc - map_info->start + load_bias_ + map_info->elf_offset; |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | 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] | 100 | std::lock_guard<std::mutex> guard(lock_); |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 101 | return valid_ && (interface_->GetFunctionName(addr, load_bias_, name, func_offset) || |
| 102 | (gnu_debugdata_interface_ && gnu_debugdata_interface_->GetFunctionName( |
| 103 | addr, load_bias_, name, func_offset))); |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 106 | bool Elf::GetGlobalVariable(const std::string& name, uint64_t* memory_address) { |
| 107 | if (!valid_) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | if (!interface_->GetGlobalVariable(name, memory_address) && |
| 112 | (gnu_debugdata_interface_ == nullptr || |
| 113 | !gnu_debugdata_interface_->GetGlobalVariable(name, memory_address))) { |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | // Adjust by the load bias. |
| 118 | if (*memory_address < load_bias_) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | *memory_address -= load_bias_; |
| 123 | |
| 124 | // If this winds up in the dynamic section, then we might need to adjust |
| 125 | // the address. |
| 126 | uint64_t dynamic_end = interface_->dynamic_vaddr() + interface_->dynamic_size(); |
| 127 | if (*memory_address >= interface_->dynamic_vaddr() && *memory_address < dynamic_end) { |
| 128 | if (interface_->dynamic_vaddr() > interface_->dynamic_offset()) { |
| 129 | *memory_address -= interface_->dynamic_vaddr() - interface_->dynamic_offset(); |
| 130 | } else { |
| 131 | *memory_address += interface_->dynamic_offset() - interface_->dynamic_vaddr(); |
| 132 | } |
| 133 | } |
| 134 | return true; |
| 135 | } |
| 136 | |
Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 137 | void Elf::GetLastError(ErrorData* data) { |
| 138 | if (valid_) { |
| 139 | *data = interface_->last_error(); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | ErrorCode Elf::GetLastErrorCode() { |
| 144 | if (valid_) { |
| 145 | return interface_->LastErrorCode(); |
| 146 | } |
| 147 | return ERROR_NONE; |
| 148 | } |
| 149 | |
| 150 | uint64_t Elf::GetLastErrorAddress() { |
| 151 | if (valid_) { |
| 152 | return interface_->LastErrorAddress(); |
| 153 | } |
| 154 | return 0; |
| 155 | } |
| 156 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 157 | // The relative pc is always relative to the start of the map from which it comes. |
Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 158 | bool Elf::Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, uint64_t elf_offset, Regs* regs, |
| 159 | Memory* process_memory, bool* finished) { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 160 | if (!valid_) { |
| 161 | return false; |
| 162 | } |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 163 | |
| 164 | // The relative pc expectd by StepIfSignalHandler is relative to the start of the elf. |
| 165 | if (regs->StepIfSignalHandler(rel_pc + elf_offset, this, process_memory)) { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 166 | *finished = false; |
| 167 | return true; |
| 168 | } |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 169 | |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 170 | // Lock during the step which can update information in the object. |
| 171 | std::lock_guard<std::mutex> guard(lock_); |
Christopher Ferris | e7b6624 | 2017-12-15 11:17:45 -0800 | [diff] [blame] | 172 | return interface_->Step(adjusted_rel_pc, load_bias_, regs, process_memory, finished); |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 175 | bool Elf::IsValidElf(Memory* memory) { |
| 176 | if (memory == nullptr) { |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | // Verify that this is a valid elf file. |
| 181 | uint8_t e_ident[SELFMAG + 1]; |
Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 182 | if (!memory->ReadFully(0, e_ident, SELFMAG)) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 183 | return false; |
| 184 | } |
| 185 | |
| 186 | if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) { |
| 187 | return false; |
| 188 | } |
| 189 | return true; |
| 190 | } |
| 191 | |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 192 | void Elf::GetInfo(Memory* memory, bool* valid, uint64_t* size) { |
| 193 | if (!IsValidElf(memory)) { |
| 194 | *valid = false; |
| 195 | return; |
| 196 | } |
| 197 | *size = 0; |
| 198 | *valid = true; |
| 199 | |
| 200 | // Now read the section header information. |
| 201 | uint8_t class_type; |
Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 202 | if (!memory->ReadFully(EI_CLASS, &class_type, 1)) { |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | if (class_type == ELFCLASS32) { |
| 206 | ElfInterface32::GetMaxSize(memory, size); |
| 207 | } else if (class_type == ELFCLASS64) { |
| 208 | ElfInterface64::GetMaxSize(memory, size); |
| 209 | } else { |
| 210 | *valid = false; |
| 211 | } |
| 212 | } |
| 213 | |
Christopher Ferris | 150db12 | 2017-12-20 18:49:01 -0800 | [diff] [blame] | 214 | bool Elf::IsValidPc(uint64_t pc) { |
| 215 | if (!valid_ || pc < load_bias_) { |
| 216 | return false; |
| 217 | } |
| 218 | pc -= load_bias_; |
| 219 | |
| 220 | if (interface_->IsValidPc(pc)) { |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | if (gnu_debugdata_interface_ != nullptr && gnu_debugdata_interface_->IsValidPc(pc)) { |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | return false; |
| 229 | } |
| 230 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 231 | ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) { |
| 232 | if (!IsValidElf(memory)) { |
| 233 | return nullptr; |
| 234 | } |
| 235 | |
| 236 | std::unique_ptr<ElfInterface> interface; |
Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 237 | if (!memory->ReadFully(EI_CLASS, &class_type_, 1)) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 238 | return nullptr; |
| 239 | } |
| 240 | if (class_type_ == ELFCLASS32) { |
| 241 | Elf32_Half e_machine; |
Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 242 | 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] | 243 | return nullptr; |
| 244 | } |
| 245 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 246 | machine_type_ = e_machine; |
| 247 | if (e_machine == EM_ARM) { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 248 | arch_ = ARCH_ARM; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 249 | interface.reset(new ElfInterfaceArm(memory)); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 250 | } else if (e_machine == EM_386) { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 251 | arch_ = ARCH_X86; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 252 | interface.reset(new ElfInterface32(memory)); |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 253 | } else if (e_machine == EM_MIPS) { |
| 254 | arch_ = ARCH_MIPS; |
| 255 | interface.reset(new ElfInterface32(memory)); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 256 | } else { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 257 | // Unsupported. |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 258 | 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] | 259 | return nullptr; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 260 | } |
| 261 | } else if (class_type_ == ELFCLASS64) { |
| 262 | Elf64_Half e_machine; |
Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 263 | 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] | 264 | return nullptr; |
| 265 | } |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 266 | |
| 267 | machine_type_ = e_machine; |
| 268 | if (e_machine == EM_AARCH64) { |
| 269 | arch_ = ARCH_ARM64; |
| 270 | } else if (e_machine == EM_X86_64) { |
| 271 | arch_ = ARCH_X86_64; |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 272 | } else if (e_machine == EM_MIPS) { |
| 273 | arch_ = ARCH_MIPS64; |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 274 | } else { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 275 | // Unsupported. |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame] | 276 | ALOGI("64 bit elf that is neither aarch64 nor x86_64 nor mips64: e_machine = %d\n", |
| 277 | e_machine); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 278 | return nullptr; |
| 279 | } |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 280 | interface.reset(new ElfInterface64(memory)); |
| 281 | } |
| 282 | |
| 283 | return interface.release(); |
| 284 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 285 | |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 286 | uint64_t Elf::GetLoadBias(Memory* memory) { |
| 287 | if (!IsValidElf(memory)) { |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | uint8_t class_type; |
| 292 | if (!memory->Read(EI_CLASS, &class_type, 1)) { |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | if (class_type == ELFCLASS32) { |
| 297 | return ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(memory); |
| 298 | } else if (class_type == ELFCLASS64) { |
| 299 | return ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(memory); |
| 300 | } |
| 301 | return 0; |
| 302 | } |
| 303 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 304 | } // namespace unwindstack |