blob: 66277874e9ba0a238c9ebe5e0ab78ba139c6d3a3 [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>
Christopher Ferrisd9575b62018-02-16 13:48:19 -080023#include <utility>
Christopher Ferris3958f802017-02-01 15:44:40 -080024
25#define LOG_TAG "unwind"
26#include <log/log.h>
27
Christopher Ferrisd226a512017-07-14 10:37:19 -070028#include <unwindstack/Elf.h>
29#include <unwindstack/ElfInterface.h>
30#include <unwindstack/MapInfo.h>
31#include <unwindstack/Memory.h>
32#include <unwindstack/Regs.h>
33
Christopher Ferris3958f802017-02-01 15:44:40 -080034#include "ElfInterfaceArm.h"
Christopher Ferrisd226a512017-07-14 10:37:19 -070035#include "Symbols.h"
36
37namespace unwindstack {
Christopher Ferris3958f802017-02-01 15:44:40 -080038
Christopher Ferris0b79ae12018-01-25 12:15:56 -080039bool Elf::cache_enabled_;
Christopher Ferrisd9575b62018-02-16 13:48:19 -080040std::unordered_map<std::string, std::pair<std::shared_ptr<Elf>, bool>>* Elf::cache_;
Christopher Ferris0b79ae12018-01-25 12:15:56 -080041std::mutex* Elf::cache_lock_;
42
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -070043bool Elf::Init() {
Christopher Ferrise69f4702017-10-19 16:08:58 -070044 load_bias_ = 0;
Christopher Ferris3958f802017-02-01 15:44:40 -080045 if (!memory_) {
46 return false;
47 }
48
49 interface_.reset(CreateInterfaceFromMemory(memory_.get()));
50 if (!interface_) {
51 return false;
52 }
53
Christopher Ferrise69f4702017-10-19 16:08:58 -070054 valid_ = interface_->Init(&load_bias_);
Christopher Ferris3958f802017-02-01 15:44:40 -080055 if (valid_) {
Christopher Ferris819f1312019-10-03 13:35:48 -070056 interface_->InitHeaders();
Christopher Ferrise8c4ecf2018-10-23 12:04:26 -070057 InitGnuDebugdata();
Christopher Ferris3958f802017-02-01 15:44:40 -080058 } else {
59 interface_.reset(nullptr);
60 }
61 return valid_;
62}
63
Christopher Ferrisbae69f12017-06-28 14:51:54 -070064// It is expensive to initialize the .gnu_debugdata section. Provide a method
65// to initialize this data separately.
66void 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 Ferrise69f4702017-10-19 16:08:58 -070077
78 // Ignore the load_bias from the compressed section, the correct load bias
79 // is in the uncompressed data.
Christopher Ferris819f1312019-10-03 13:35:48 -070080 int64_t load_bias;
Christopher Ferrise69f4702017-10-19 16:08:58 -070081 if (gnu->Init(&load_bias)) {
Christopher Ferris819f1312019-10-03 13:35:48 -070082 gnu->InitHeaders();
Christopher Ferrise7b66242017-12-15 11:17:45 -080083 interface_->SetGnuDebugdataInterface(gnu);
Christopher Ferrisbae69f12017-06-28 14:51:54 -070084 } else {
85 // Free all of the memory associated with the gnu_debugdata section.
86 gnu_debugdata_memory_.reset(nullptr);
87 gnu_debugdata_interface_.reset(nullptr);
88 }
89}
90
Christopher Ferris4568f4b2018-10-23 17:42:41 -070091void Elf::Invalidate() {
92 interface_.reset(nullptr);
93 valid_ = false;
94}
95
Christopher Ferris02a6c442019-03-11 14:43:33 -070096std::string Elf::GetSoname() {
Christopher Ferrisbe788d82017-11-27 14:50:38 -080097 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferris02a6c442019-03-11 14:43:33 -070098 if (!valid_) {
99 return "";
100 }
101 return interface_->GetSoname();
Christopher Ferrisd226a512017-07-14 10:37:19 -0700102}
103
104uint64_t Elf::GetRelPc(uint64_t pc, const MapInfo* map_info) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700105 return pc - map_info->start + load_bias_ + map_info->elf_offset;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700106}
107
108bool Elf::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800109 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700110 return valid_ && (interface_->GetFunctionName(addr, name, func_offset) ||
111 (gnu_debugdata_interface_ &&
112 gnu_debugdata_interface_->GetFunctionName(addr, name, func_offset)));
Christopher Ferrisd226a512017-07-14 10:37:19 -0700113}
114
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800115bool Elf::GetGlobalVariableOffset(const std::string& name, uint64_t* memory_offset) {
Christopher Ferris150db122017-12-20 18:49:01 -0800116 if (!valid_) {
117 return false;
118 }
119
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800120 uint64_t vaddr;
121 if (!interface_->GetGlobalVariable(name, &vaddr) &&
Christopher Ferris150db122017-12-20 18:49:01 -0800122 (gnu_debugdata_interface_ == nullptr ||
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800123 !gnu_debugdata_interface_->GetGlobalVariable(name, &vaddr))) {
Christopher Ferris150db122017-12-20 18:49:01 -0800124 return false;
125 }
126
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800127 // Check the .data section.
128 uint64_t vaddr_start = interface_->data_vaddr_start();
129 if (vaddr >= vaddr_start && vaddr < interface_->data_vaddr_end()) {
130 *memory_offset = vaddr - vaddr_start + interface_->data_offset();
131 return true;
Christopher Ferris150db122017-12-20 18:49:01 -0800132 }
133
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800134 // Check the .dynamic section.
135 vaddr_start = interface_->dynamic_vaddr_start();
136 if (vaddr >= vaddr_start && vaddr < interface_->dynamic_vaddr_end()) {
137 *memory_offset = vaddr - vaddr_start + interface_->dynamic_offset();
138 return true;
Christopher Ferris150db122017-12-20 18:49:01 -0800139 }
Christopher Ferrisdf683b72019-12-03 17:13:49 -0800140
141 return false;
Christopher Ferris150db122017-12-20 18:49:01 -0800142}
143
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800144std::string Elf::GetBuildID() {
145 if (!valid_) {
146 return "";
147 }
148 return interface_->GetBuildID();
Florian Mayerda459e52018-11-23 16:56:17 +0000149}
150
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800151void Elf::GetLastError(ErrorData* data) {
152 if (valid_) {
153 *data = interface_->last_error();
154 }
155}
156
157ErrorCode Elf::GetLastErrorCode() {
158 if (valid_) {
159 return interface_->LastErrorCode();
160 }
Christopher Ferrisd11ed862019-04-11 19:45:35 -0700161 return ERROR_INVALID_ELF;
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800162}
163
164uint64_t Elf::GetLastErrorAddress() {
165 if (valid_) {
166 return interface_->LastErrorAddress();
167 }
168 return 0;
169}
170
Christopher Ferrisd11ed862019-04-11 19:45:35 -0700171// The relative pc expectd by this function is relative to the start of the elf.
172bool Elf::StepIfSignalHandler(uint64_t rel_pc, Regs* regs, Memory* process_memory) {
173 if (!valid_) {
174 return false;
175 }
176 return regs->StepIfSignalHandler(rel_pc, this, process_memory);
177}
178
Christopher Ferrise69f4702017-10-19 16:08:58 -0700179// The relative pc is always relative to the start of the map from which it comes.
Christopher Ferrisd11ed862019-04-11 19:45:35 -0700180bool Elf::Step(uint64_t rel_pc, Regs* regs, Memory* process_memory, bool* finished) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700181 if (!valid_) {
182 return false;
183 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700184
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800185 // Lock during the step which can update information in the object.
186 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrisd11ed862019-04-11 19:45:35 -0700187 return interface_->Step(rel_pc, regs, process_memory, finished);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700188}
189
Christopher Ferris3958f802017-02-01 15:44:40 -0800190bool Elf::IsValidElf(Memory* memory) {
191 if (memory == nullptr) {
192 return false;
193 }
194
195 // Verify that this is a valid elf file.
196 uint8_t e_ident[SELFMAG + 1];
Josh Gaoef35aa52017-10-18 11:44:51 -0700197 if (!memory->ReadFully(0, e_ident, SELFMAG)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800198 return false;
199 }
200
201 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
202 return false;
203 }
204 return true;
205}
206
Christopher Ferrisa2f38f12018-10-09 18:49:11 -0700207bool Elf::GetInfo(Memory* memory, uint64_t* size) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700208 if (!IsValidElf(memory)) {
Christopher Ferrisa2f38f12018-10-09 18:49:11 -0700209 return false;
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700210 }
211 *size = 0;
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700212
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700213 uint8_t class_type;
Josh Gaoef35aa52017-10-18 11:44:51 -0700214 if (!memory->ReadFully(EI_CLASS, &class_type, 1)) {
Christopher Ferrisa2f38f12018-10-09 18:49:11 -0700215 return false;
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700216 }
Christopher Ferrisa2f38f12018-10-09 18:49:11 -0700217
218 // Get the maximum size of the elf data from the header.
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700219 if (class_type == ELFCLASS32) {
220 ElfInterface32::GetMaxSize(memory, size);
221 } else if (class_type == ELFCLASS64) {
222 ElfInterface64::GetMaxSize(memory, size);
223 } else {
Christopher Ferrisa2f38f12018-10-09 18:49:11 -0700224 return false;
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700225 }
Christopher Ferrisa2f38f12018-10-09 18:49:11 -0700226 return true;
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700227}
228
Christopher Ferris150db122017-12-20 18:49:01 -0800229bool Elf::IsValidPc(uint64_t pc) {
Christopher Ferris819f1312019-10-03 13:35:48 -0700230 if (!valid_ || (load_bias_ > 0 && pc < static_cast<uint64_t>(load_bias_))) {
Christopher Ferris150db122017-12-20 18:49:01 -0800231 return false;
232 }
Christopher Ferris150db122017-12-20 18:49:01 -0800233
234 if (interface_->IsValidPc(pc)) {
235 return true;
236 }
237
238 if (gnu_debugdata_interface_ != nullptr && gnu_debugdata_interface_->IsValidPc(pc)) {
239 return true;
240 }
241
242 return false;
243}
244
Christopher Ferris3958f802017-02-01 15:44:40 -0800245ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) {
246 if (!IsValidElf(memory)) {
247 return nullptr;
248 }
249
250 std::unique_ptr<ElfInterface> interface;
Josh Gaoef35aa52017-10-18 11:44:51 -0700251 if (!memory->ReadFully(EI_CLASS, &class_type_, 1)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800252 return nullptr;
253 }
254 if (class_type_ == ELFCLASS32) {
255 Elf32_Half e_machine;
Josh Gaoef35aa52017-10-18 11:44:51 -0700256 if (!memory->ReadFully(EI_NIDENT + sizeof(Elf32_Half), &e_machine, sizeof(e_machine))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800257 return nullptr;
258 }
259
Christopher Ferris3958f802017-02-01 15:44:40 -0800260 machine_type_ = e_machine;
261 if (e_machine == EM_ARM) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800262 arch_ = ARCH_ARM;
Christopher Ferris3958f802017-02-01 15:44:40 -0800263 interface.reset(new ElfInterfaceArm(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700264 } else if (e_machine == EM_386) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800265 arch_ = ARCH_X86;
Christopher Ferris3958f802017-02-01 15:44:40 -0800266 interface.reset(new ElfInterface32(memory));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100267 } else if (e_machine == EM_MIPS) {
268 arch_ = ARCH_MIPS;
269 interface.reset(new ElfInterface32(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700270 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800271 // Unsupported.
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100272 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 -0700273 return nullptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800274 }
275 } else if (class_type_ == ELFCLASS64) {
276 Elf64_Half e_machine;
Josh Gaoef35aa52017-10-18 11:44:51 -0700277 if (!memory->ReadFully(EI_NIDENT + sizeof(Elf64_Half), &e_machine, sizeof(e_machine))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800278 return nullptr;
279 }
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800280
281 machine_type_ = e_machine;
282 if (e_machine == EM_AARCH64) {
283 arch_ = ARCH_ARM64;
284 } else if (e_machine == EM_X86_64) {
285 arch_ = ARCH_X86_64;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100286 } else if (e_machine == EM_MIPS) {
287 arch_ = ARCH_MIPS64;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800288 } else {
Christopher Ferris3958f802017-02-01 15:44:40 -0800289 // Unsupported.
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100290 ALOGI("64 bit elf that is neither aarch64 nor x86_64 nor mips64: e_machine = %d\n",
291 e_machine);
Christopher Ferris3958f802017-02-01 15:44:40 -0800292 return nullptr;
293 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800294 interface.reset(new ElfInterface64(memory));
295 }
296
297 return interface.release();
298}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700299
Christopher Ferris819f1312019-10-03 13:35:48 -0700300int64_t Elf::GetLoadBias(Memory* memory) {
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800301 if (!IsValidElf(memory)) {
302 return 0;
303 }
304
305 uint8_t class_type;
306 if (!memory->Read(EI_CLASS, &class_type, 1)) {
307 return 0;
308 }
309
310 if (class_type == ELFCLASS32) {
311 return ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(memory);
312 } else if (class_type == ELFCLASS64) {
313 return ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(memory);
314 }
315 return 0;
316}
317
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800318void Elf::SetCachingEnabled(bool enable) {
319 if (!cache_enabled_ && enable) {
320 cache_enabled_ = true;
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800321 cache_ = new std::unordered_map<std::string, std::pair<std::shared_ptr<Elf>, bool>>;
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800322 cache_lock_ = new std::mutex;
323 } else if (cache_enabled_ && !enable) {
324 cache_enabled_ = false;
325 delete cache_;
326 delete cache_lock_;
327 }
328}
329
330void Elf::CacheLock() {
331 cache_lock_->lock();
332}
333
334void Elf::CacheUnlock() {
335 cache_lock_->unlock();
336}
337
338void Elf::CacheAdd(MapInfo* info) {
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800339 // If elf_offset != 0, then cache both name:offset and name.
340 // The cached name is used to do lookups if multiple maps for the same
341 // named elf file exist.
342 // For example, if there are two maps boot.odex:1000 and boot.odex:2000
343 // where each reference the entire boot.odex, the cache will properly
344 // use the same cached elf object.
345
346 if (info->offset == 0 || info->elf_offset != 0) {
347 (*cache_)[info->name] = std::make_pair(info->elf, true);
348 }
349
350 if (info->offset != 0) {
351 // The second element in the pair indicates whether elf_offset should
352 // be set to offset when getting out of the cache.
353 (*cache_)[info->name + ':' + std::to_string(info->offset)] =
354 std::make_pair(info->elf, info->elf_offset != 0);
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800355 }
356}
357
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800358bool Elf::CacheAfterCreateMemory(MapInfo* info) {
359 if (info->name.empty() || info->offset == 0 || info->elf_offset == 0) {
360 return false;
361 }
362
363 auto entry = cache_->find(info->name);
364 if (entry == cache_->end()) {
365 return false;
366 }
367
368 // In this case, the whole file is the elf, and the name has already
369 // been cached. Add an entry at name:offset to get this directly out
370 // of the cache next time.
371 info->elf = entry->second.first;
372 (*cache_)[info->name + ':' + std::to_string(info->offset)] = std::make_pair(info->elf, true);
373 return true;
374}
375
376bool Elf::CacheGet(MapInfo* info) {
377 std::string name(info->name);
378 if (info->offset != 0) {
379 name += ':' + std::to_string(info->offset);
380 }
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800381 auto entry = cache_->find(name);
382 if (entry != cache_->end()) {
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800383 info->elf = entry->second.first;
384 if (entry->second.second) {
385 info->elf_offset = info->offset;
386 }
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800387 return true;
388 }
389 return false;
390}
391
Christopher Ferrisbf373ed2019-01-16 17:23:39 -0800392std::string Elf::GetBuildID(Memory* memory) {
393 if (!IsValidElf(memory)) {
394 return "";
395 }
396
397 uint8_t class_type;
398 if (!memory->Read(EI_CLASS, &class_type, 1)) {
399 return "";
400 }
401
402 if (class_type == ELFCLASS32) {
403 return ElfInterface::ReadBuildIDFromMemory<Elf32_Ehdr, Elf32_Shdr, Elf32_Nhdr>(memory);
404 } else if (class_type == ELFCLASS64) {
405 return ElfInterface::ReadBuildIDFromMemory<Elf64_Ehdr, Elf64_Shdr, Elf64_Nhdr>(memory);
406 }
407 return "";
408}
409
Christopher Ferrisd226a512017-07-14 10:37:19 -0700410} // namespace unwindstack