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(); |
| 82 | } else { |
| 83 | // Free all of the memory associated with the gnu_debugdata section. |
| 84 | gnu_debugdata_memory_.reset(nullptr); |
| 85 | gnu_debugdata_interface_.reset(nullptr); |
| 86 | } |
| 87 | } |
| 88 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 89 | bool Elf::GetSoname(std::string* name) { |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 90 | std::lock_guard<std::mutex> guard(lock_); |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 91 | return valid_ && interface_->GetSoname(name); |
| 92 | } |
| 93 | |
| 94 | uint64_t Elf::GetRelPc(uint64_t pc, const MapInfo* map_info) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 95 | return pc - map_info->start + load_bias_ + map_info->elf_offset; |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | 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] | 99 | std::lock_guard<std::mutex> guard(lock_); |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 100 | return valid_ && (interface_->GetFunctionName(addr, load_bias_, name, func_offset) || |
| 101 | (gnu_debugdata_interface_ && gnu_debugdata_interface_->GetFunctionName( |
| 102 | addr, load_bias_, name, func_offset))); |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 105 | // 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] | 106 | bool Elf::Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, uint64_t elf_offset, Regs* regs, |
| 107 | Memory* process_memory, bool* finished) { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 108 | if (!valid_) { |
| 109 | return false; |
| 110 | } |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 111 | |
| 112 | // The relative pc expectd by StepIfSignalHandler is relative to the start of the elf. |
| 113 | if (regs->StepIfSignalHandler(rel_pc + elf_offset, this, process_memory)) { |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 114 | *finished = false; |
| 115 | return true; |
| 116 | } |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 117 | |
| 118 | // Adjust the load bias to get the real relative pc. |
Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 119 | if (adjusted_rel_pc < load_bias_) { |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 120 | return false; |
| 121 | } |
Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 122 | adjusted_rel_pc -= load_bias_; |
Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 123 | |
Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 124 | // Lock during the step which can update information in the object. |
| 125 | std::lock_guard<std::mutex> guard(lock_); |
Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 126 | return interface_->Step(adjusted_rel_pc, regs, process_memory, finished) || |
Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 127 | (gnu_debugdata_interface_ && |
Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 128 | gnu_debugdata_interface_->Step(adjusted_rel_pc, regs, process_memory, finished)); |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 131 | bool Elf::IsValidElf(Memory* memory) { |
| 132 | if (memory == nullptr) { |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | // Verify that this is a valid elf file. |
| 137 | uint8_t e_ident[SELFMAG + 1]; |
Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 138 | if (!memory->ReadFully(0, e_ident, SELFMAG)) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 139 | return false; |
| 140 | } |
| 141 | |
| 142 | if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) { |
| 143 | return false; |
| 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 148 | void Elf::GetInfo(Memory* memory, bool* valid, uint64_t* size) { |
| 149 | if (!IsValidElf(memory)) { |
| 150 | *valid = false; |
| 151 | return; |
| 152 | } |
| 153 | *size = 0; |
| 154 | *valid = true; |
| 155 | |
| 156 | // Now read the section header information. |
| 157 | uint8_t class_type; |
Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 158 | if (!memory->ReadFully(EI_CLASS, &class_type, 1)) { |
Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 159 | return; |
| 160 | } |
| 161 | if (class_type == ELFCLASS32) { |
| 162 | ElfInterface32::GetMaxSize(memory, size); |
| 163 | } else if (class_type == ELFCLASS64) { |
| 164 | ElfInterface64::GetMaxSize(memory, size); |
| 165 | } else { |
| 166 | *valid = false; |
| 167 | } |
| 168 | } |
| 169 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 170 | ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) { |
| 171 | if (!IsValidElf(memory)) { |
| 172 | return nullptr; |
| 173 | } |
| 174 | |
| 175 | std::unique_ptr<ElfInterface> interface; |
Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 176 | if (!memory->ReadFully(EI_CLASS, &class_type_, 1)) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 177 | return nullptr; |
| 178 | } |
| 179 | if (class_type_ == ELFCLASS32) { |
| 180 | Elf32_Half e_machine; |
Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 181 | 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] | 182 | return nullptr; |
| 183 | } |
| 184 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 185 | machine_type_ = e_machine; |
| 186 | if (e_machine == EM_ARM) { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 187 | arch_ = ARCH_ARM; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 188 | interface.reset(new ElfInterfaceArm(memory)); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 189 | } else if (e_machine == EM_386) { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 190 | arch_ = ARCH_X86; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 191 | interface.reset(new ElfInterface32(memory)); |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame^] | 192 | } else if (e_machine == EM_MIPS) { |
| 193 | arch_ = ARCH_MIPS; |
| 194 | interface.reset(new ElfInterface32(memory)); |
Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 195 | } else { |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 196 | // Unsupported. |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame^] | 197 | 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] | 198 | return nullptr; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 199 | } |
| 200 | } else if (class_type_ == ELFCLASS64) { |
| 201 | Elf64_Half e_machine; |
Josh Gao | ef35aa5 | 2017-10-18 11:44:51 -0700 | [diff] [blame] | 202 | 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] | 203 | return nullptr; |
| 204 | } |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 205 | |
| 206 | machine_type_ = e_machine; |
| 207 | if (e_machine == EM_AARCH64) { |
| 208 | arch_ = ARCH_ARM64; |
| 209 | } else if (e_machine == EM_X86_64) { |
| 210 | arch_ = ARCH_X86_64; |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame^] | 211 | } else if (e_machine == EM_MIPS) { |
| 212 | arch_ = ARCH_MIPS64; |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 213 | } else { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 214 | // Unsupported. |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame^] | 215 | ALOGI("64 bit elf that is neither aarch64 nor x86_64 nor mips64: e_machine = %d\n", |
| 216 | e_machine); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 217 | return nullptr; |
| 218 | } |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 219 | interface.reset(new ElfInterface64(memory)); |
| 220 | } |
| 221 | |
| 222 | return interface.release(); |
| 223 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 224 | |
Christopher Ferris | b7de5f5 | 2017-12-01 21:37:37 -0800 | [diff] [blame] | 225 | uint64_t Elf::GetLoadBias(Memory* memory) { |
| 226 | if (!IsValidElf(memory)) { |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | uint8_t class_type; |
| 231 | if (!memory->Read(EI_CLASS, &class_type, 1)) { |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | if (class_type == ELFCLASS32) { |
| 236 | return ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(memory); |
| 237 | } else if (class_type == ELFCLASS64) { |
| 238 | return ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(memory); |
| 239 | } |
| 240 | return 0; |
| 241 | } |
| 242 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 243 | } // namespace unwindstack |