blob: 220e549aa094072b80bb4cc91c79ad17458ee836 [file] [log] [blame]
Christopher Ferris3958f802017-02-01 15:44:40 -08001/*
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 Ferrisbe788d82017-11-27 14:50:38 -080021#include <mutex>
Christopher Ferris3958f802017-02-01 15:44:40 -080022#include <string>
23
24#define LOG_TAG "unwind"
25#include <log/log.h>
26
Christopher Ferrisd226a512017-07-14 10:37:19 -070027#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 Ferris3958f802017-02-01 15:44:40 -080033#include "ElfInterfaceArm.h"
Christopher Ferrisd226a512017-07-14 10:37:19 -070034#include "Symbols.h"
35
36namespace unwindstack {
Christopher Ferris3958f802017-02-01 15:44:40 -080037
Christopher Ferrise69f4702017-10-19 16:08:58 -070038bool Elf::Init(bool init_gnu_debugdata) {
39 load_bias_ = 0;
Christopher Ferris3958f802017-02-01 15:44:40 -080040 if (!memory_) {
41 return false;
42 }
43
44 interface_.reset(CreateInterfaceFromMemory(memory_.get()));
45 if (!interface_) {
46 return false;
47 }
48
Christopher Ferrise69f4702017-10-19 16:08:58 -070049 valid_ = interface_->Init(&load_bias_);
Christopher Ferris3958f802017-02-01 15:44:40 -080050 if (valid_) {
51 interface_->InitHeaders();
Christopher Ferrise69f4702017-10-19 16:08:58 -070052 if (init_gnu_debugdata) {
53 InitGnuDebugdata();
54 } else {
55 gnu_debugdata_interface_.reset(nullptr);
56 }
Christopher Ferris3958f802017-02-01 15:44:40 -080057 } else {
58 interface_.reset(nullptr);
59 }
60 return valid_;
61}
62
Christopher Ferrisbae69f12017-06-28 14:51:54 -070063// It is expensive to initialize the .gnu_debugdata section. Provide a method
64// to initialize this data separately.
65void 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 Ferrise69f4702017-10-19 16:08:58 -070076
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 Ferrisbae69f12017-06-28 14:51:54 -070081 gnu->InitHeaders();
Christopher Ferrise7b66242017-12-15 11:17:45 -080082 interface_->SetGnuDebugdataInterface(gnu);
Christopher Ferrisbae69f12017-06-28 14:51:54 -070083 } 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 Ferrisd226a512017-07-14 10:37:19 -070090bool Elf::GetSoname(std::string* name) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -080091 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrisd226a512017-07-14 10:37:19 -070092 return valid_ && interface_->GetSoname(name);
93}
94
95uint64_t Elf::GetRelPc(uint64_t pc, const MapInfo* map_info) {
Christopher Ferrise69f4702017-10-19 16:08:58 -070096 return pc - map_info->start + load_bias_ + map_info->elf_offset;
Christopher Ferrisd226a512017-07-14 10:37:19 -070097}
98
99bool Elf::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800100 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700101 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 Ferrisd226a512017-07-14 10:37:19 -0700104}
105
Christopher Ferris150db122017-12-20 18:49:01 -0800106bool 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 Ferrise69f4702017-10-19 16:08:58 -0700137// The relative pc is always relative to the start of the map from which it comes.
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800138bool Elf::Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, uint64_t elf_offset, Regs* regs,
139 Memory* process_memory, bool* finished) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700140 if (!valid_) {
141 return false;
142 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700143
144 // The relative pc expectd by StepIfSignalHandler is relative to the start of the elf.
145 if (regs->StepIfSignalHandler(rel_pc + elf_offset, this, process_memory)) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700146 *finished = false;
147 return true;
148 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700149
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800150 // Lock during the step which can update information in the object.
151 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrise7b66242017-12-15 11:17:45 -0800152 return interface_->Step(adjusted_rel_pc, load_bias_, regs, process_memory, finished);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700153}
154
Christopher Ferris3958f802017-02-01 15:44:40 -0800155bool Elf::IsValidElf(Memory* memory) {
156 if (memory == nullptr) {
157 return false;
158 }
159
160 // Verify that this is a valid elf file.
161 uint8_t e_ident[SELFMAG + 1];
Josh Gaoef35aa52017-10-18 11:44:51 -0700162 if (!memory->ReadFully(0, e_ident, SELFMAG)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800163 return false;
164 }
165
166 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
167 return false;
168 }
169 return true;
170}
171
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700172void Elf::GetInfo(Memory* memory, bool* valid, uint64_t* size) {
173 if (!IsValidElf(memory)) {
174 *valid = false;
175 return;
176 }
177 *size = 0;
178 *valid = true;
179
180 // Now read the section header information.
181 uint8_t class_type;
Josh Gaoef35aa52017-10-18 11:44:51 -0700182 if (!memory->ReadFully(EI_CLASS, &class_type, 1)) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700183 return;
184 }
185 if (class_type == ELFCLASS32) {
186 ElfInterface32::GetMaxSize(memory, size);
187 } else if (class_type == ELFCLASS64) {
188 ElfInterface64::GetMaxSize(memory, size);
189 } else {
190 *valid = false;
191 }
192}
193
Christopher Ferris150db122017-12-20 18:49:01 -0800194bool Elf::IsValidPc(uint64_t pc) {
195 if (!valid_ || pc < load_bias_) {
196 return false;
197 }
198 pc -= load_bias_;
199
200 if (interface_->IsValidPc(pc)) {
201 return true;
202 }
203
204 if (gnu_debugdata_interface_ != nullptr && gnu_debugdata_interface_->IsValidPc(pc)) {
205 return true;
206 }
207
208 return false;
209}
210
Christopher Ferris3958f802017-02-01 15:44:40 -0800211ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) {
212 if (!IsValidElf(memory)) {
213 return nullptr;
214 }
215
216 std::unique_ptr<ElfInterface> interface;
Josh Gaoef35aa52017-10-18 11:44:51 -0700217 if (!memory->ReadFully(EI_CLASS, &class_type_, 1)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800218 return nullptr;
219 }
220 if (class_type_ == ELFCLASS32) {
221 Elf32_Half e_machine;
Josh Gaoef35aa52017-10-18 11:44:51 -0700222 if (!memory->ReadFully(EI_NIDENT + sizeof(Elf32_Half), &e_machine, sizeof(e_machine))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800223 return nullptr;
224 }
225
Christopher Ferris3958f802017-02-01 15:44:40 -0800226 machine_type_ = e_machine;
227 if (e_machine == EM_ARM) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800228 arch_ = ARCH_ARM;
Christopher Ferris3958f802017-02-01 15:44:40 -0800229 interface.reset(new ElfInterfaceArm(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700230 } else if (e_machine == EM_386) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800231 arch_ = ARCH_X86;
Christopher Ferris3958f802017-02-01 15:44:40 -0800232 interface.reset(new ElfInterface32(memory));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100233 } else if (e_machine == EM_MIPS) {
234 arch_ = ARCH_MIPS;
235 interface.reset(new ElfInterface32(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700236 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800237 // Unsupported.
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100238 ALOGI("32 bit elf that is neither arm nor x86 nor mips: e_machine = %d\n", e_machine);
Christopher Ferrisa0196652017-07-18 16:09:20 -0700239 return nullptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800240 }
241 } else if (class_type_ == ELFCLASS64) {
242 Elf64_Half e_machine;
Josh Gaoef35aa52017-10-18 11:44:51 -0700243 if (!memory->ReadFully(EI_NIDENT + sizeof(Elf64_Half), &e_machine, sizeof(e_machine))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800244 return nullptr;
245 }
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800246
247 machine_type_ = e_machine;
248 if (e_machine == EM_AARCH64) {
249 arch_ = ARCH_ARM64;
250 } else if (e_machine == EM_X86_64) {
251 arch_ = ARCH_X86_64;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100252 } else if (e_machine == EM_MIPS) {
253 arch_ = ARCH_MIPS64;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800254 } else {
Christopher Ferris3958f802017-02-01 15:44:40 -0800255 // Unsupported.
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100256 ALOGI("64 bit elf that is neither aarch64 nor x86_64 nor mips64: e_machine = %d\n",
257 e_machine);
Christopher Ferris3958f802017-02-01 15:44:40 -0800258 return nullptr;
259 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800260 interface.reset(new ElfInterface64(memory));
261 }
262
263 return interface.release();
264}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700265
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800266uint64_t Elf::GetLoadBias(Memory* memory) {
267 if (!IsValidElf(memory)) {
268 return 0;
269 }
270
271 uint8_t class_type;
272 if (!memory->Read(EI_CLASS, &class_type, 1)) {
273 return 0;
274 }
275
276 if (class_type == ELFCLASS32) {
277 return ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(memory);
278 } else if (class_type == ELFCLASS64) {
279 return ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(memory);
280 }
281 return 0;
282}
283
Christopher Ferrisd226a512017-07-14 10:37:19 -0700284} // namespace unwindstack