blob: 60ef796250459e7ba14ad7d5b4941c543bb614ef [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 Ferris4cc36d22018-06-06 14:47:31 -070056 interface_->InitHeaders(load_bias_);
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.
80 uint64_t load_bias;
81 if (gnu->Init(&load_bias)) {
Christopher Ferris4cc36d22018-06-06 14:47:31 -070082 gnu->InitHeaders(load_bias);
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 Ferrisd226a512017-07-14 10:37:19 -070091bool Elf::GetSoname(std::string* name) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -080092 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrisd226a512017-07-14 10:37:19 -070093 return valid_ && interface_->GetSoname(name);
94}
95
96uint64_t Elf::GetRelPc(uint64_t pc, const MapInfo* map_info) {
Christopher Ferrise69f4702017-10-19 16:08:58 -070097 return pc - map_info->start + load_bias_ + map_info->elf_offset;
Christopher Ferrisd226a512017-07-14 10:37:19 -070098}
99
100bool Elf::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800101 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700102 return valid_ && (interface_->GetFunctionName(addr, name, func_offset) ||
103 (gnu_debugdata_interface_ &&
104 gnu_debugdata_interface_->GetFunctionName(addr, name, func_offset)));
Christopher Ferrisd226a512017-07-14 10:37:19 -0700105}
106
Christopher Ferris150db122017-12-20 18:49:01 -0800107bool Elf::GetGlobalVariable(const std::string& name, uint64_t* memory_address) {
108 if (!valid_) {
109 return false;
110 }
111
112 if (!interface_->GetGlobalVariable(name, memory_address) &&
113 (gnu_debugdata_interface_ == nullptr ||
114 !gnu_debugdata_interface_->GetGlobalVariable(name, memory_address))) {
115 return false;
116 }
117
118 // Adjust by the load bias.
119 if (*memory_address < load_bias_) {
120 return false;
121 }
122
123 *memory_address -= load_bias_;
124
125 // If this winds up in the dynamic section, then we might need to adjust
126 // the address.
127 uint64_t dynamic_end = interface_->dynamic_vaddr() + interface_->dynamic_size();
128 if (*memory_address >= interface_->dynamic_vaddr() && *memory_address < dynamic_end) {
129 if (interface_->dynamic_vaddr() > interface_->dynamic_offset()) {
130 *memory_address -= interface_->dynamic_vaddr() - interface_->dynamic_offset();
131 } else {
132 *memory_address += interface_->dynamic_offset() - interface_->dynamic_vaddr();
133 }
134 }
135 return true;
136}
137
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800138void Elf::GetLastError(ErrorData* data) {
139 if (valid_) {
140 *data = interface_->last_error();
141 }
142}
143
144ErrorCode Elf::GetLastErrorCode() {
145 if (valid_) {
146 return interface_->LastErrorCode();
147 }
148 return ERROR_NONE;
149}
150
151uint64_t Elf::GetLastErrorAddress() {
152 if (valid_) {
153 return interface_->LastErrorAddress();
154 }
155 return 0;
156}
157
Christopher Ferrise69f4702017-10-19 16:08:58 -0700158// The relative pc is always relative to the start of the map from which it comes.
Christopher Ferris239425b2018-05-17 18:37:38 -0700159bool Elf::Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, Regs* regs, Memory* process_memory,
160 bool* finished) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700161 if (!valid_) {
162 return false;
163 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700164
165 // The relative pc expectd by StepIfSignalHandler is relative to the start of the elf.
Christopher Ferris239425b2018-05-17 18:37:38 -0700166 if (regs->StepIfSignalHandler(rel_pc, this, process_memory)) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700167 *finished = false;
168 return true;
169 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700170
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800171 // Lock during the step which can update information in the object.
172 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700173 return interface_->Step(adjusted_rel_pc, regs, process_memory, finished);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700174}
175
Christopher Ferris3958f802017-02-01 15:44:40 -0800176bool Elf::IsValidElf(Memory* memory) {
177 if (memory == nullptr) {
178 return false;
179 }
180
181 // Verify that this is a valid elf file.
182 uint8_t e_ident[SELFMAG + 1];
Josh Gaoef35aa52017-10-18 11:44:51 -0700183 if (!memory->ReadFully(0, e_ident, SELFMAG)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800184 return false;
185 }
186
187 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
188 return false;
189 }
190 return true;
191}
192
Christopher Ferrisa2f38f12018-10-09 18:49:11 -0700193bool Elf::GetInfo(Memory* memory, uint64_t* size) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700194 if (!IsValidElf(memory)) {
Christopher Ferrisa2f38f12018-10-09 18:49:11 -0700195 return false;
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700196 }
197 *size = 0;
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700198
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700199 uint8_t class_type;
Josh Gaoef35aa52017-10-18 11:44:51 -0700200 if (!memory->ReadFully(EI_CLASS, &class_type, 1)) {
Christopher Ferrisa2f38f12018-10-09 18:49:11 -0700201 return false;
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700202 }
Christopher Ferrisa2f38f12018-10-09 18:49:11 -0700203
204 // Get the maximum size of the elf data from the header.
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700205 if (class_type == ELFCLASS32) {
206 ElfInterface32::GetMaxSize(memory, size);
207 } else if (class_type == ELFCLASS64) {
208 ElfInterface64::GetMaxSize(memory, size);
209 } else {
Christopher Ferrisa2f38f12018-10-09 18:49:11 -0700210 return false;
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700211 }
Christopher Ferrisa2f38f12018-10-09 18:49:11 -0700212 return true;
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700213}
214
Christopher Ferris150db122017-12-20 18:49:01 -0800215bool Elf::IsValidPc(uint64_t pc) {
216 if (!valid_ || pc < load_bias_) {
217 return false;
218 }
Christopher Ferris150db122017-12-20 18:49:01 -0800219
220 if (interface_->IsValidPc(pc)) {
221 return true;
222 }
223
224 if (gnu_debugdata_interface_ != nullptr && gnu_debugdata_interface_->IsValidPc(pc)) {
225 return true;
226 }
227
228 return false;
229}
230
Christopher Ferris3958f802017-02-01 15:44:40 -0800231ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) {
232 if (!IsValidElf(memory)) {
233 return nullptr;
234 }
235
236 std::unique_ptr<ElfInterface> interface;
Josh Gaoef35aa52017-10-18 11:44:51 -0700237 if (!memory->ReadFully(EI_CLASS, &class_type_, 1)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800238 return nullptr;
239 }
240 if (class_type_ == ELFCLASS32) {
241 Elf32_Half e_machine;
Josh Gaoef35aa52017-10-18 11:44:51 -0700242 if (!memory->ReadFully(EI_NIDENT + sizeof(Elf32_Half), &e_machine, sizeof(e_machine))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800243 return nullptr;
244 }
245
Christopher Ferris3958f802017-02-01 15:44:40 -0800246 machine_type_ = e_machine;
247 if (e_machine == EM_ARM) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800248 arch_ = ARCH_ARM;
Christopher Ferris3958f802017-02-01 15:44:40 -0800249 interface.reset(new ElfInterfaceArm(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700250 } else if (e_machine == EM_386) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800251 arch_ = ARCH_X86;
Christopher Ferris3958f802017-02-01 15:44:40 -0800252 interface.reset(new ElfInterface32(memory));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100253 } else if (e_machine == EM_MIPS) {
254 arch_ = ARCH_MIPS;
255 interface.reset(new ElfInterface32(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700256 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800257 // Unsupported.
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100258 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 -0700259 return nullptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800260 }
261 } else if (class_type_ == ELFCLASS64) {
262 Elf64_Half e_machine;
Josh Gaoef35aa52017-10-18 11:44:51 -0700263 if (!memory->ReadFully(EI_NIDENT + sizeof(Elf64_Half), &e_machine, sizeof(e_machine))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800264 return nullptr;
265 }
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800266
267 machine_type_ = e_machine;
268 if (e_machine == EM_AARCH64) {
269 arch_ = ARCH_ARM64;
270 } else if (e_machine == EM_X86_64) {
271 arch_ = ARCH_X86_64;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100272 } else if (e_machine == EM_MIPS) {
273 arch_ = ARCH_MIPS64;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800274 } else {
Christopher Ferris3958f802017-02-01 15:44:40 -0800275 // Unsupported.
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100276 ALOGI("64 bit elf that is neither aarch64 nor x86_64 nor mips64: e_machine = %d\n",
277 e_machine);
Christopher Ferris3958f802017-02-01 15:44:40 -0800278 return nullptr;
279 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800280 interface.reset(new ElfInterface64(memory));
281 }
282
283 return interface.release();
284}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700285
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800286uint64_t Elf::GetLoadBias(Memory* memory) {
287 if (!IsValidElf(memory)) {
288 return 0;
289 }
290
291 uint8_t class_type;
292 if (!memory->Read(EI_CLASS, &class_type, 1)) {
293 return 0;
294 }
295
296 if (class_type == ELFCLASS32) {
297 return ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(memory);
298 } else if (class_type == ELFCLASS64) {
299 return ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(memory);
300 }
301 return 0;
302}
303
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800304void Elf::SetCachingEnabled(bool enable) {
305 if (!cache_enabled_ && enable) {
306 cache_enabled_ = true;
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800307 cache_ = new std::unordered_map<std::string, std::pair<std::shared_ptr<Elf>, bool>>;
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800308 cache_lock_ = new std::mutex;
309 } else if (cache_enabled_ && !enable) {
310 cache_enabled_ = false;
311 delete cache_;
312 delete cache_lock_;
313 }
314}
315
316void Elf::CacheLock() {
317 cache_lock_->lock();
318}
319
320void Elf::CacheUnlock() {
321 cache_lock_->unlock();
322}
323
324void Elf::CacheAdd(MapInfo* info) {
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800325 // If elf_offset != 0, then cache both name:offset and name.
326 // The cached name is used to do lookups if multiple maps for the same
327 // named elf file exist.
328 // For example, if there are two maps boot.odex:1000 and boot.odex:2000
329 // where each reference the entire boot.odex, the cache will properly
330 // use the same cached elf object.
331
332 if (info->offset == 0 || info->elf_offset != 0) {
333 (*cache_)[info->name] = std::make_pair(info->elf, true);
334 }
335
336 if (info->offset != 0) {
337 // The second element in the pair indicates whether elf_offset should
338 // be set to offset when getting out of the cache.
339 (*cache_)[info->name + ':' + std::to_string(info->offset)] =
340 std::make_pair(info->elf, info->elf_offset != 0);
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800341 }
342}
343
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800344bool Elf::CacheAfterCreateMemory(MapInfo* info) {
345 if (info->name.empty() || info->offset == 0 || info->elf_offset == 0) {
346 return false;
347 }
348
349 auto entry = cache_->find(info->name);
350 if (entry == cache_->end()) {
351 return false;
352 }
353
354 // In this case, the whole file is the elf, and the name has already
355 // been cached. Add an entry at name:offset to get this directly out
356 // of the cache next time.
357 info->elf = entry->second.first;
358 (*cache_)[info->name + ':' + std::to_string(info->offset)] = std::make_pair(info->elf, true);
359 return true;
360}
361
362bool Elf::CacheGet(MapInfo* info) {
363 std::string name(info->name);
364 if (info->offset != 0) {
365 name += ':' + std::to_string(info->offset);
366 }
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800367 auto entry = cache_->find(name);
368 if (entry != cache_->end()) {
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800369 info->elf = entry->second.first;
370 if (entry->second.second) {
371 info->elf_offset = info->offset;
372 }
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800373 return true;
374 }
375 return false;
376}
377
Christopher Ferrisd226a512017-07-14 10:37:19 -0700378} // namespace unwindstack