| 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 |  | 
|  | 38 | bool Elf::Init() { | 
|  | 39 | if (!memory_) { | 
|  | 40 | return false; | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | interface_.reset(CreateInterfaceFromMemory(memory_.get())); | 
|  | 44 | if (!interface_) { | 
|  | 45 | return false; | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | valid_ = interface_->Init(); | 
|  | 49 | if (valid_) { | 
|  | 50 | interface_->InitHeaders(); | 
|  | 51 | } else { | 
|  | 52 | interface_.reset(nullptr); | 
|  | 53 | } | 
|  | 54 | return valid_; | 
|  | 55 | } | 
|  | 56 |  | 
| Christopher Ferris | bae69f1 | 2017-06-28 14:51:54 -0700 | [diff] [blame] | 57 | // It is expensive to initialize the .gnu_debugdata section. Provide a method | 
|  | 58 | // to initialize this data separately. | 
|  | 59 | void Elf::InitGnuDebugdata() { | 
|  | 60 | if (!valid_ || interface_->gnu_debugdata_offset() == 0) { | 
|  | 61 | return; | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | gnu_debugdata_memory_.reset(interface_->CreateGnuDebugdataMemory()); | 
|  | 65 | gnu_debugdata_interface_.reset(CreateInterfaceFromMemory(gnu_debugdata_memory_.get())); | 
|  | 66 | ElfInterface* gnu = gnu_debugdata_interface_.get(); | 
|  | 67 | if (gnu == nullptr) { | 
|  | 68 | return; | 
|  | 69 | } | 
|  | 70 | if (gnu->Init()) { | 
|  | 71 | gnu->InitHeaders(); | 
|  | 72 | } else { | 
|  | 73 | // Free all of the memory associated with the gnu_debugdata section. | 
|  | 74 | gnu_debugdata_memory_.reset(nullptr); | 
|  | 75 | gnu_debugdata_interface_.reset(nullptr); | 
|  | 76 | } | 
|  | 77 | } | 
|  | 78 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 79 | bool Elf::GetSoname(std::string* name) { | 
|  | 80 | return valid_ && interface_->GetSoname(name); | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | uint64_t Elf::GetRelPc(uint64_t pc, const MapInfo* map_info) { | 
|  | 84 | uint64_t load_bias = 0; | 
|  | 85 | if (valid()) { | 
|  | 86 | load_bias = interface_->load_bias(); | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | return pc - map_info->start + load_bias + map_info->elf_offset; | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 | bool Elf::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) { | 
|  | 93 | return valid_ && (interface_->GetFunctionName(addr, name, func_offset) || | 
|  | 94 | (gnu_debugdata_interface_ && | 
|  | 95 | gnu_debugdata_interface_->GetFunctionName(addr, name, func_offset))); | 
|  | 96 | } | 
|  | 97 |  | 
| Christopher Ferris | b9de87f | 2017-09-20 13:37:24 -0700 | [diff] [blame] | 98 | bool Elf::Step(uint64_t rel_pc, Regs* regs, Memory* process_memory, bool* finished) { | 
|  | 99 | if (!valid_) { | 
|  | 100 | return false; | 
|  | 101 | } | 
|  | 102 | if (regs->StepIfSignalHandler(rel_pc, this, process_memory)) { | 
|  | 103 | *finished = false; | 
|  | 104 | return true; | 
|  | 105 | } | 
|  | 106 | return interface_->Step(rel_pc, regs, process_memory, finished) || | 
|  | 107 | (gnu_debugdata_interface_ && | 
|  | 108 | gnu_debugdata_interface_->Step(rel_pc, regs, process_memory, finished)); | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 109 | } | 
|  | 110 |  | 
|  | 111 | uint64_t Elf::GetLoadBias() { | 
|  | 112 | if (!valid_) return 0; | 
|  | 113 | return interface_->load_bias(); | 
|  | 114 | } | 
|  | 115 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 116 | bool Elf::IsValidElf(Memory* memory) { | 
|  | 117 | if (memory == nullptr) { | 
|  | 118 | return false; | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | // Verify that this is a valid elf file. | 
|  | 122 | uint8_t e_ident[SELFMAG + 1]; | 
|  | 123 | if (!memory->Read(0, e_ident, SELFMAG)) { | 
|  | 124 | return false; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) { | 
|  | 128 | return false; | 
|  | 129 | } | 
|  | 130 | return true; | 
|  | 131 | } | 
|  | 132 |  | 
| Christopher Ferris | 3f805ac | 2017-08-30 13:15:19 -0700 | [diff] [blame] | 133 | void Elf::GetInfo(Memory* memory, bool* valid, uint64_t* size) { | 
|  | 134 | if (!IsValidElf(memory)) { | 
|  | 135 | *valid = false; | 
|  | 136 | return; | 
|  | 137 | } | 
|  | 138 | *size = 0; | 
|  | 139 | *valid = true; | 
|  | 140 |  | 
|  | 141 | // Now read the section header information. | 
|  | 142 | uint8_t class_type; | 
|  | 143 | if (!memory->Read(EI_CLASS, &class_type, 1)) { | 
|  | 144 | return; | 
|  | 145 | } | 
|  | 146 | if (class_type == ELFCLASS32) { | 
|  | 147 | ElfInterface32::GetMaxSize(memory, size); | 
|  | 148 | } else if (class_type == ELFCLASS64) { | 
|  | 149 | ElfInterface64::GetMaxSize(memory, size); | 
|  | 150 | } else { | 
|  | 151 | *valid = false; | 
|  | 152 | } | 
|  | 153 | } | 
|  | 154 |  | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 155 | ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) { | 
|  | 156 | if (!IsValidElf(memory)) { | 
|  | 157 | return nullptr; | 
|  | 158 | } | 
|  | 159 |  | 
|  | 160 | std::unique_ptr<ElfInterface> interface; | 
|  | 161 | if (!memory->Read(EI_CLASS, &class_type_, 1)) { | 
|  | 162 | return nullptr; | 
|  | 163 | } | 
|  | 164 | if (class_type_ == ELFCLASS32) { | 
|  | 165 | Elf32_Half e_machine; | 
|  | 166 | if (!memory->Read(EI_NIDENT + sizeof(Elf32_Half), &e_machine, sizeof(e_machine))) { | 
|  | 167 | return nullptr; | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | if (e_machine != EM_ARM && e_machine != EM_386) { | 
|  | 171 | // Unsupported. | 
|  | 172 | ALOGI("32 bit elf that is neither arm nor x86: e_machine = %d\n", e_machine); | 
|  | 173 | return nullptr; | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | machine_type_ = e_machine; | 
|  | 177 | if (e_machine == EM_ARM) { | 
|  | 178 | interface.reset(new ElfInterfaceArm(memory)); | 
| Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 179 | } else if (e_machine == EM_386) { | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 180 | interface.reset(new ElfInterface32(memory)); | 
| Christopher Ferris | a019665 | 2017-07-18 16:09:20 -0700 | [diff] [blame] | 181 | } else { | 
|  | 182 | ALOGI("32 bit elf that is neither arm nor x86: e_machine = %d\n", e_machine); | 
|  | 183 | return nullptr; | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 184 | } | 
|  | 185 | } else if (class_type_ == ELFCLASS64) { | 
|  | 186 | Elf64_Half e_machine; | 
|  | 187 | if (!memory->Read(EI_NIDENT + sizeof(Elf64_Half), &e_machine, sizeof(e_machine))) { | 
|  | 188 | return nullptr; | 
|  | 189 | } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 190 | if (e_machine != EM_AARCH64 && e_machine != EM_X86_64) { | 
|  | 191 | // Unsupported. | 
|  | 192 | ALOGI("64 bit elf that is neither aarch64 nor x86_64: e_machine = %d\n", e_machine); | 
|  | 193 | return nullptr; | 
|  | 194 | } | 
| Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 195 | machine_type_ = e_machine; | 
|  | 196 | interface.reset(new ElfInterface64(memory)); | 
|  | 197 | } | 
|  | 198 |  | 
|  | 199 | return interface.release(); | 
|  | 200 | } | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 201 |  | 
|  | 202 | }  // namespace unwindstack |