| 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> | 
|  | 21 | #include <string> | 
|  | 22 |  | 
|  | 23 | #define LOG_TAG "unwind" | 
|  | 24 | #include <log/log.h> | 
|  | 25 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 26 | #include <unwindstack/Elf.h> | 
|  | 27 | #include <unwindstack/ElfInterface.h> | 
|  | 28 | #include <unwindstack/MapInfo.h> | 
|  | 29 | #include <unwindstack/Memory.h> | 
|  | 30 | #include <unwindstack/Regs.h> | 
|  | 31 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 32 | #include "ElfInterfaceArm.h" | 
|  | 33 | #include "Machine.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) { | 
|  | 90 | return valid_ && interface_->GetSoname(name); | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | uint64_t Elf::GetRelPc(uint64_t pc, const MapInfo* map_info) { | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 94 | return pc - map_info->start + load_bias_ + map_info->elf_offset; | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
|  | 97 | bool Elf::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) { | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 98 | return valid_ && (interface_->GetFunctionName(addr, load_bias_, name, func_offset) || | 
|  | 99 | (gnu_debugdata_interface_ && gnu_debugdata_interface_->GetFunctionName( | 
|  | 100 | addr, load_bias_, name, func_offset))); | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 103 | // The relative pc is always relative to the start of the map from which it comes. | 
|  | 104 | bool Elf::Step(uint64_t rel_pc, uint64_t elf_offset, Regs* regs, Memory* process_memory, | 
|  | 105 | bool* finished) { | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 106 | if (!valid_) { | 
|  | 107 | return false; | 
|  | 108 | } | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 109 |  | 
|  | 110 | // The relative pc expectd by StepIfSignalHandler is relative to the start of the elf. | 
|  | 111 | if (regs->StepIfSignalHandler(rel_pc + elf_offset, this, process_memory)) { | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 112 | *finished = false; | 
|  | 113 | return true; | 
|  | 114 | } | 
| Christopher Ferris | e69f470 | 2017-10-19 16:08:58 -0700 | [diff] [blame] | 115 |  | 
|  | 116 | // Adjust the load bias to get the real relative pc. | 
|  | 117 | if (rel_pc < load_bias_) { | 
|  | 118 | return false; | 
|  | 119 | } | 
|  | 120 | rel_pc -= load_bias_; | 
|  | 121 |  | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 122 | return interface_->Step(rel_pc, regs, process_memory, finished) || | 
|  | 123 | (gnu_debugdata_interface_ && | 
|  | 124 | gnu_debugdata_interface_->Step(rel_pc, regs, process_memory, finished)); | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 125 | } | 
|  | 126 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 127 | bool Elf::IsValidElf(Memory* memory) { | 
|  | 128 | if (memory == nullptr) { | 
|  | 129 | return false; | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | // Verify that this is a valid elf file. | 
|  | 133 | uint8_t e_ident[SELFMAG + 1]; | 
|  | 134 | if (!memory->Read(0, e_ident, SELFMAG)) { | 
|  | 135 | return false; | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) { | 
|  | 139 | return false; | 
|  | 140 | } | 
|  | 141 | return true; | 
|  | 142 | } | 
|  | 143 |  | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 144 | void Elf::GetInfo(Memory* memory, bool* valid, uint64_t* size) { | 
|  | 145 | if (!IsValidElf(memory)) { | 
|  | 146 | *valid = false; | 
|  | 147 | return; | 
|  | 148 | } | 
|  | 149 | *size = 0; | 
|  | 150 | *valid = true; | 
|  | 151 |  | 
|  | 152 | // Now read the section header information. | 
|  | 153 | uint8_t class_type; | 
|  | 154 | if (!memory->Read(EI_CLASS, &class_type, 1)) { | 
|  | 155 | return; | 
|  | 156 | } | 
|  | 157 | if (class_type == ELFCLASS32) { | 
|  | 158 | ElfInterface32::GetMaxSize(memory, size); | 
|  | 159 | } else if (class_type == ELFCLASS64) { | 
|  | 160 | ElfInterface64::GetMaxSize(memory, size); | 
|  | 161 | } else { | 
|  | 162 | *valid = false; | 
|  | 163 | } | 
|  | 164 | } | 
|  | 165 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 166 | ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) { | 
|  | 167 | if (!IsValidElf(memory)) { | 
|  | 168 | return nullptr; | 
|  | 169 | } | 
|  | 170 |  | 
|  | 171 | std::unique_ptr<ElfInterface> interface; | 
|  | 172 | if (!memory->Read(EI_CLASS, &class_type_, 1)) { | 
|  | 173 | return nullptr; | 
|  | 174 | } | 
|  | 175 | if (class_type_ == ELFCLASS32) { | 
|  | 176 | Elf32_Half e_machine; | 
|  | 177 | if (!memory->Read(EI_NIDENT + sizeof(Elf32_Half), &e_machine, sizeof(e_machine))) { | 
|  | 178 | return nullptr; | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | if (e_machine != EM_ARM && e_machine != EM_386) { | 
|  | 182 | // Unsupported. | 
|  | 183 | ALOGI("32 bit elf that is neither arm nor x86: e_machine = %d\n", e_machine); | 
|  | 184 | return nullptr; | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | machine_type_ = e_machine; | 
|  | 188 | if (e_machine == EM_ARM) { | 
|  | 189 | interface.reset(new ElfInterfaceArm(memory)); | 
| Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 190 | } else if (e_machine == EM_386) { | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 191 | interface.reset(new ElfInterface32(memory)); | 
| Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 192 | } else { | 
|  | 193 | ALOGI("32 bit elf that is neither arm nor x86: e_machine = %d\n", e_machine); | 
|  | 194 | return nullptr; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 195 | } | 
|  | 196 | } else if (class_type_ == ELFCLASS64) { | 
|  | 197 | Elf64_Half e_machine; | 
|  | 198 | if (!memory->Read(EI_NIDENT + sizeof(Elf64_Half), &e_machine, sizeof(e_machine))) { | 
|  | 199 | return nullptr; | 
|  | 200 | } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 201 | if (e_machine != EM_AARCH64 && e_machine != EM_X86_64) { | 
|  | 202 | // Unsupported. | 
|  | 203 | ALOGI("64 bit elf that is neither aarch64 nor x86_64: e_machine = %d\n", e_machine); | 
|  | 204 | return nullptr; | 
|  | 205 | } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 206 | machine_type_ = e_machine; | 
|  | 207 | interface.reset(new ElfInterface64(memory)); | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | return interface.release(); | 
|  | 211 | } | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 212 |  | 
|  | 213 | }  // namespace unwindstack |