| 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" | 
|  | 34 | #include "Machine.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 | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 39 | bool Elf::Init(bool init_gnu_debugdata) { | 
|  | 40 | load_bias_ = 0; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 41 | if (!memory_) { | 
|  | 42 | return false; | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | interface_.reset(CreateInterfaceFromMemory(memory_.get())); | 
|  | 46 | if (!interface_) { | 
|  | 47 | return false; | 
|  | 48 | } | 
|  | 49 |  | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 50 | valid_ = interface_->Init(&load_bias_); | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 51 | if (valid_) { | 
|  | 52 | interface_->InitHeaders(); | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 53 | if (init_gnu_debugdata) { | 
|  | 54 | InitGnuDebugdata(); | 
|  | 55 | } else { | 
|  | 56 | gnu_debugdata_interface_.reset(nullptr); | 
|  | 57 | } | 
| 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 | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 82 | gnu->InitHeaders(); | 
|  | 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 | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 106 | // 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] | 107 | bool Elf::Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, uint64_t elf_offset, Regs* regs, | 
|  | 108 | Memory* process_memory, bool* finished) { | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 109 | if (!valid_) { | 
|  | 110 | return false; | 
|  | 111 | } | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 112 |  | 
|  | 113 | // The relative pc expectd by StepIfSignalHandler is relative to the start of the elf. | 
|  | 114 | if (regs->StepIfSignalHandler(rel_pc + elf_offset, this, process_memory)) { | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 115 | *finished = false; | 
|  | 116 | return true; | 
|  | 117 | } | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 118 |  | 
|  | 119 | // Adjust the load bias to get the real relative pc. | 
| Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 120 | if (adjusted_rel_pc < load_bias_) { | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 121 | return false; | 
|  | 122 | } | 
| Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 123 | adjusted_rel_pc -= load_bias_; | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 124 |  | 
| Christopher Ferris | be788d8 | 2017-11-27 14:50:38 -0800 | [diff] [blame] | 125 | // Lock during the step which can update information in the object. | 
|  | 126 | std::lock_guard<std::mutex> guard(lock_); | 
| Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 127 | return interface_->Step(adjusted_rel_pc, regs, process_memory, finished) || | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 128 | (gnu_debugdata_interface_ && | 
| Christopher Ferris | c3d79f7 | 2017-11-28 19:14:54 -0800 | [diff] [blame] | 129 | gnu_debugdata_interface_->Step(adjusted_rel_pc, regs, process_memory, finished)); | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 130 | } | 
|  | 131 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 132 | bool Elf::IsValidElf(Memory* memory) { | 
|  | 133 | if (memory == nullptr) { | 
|  | 134 | return false; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | // Verify that this is a valid elf file. | 
|  | 138 | uint8_t e_ident[SELFMAG + 1]; | 
|  | 139 | if (!memory->Read(0, e_ident, SELFMAG)) { | 
|  | 140 | return false; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) { | 
|  | 144 | return false; | 
|  | 145 | } | 
|  | 146 | return true; | 
|  | 147 | } | 
|  | 148 |  | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 149 | void Elf::GetInfo(Memory* memory, bool* valid, uint64_t* size) { | 
|  | 150 | if (!IsValidElf(memory)) { | 
|  | 151 | *valid = false; | 
|  | 152 | return; | 
|  | 153 | } | 
|  | 154 | *size = 0; | 
|  | 155 | *valid = true; | 
|  | 156 |  | 
|  | 157 | // Now read the section header information. | 
|  | 158 | uint8_t class_type; | 
|  | 159 | if (!memory->Read(EI_CLASS, &class_type, 1)) { | 
|  | 160 | return; | 
|  | 161 | } | 
|  | 162 | if (class_type == ELFCLASS32) { | 
|  | 163 | ElfInterface32::GetMaxSize(memory, size); | 
|  | 164 | } else if (class_type == ELFCLASS64) { | 
|  | 165 | ElfInterface64::GetMaxSize(memory, size); | 
|  | 166 | } else { | 
|  | 167 | *valid = false; | 
|  | 168 | } | 
|  | 169 | } | 
|  | 170 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 171 | ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) { | 
|  | 172 | if (!IsValidElf(memory)) { | 
|  | 173 | return nullptr; | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | std::unique_ptr<ElfInterface> interface; | 
|  | 177 | if (!memory->Read(EI_CLASS, &class_type_, 1)) { | 
|  | 178 | return nullptr; | 
|  | 179 | } | 
|  | 180 | if (class_type_ == ELFCLASS32) { | 
|  | 181 | Elf32_Half e_machine; | 
|  | 182 | if (!memory->Read(EI_NIDENT + sizeof(Elf32_Half), &e_machine, sizeof(e_machine))) { | 
|  | 183 | return nullptr; | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | if (e_machine != EM_ARM && e_machine != EM_386) { | 
|  | 187 | // Unsupported. | 
|  | 188 | ALOGI("32 bit elf that is neither arm nor x86: e_machine = %d\n", e_machine); | 
|  | 189 | return nullptr; | 
|  | 190 | } | 
|  | 191 |  | 
|  | 192 | machine_type_ = e_machine; | 
|  | 193 | if (e_machine == EM_ARM) { | 
|  | 194 | interface.reset(new ElfInterfaceArm(memory)); | 
| Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 195 | } else if (e_machine == EM_386) { | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 196 | interface.reset(new ElfInterface32(memory)); | 
| Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 197 | } else { | 
|  | 198 | ALOGI("32 bit elf that is neither arm nor x86: e_machine = %d\n", e_machine); | 
|  | 199 | return nullptr; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 200 | } | 
|  | 201 | } else if (class_type_ == ELFCLASS64) { | 
|  | 202 | Elf64_Half e_machine; | 
|  | 203 | if (!memory->Read(EI_NIDENT + sizeof(Elf64_Half), &e_machine, sizeof(e_machine))) { | 
|  | 204 | return nullptr; | 
|  | 205 | } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 206 | if (e_machine != EM_AARCH64 && e_machine != EM_X86_64) { | 
|  | 207 | // Unsupported. | 
|  | 208 | ALOGI("64 bit elf that is neither aarch64 nor x86_64: e_machine = %d\n", e_machine); | 
|  | 209 | return nullptr; | 
|  | 210 | } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 211 | machine_type_ = e_machine; | 
|  | 212 | interface.reset(new ElfInterface64(memory)); | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | return interface.release(); | 
|  | 216 | } | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 217 |  | 
|  | 218 | }  // namespace unwindstack |