blob: dbf772e5d591b8fdfedabbd52c79afb233e322ed [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 Ferris0b79ae12018-01-25 12:15:56 -080038bool Elf::cache_enabled_;
39std::unordered_map<std::string, std::shared_ptr<Elf>>* Elf::cache_;
40std::mutex* Elf::cache_lock_;
41
Christopher Ferrise69f4702017-10-19 16:08:58 -070042bool Elf::Init(bool init_gnu_debugdata) {
43 load_bias_ = 0;
Christopher Ferris3958f802017-02-01 15:44:40 -080044 if (!memory_) {
45 return false;
46 }
47
48 interface_.reset(CreateInterfaceFromMemory(memory_.get()));
49 if (!interface_) {
50 return false;
51 }
52
Christopher Ferrise69f4702017-10-19 16:08:58 -070053 valid_ = interface_->Init(&load_bias_);
Christopher Ferris3958f802017-02-01 15:44:40 -080054 if (valid_) {
55 interface_->InitHeaders();
Christopher Ferrise69f4702017-10-19 16:08:58 -070056 if (init_gnu_debugdata) {
57 InitGnuDebugdata();
58 } else {
59 gnu_debugdata_interface_.reset(nullptr);
60 }
Christopher Ferris3958f802017-02-01 15:44:40 -080061 } else {
62 interface_.reset(nullptr);
63 }
64 return valid_;
65}
66
Christopher Ferrisbae69f12017-06-28 14:51:54 -070067// It is expensive to initialize the .gnu_debugdata section. Provide a method
68// to initialize this data separately.
69void Elf::InitGnuDebugdata() {
70 if (!valid_ || interface_->gnu_debugdata_offset() == 0) {
71 return;
72 }
73
74 gnu_debugdata_memory_.reset(interface_->CreateGnuDebugdataMemory());
75 gnu_debugdata_interface_.reset(CreateInterfaceFromMemory(gnu_debugdata_memory_.get()));
76 ElfInterface* gnu = gnu_debugdata_interface_.get();
77 if (gnu == nullptr) {
78 return;
79 }
Christopher Ferrise69f4702017-10-19 16:08:58 -070080
81 // Ignore the load_bias from the compressed section, the correct load bias
82 // is in the uncompressed data.
83 uint64_t load_bias;
84 if (gnu->Init(&load_bias)) {
Christopher Ferrisbae69f12017-06-28 14:51:54 -070085 gnu->InitHeaders();
Christopher Ferrise7b66242017-12-15 11:17:45 -080086 interface_->SetGnuDebugdataInterface(gnu);
Christopher Ferrisbae69f12017-06-28 14:51:54 -070087 } else {
88 // Free all of the memory associated with the gnu_debugdata section.
89 gnu_debugdata_memory_.reset(nullptr);
90 gnu_debugdata_interface_.reset(nullptr);
91 }
92}
93
Christopher Ferrisd226a512017-07-14 10:37:19 -070094bool Elf::GetSoname(std::string* name) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -080095 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrisd226a512017-07-14 10:37:19 -070096 return valid_ && interface_->GetSoname(name);
97}
98
99uint64_t Elf::GetRelPc(uint64_t pc, const MapInfo* map_info) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700100 return pc - map_info->start + load_bias_ + map_info->elf_offset;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700101}
102
103bool Elf::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800104 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700105 return valid_ && (interface_->GetFunctionName(addr, load_bias_, name, func_offset) ||
106 (gnu_debugdata_interface_ && gnu_debugdata_interface_->GetFunctionName(
107 addr, load_bias_, name, func_offset)));
Christopher Ferrisd226a512017-07-14 10:37:19 -0700108}
109
Christopher Ferris150db122017-12-20 18:49:01 -0800110bool Elf::GetGlobalVariable(const std::string& name, uint64_t* memory_address) {
111 if (!valid_) {
112 return false;
113 }
114
115 if (!interface_->GetGlobalVariable(name, memory_address) &&
116 (gnu_debugdata_interface_ == nullptr ||
117 !gnu_debugdata_interface_->GetGlobalVariable(name, memory_address))) {
118 return false;
119 }
120
121 // Adjust by the load bias.
122 if (*memory_address < load_bias_) {
123 return false;
124 }
125
126 *memory_address -= load_bias_;
127
128 // If this winds up in the dynamic section, then we might need to adjust
129 // the address.
130 uint64_t dynamic_end = interface_->dynamic_vaddr() + interface_->dynamic_size();
131 if (*memory_address >= interface_->dynamic_vaddr() && *memory_address < dynamic_end) {
132 if (interface_->dynamic_vaddr() > interface_->dynamic_offset()) {
133 *memory_address -= interface_->dynamic_vaddr() - interface_->dynamic_offset();
134 } else {
135 *memory_address += interface_->dynamic_offset() - interface_->dynamic_vaddr();
136 }
137 }
138 return true;
139}
140
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800141void Elf::GetLastError(ErrorData* data) {
142 if (valid_) {
143 *data = interface_->last_error();
144 }
145}
146
147ErrorCode Elf::GetLastErrorCode() {
148 if (valid_) {
149 return interface_->LastErrorCode();
150 }
151 return ERROR_NONE;
152}
153
154uint64_t Elf::GetLastErrorAddress() {
155 if (valid_) {
156 return interface_->LastErrorAddress();
157 }
158 return 0;
159}
160
Christopher Ferrise69f4702017-10-19 16:08:58 -0700161// The relative pc is always relative to the start of the map from which it comes.
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800162bool Elf::Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, uint64_t elf_offset, Regs* regs,
163 Memory* process_memory, bool* finished) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700164 if (!valid_) {
165 return false;
166 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700167
168 // The relative pc expectd by StepIfSignalHandler is relative to the start of the elf.
169 if (regs->StepIfSignalHandler(rel_pc + elf_offset, this, process_memory)) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700170 *finished = false;
171 return true;
172 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700173
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800174 // Lock during the step which can update information in the object.
175 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrise7b66242017-12-15 11:17:45 -0800176 return interface_->Step(adjusted_rel_pc, load_bias_, regs, process_memory, finished);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700177}
178
Christopher Ferris3958f802017-02-01 15:44:40 -0800179bool Elf::IsValidElf(Memory* memory) {
180 if (memory == nullptr) {
181 return false;
182 }
183
184 // Verify that this is a valid elf file.
185 uint8_t e_ident[SELFMAG + 1];
Josh Gaoef35aa52017-10-18 11:44:51 -0700186 if (!memory->ReadFully(0, e_ident, SELFMAG)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800187 return false;
188 }
189
190 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
191 return false;
192 }
193 return true;
194}
195
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700196void Elf::GetInfo(Memory* memory, bool* valid, uint64_t* size) {
197 if (!IsValidElf(memory)) {
198 *valid = false;
199 return;
200 }
201 *size = 0;
202 *valid = true;
203
204 // Now read the section header information.
205 uint8_t class_type;
Josh Gaoef35aa52017-10-18 11:44:51 -0700206 if (!memory->ReadFully(EI_CLASS, &class_type, 1)) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700207 return;
208 }
209 if (class_type == ELFCLASS32) {
210 ElfInterface32::GetMaxSize(memory, size);
211 } else if (class_type == ELFCLASS64) {
212 ElfInterface64::GetMaxSize(memory, size);
213 } else {
214 *valid = false;
215 }
216}
217
Christopher Ferris150db122017-12-20 18:49:01 -0800218bool Elf::IsValidPc(uint64_t pc) {
219 if (!valid_ || pc < load_bias_) {
220 return false;
221 }
222 pc -= load_bias_;
223
224 if (interface_->IsValidPc(pc)) {
225 return true;
226 }
227
228 if (gnu_debugdata_interface_ != nullptr && gnu_debugdata_interface_->IsValidPc(pc)) {
229 return true;
230 }
231
232 return false;
233}
234
Christopher Ferris3958f802017-02-01 15:44:40 -0800235ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) {
236 if (!IsValidElf(memory)) {
237 return nullptr;
238 }
239
240 std::unique_ptr<ElfInterface> interface;
Josh Gaoef35aa52017-10-18 11:44:51 -0700241 if (!memory->ReadFully(EI_CLASS, &class_type_, 1)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800242 return nullptr;
243 }
244 if (class_type_ == ELFCLASS32) {
245 Elf32_Half e_machine;
Josh Gaoef35aa52017-10-18 11:44:51 -0700246 if (!memory->ReadFully(EI_NIDENT + sizeof(Elf32_Half), &e_machine, sizeof(e_machine))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800247 return nullptr;
248 }
249
Christopher Ferris3958f802017-02-01 15:44:40 -0800250 machine_type_ = e_machine;
251 if (e_machine == EM_ARM) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800252 arch_ = ARCH_ARM;
Christopher Ferris3958f802017-02-01 15:44:40 -0800253 interface.reset(new ElfInterfaceArm(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700254 } else if (e_machine == EM_386) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800255 arch_ = ARCH_X86;
Christopher Ferris3958f802017-02-01 15:44:40 -0800256 interface.reset(new ElfInterface32(memory));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100257 } else if (e_machine == EM_MIPS) {
258 arch_ = ARCH_MIPS;
259 interface.reset(new ElfInterface32(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700260 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800261 // Unsupported.
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100262 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 -0700263 return nullptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800264 }
265 } else if (class_type_ == ELFCLASS64) {
266 Elf64_Half e_machine;
Josh Gaoef35aa52017-10-18 11:44:51 -0700267 if (!memory->ReadFully(EI_NIDENT + sizeof(Elf64_Half), &e_machine, sizeof(e_machine))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800268 return nullptr;
269 }
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800270
271 machine_type_ = e_machine;
272 if (e_machine == EM_AARCH64) {
273 arch_ = ARCH_ARM64;
274 } else if (e_machine == EM_X86_64) {
275 arch_ = ARCH_X86_64;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100276 } else if (e_machine == EM_MIPS) {
277 arch_ = ARCH_MIPS64;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800278 } else {
Christopher Ferris3958f802017-02-01 15:44:40 -0800279 // Unsupported.
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100280 ALOGI("64 bit elf that is neither aarch64 nor x86_64 nor mips64: e_machine = %d\n",
281 e_machine);
Christopher Ferris3958f802017-02-01 15:44:40 -0800282 return nullptr;
283 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800284 interface.reset(new ElfInterface64(memory));
285 }
286
287 return interface.release();
288}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700289
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800290uint64_t Elf::GetLoadBias(Memory* memory) {
291 if (!IsValidElf(memory)) {
292 return 0;
293 }
294
295 uint8_t class_type;
296 if (!memory->Read(EI_CLASS, &class_type, 1)) {
297 return 0;
298 }
299
300 if (class_type == ELFCLASS32) {
301 return ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(memory);
302 } else if (class_type == ELFCLASS64) {
303 return ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(memory);
304 }
305 return 0;
306}
307
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800308void Elf::SetCachingEnabled(bool enable) {
309 if (!cache_enabled_ && enable) {
310 cache_enabled_ = true;
311 cache_ = new std::unordered_map<std::string, std::shared_ptr<Elf>>;
312 cache_lock_ = new std::mutex;
313 } else if (cache_enabled_ && !enable) {
314 cache_enabled_ = false;
315 delete cache_;
316 delete cache_lock_;
317 }
318}
319
320void Elf::CacheLock() {
321 cache_lock_->lock();
322}
323
324void Elf::CacheUnlock() {
325 cache_lock_->unlock();
326}
327
328void Elf::CacheAdd(MapInfo* info) {
329 if (info->offset == 0) {
330 (*cache_)[info->name] = info->elf;
331 } else {
332 std::string name(info->name + ':' + std::to_string(info->offset));
333 (*cache_)[name] = info->elf;
334 }
335}
336
337bool Elf::CacheGet(const std::string& name, std::shared_ptr<Elf>* elf) {
338 auto entry = cache_->find(name);
339 if (entry != cache_->end()) {
340 *elf = entry->second;
341 return true;
342 }
343 return false;
344}
345
Christopher Ferrisd226a512017-07-14 10:37:19 -0700346} // namespace unwindstack