blob: 02f8a9ae0549e9bfcd7e9767d8e16ef2b6085cf7 [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 Ferrise69f4702017-10-19 16:08:58 -070043bool Elf::Init(bool init_gnu_debugdata) {
44 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_) {
56 interface_->InitHeaders();
Christopher Ferrise69f4702017-10-19 16:08:58 -070057 if (init_gnu_debugdata) {
58 InitGnuDebugdata();
59 } else {
60 gnu_debugdata_interface_.reset(nullptr);
61 }
Christopher Ferris3958f802017-02-01 15:44:40 -080062 } else {
63 interface_.reset(nullptr);
64 }
65 return valid_;
66}
67
Christopher Ferrisbae69f12017-06-28 14:51:54 -070068// It is expensive to initialize the .gnu_debugdata section. Provide a method
69// to initialize this data separately.
70void Elf::InitGnuDebugdata() {
71 if (!valid_ || interface_->gnu_debugdata_offset() == 0) {
72 return;
73 }
74
75 gnu_debugdata_memory_.reset(interface_->CreateGnuDebugdataMemory());
76 gnu_debugdata_interface_.reset(CreateInterfaceFromMemory(gnu_debugdata_memory_.get()));
77 ElfInterface* gnu = gnu_debugdata_interface_.get();
78 if (gnu == nullptr) {
79 return;
80 }
Christopher Ferrise69f4702017-10-19 16:08:58 -070081
82 // Ignore the load_bias from the compressed section, the correct load bias
83 // is in the uncompressed data.
84 uint64_t load_bias;
85 if (gnu->Init(&load_bias)) {
Christopher Ferrisbae69f12017-06-28 14:51:54 -070086 gnu->InitHeaders();
Christopher Ferrise7b66242017-12-15 11:17:45 -080087 interface_->SetGnuDebugdataInterface(gnu);
Christopher Ferrisbae69f12017-06-28 14:51:54 -070088 } else {
89 // Free all of the memory associated with the gnu_debugdata section.
90 gnu_debugdata_memory_.reset(nullptr);
91 gnu_debugdata_interface_.reset(nullptr);
92 }
93}
94
Christopher Ferrisd226a512017-07-14 10:37:19 -070095bool Elf::GetSoname(std::string* name) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -080096 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrisd226a512017-07-14 10:37:19 -070097 return valid_ && interface_->GetSoname(name);
98}
99
100uint64_t Elf::GetRelPc(uint64_t pc, const MapInfo* map_info) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700101 return pc - map_info->start + load_bias_ + map_info->elf_offset;
Christopher Ferrisd226a512017-07-14 10:37:19 -0700102}
103
104bool Elf::GetFunctionName(uint64_t addr, std::string* name, uint64_t* func_offset) {
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800105 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrise69f4702017-10-19 16:08:58 -0700106 return valid_ && (interface_->GetFunctionName(addr, load_bias_, name, func_offset) ||
107 (gnu_debugdata_interface_ && gnu_debugdata_interface_->GetFunctionName(
108 addr, load_bias_, name, func_offset)));
Christopher Ferrisd226a512017-07-14 10:37:19 -0700109}
110
Christopher Ferris150db122017-12-20 18:49:01 -0800111bool Elf::GetGlobalVariable(const std::string& name, uint64_t* memory_address) {
112 if (!valid_) {
113 return false;
114 }
115
116 if (!interface_->GetGlobalVariable(name, memory_address) &&
117 (gnu_debugdata_interface_ == nullptr ||
118 !gnu_debugdata_interface_->GetGlobalVariable(name, memory_address))) {
119 return false;
120 }
121
122 // Adjust by the load bias.
123 if (*memory_address < load_bias_) {
124 return false;
125 }
126
127 *memory_address -= load_bias_;
128
129 // If this winds up in the dynamic section, then we might need to adjust
130 // the address.
131 uint64_t dynamic_end = interface_->dynamic_vaddr() + interface_->dynamic_size();
132 if (*memory_address >= interface_->dynamic_vaddr() && *memory_address < dynamic_end) {
133 if (interface_->dynamic_vaddr() > interface_->dynamic_offset()) {
134 *memory_address -= interface_->dynamic_vaddr() - interface_->dynamic_offset();
135 } else {
136 *memory_address += interface_->dynamic_offset() - interface_->dynamic_vaddr();
137 }
138 }
139 return true;
140}
141
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800142void Elf::GetLastError(ErrorData* data) {
143 if (valid_) {
144 *data = interface_->last_error();
145 }
146}
147
148ErrorCode Elf::GetLastErrorCode() {
149 if (valid_) {
150 return interface_->LastErrorCode();
151 }
152 return ERROR_NONE;
153}
154
155uint64_t Elf::GetLastErrorAddress() {
156 if (valid_) {
157 return interface_->LastErrorAddress();
158 }
159 return 0;
160}
161
Christopher Ferrise69f4702017-10-19 16:08:58 -0700162// The relative pc is always relative to the start of the map from which it comes.
Christopher Ferrisc3d79f72017-11-28 19:14:54 -0800163bool Elf::Step(uint64_t rel_pc, uint64_t adjusted_rel_pc, uint64_t elf_offset, Regs* regs,
164 Memory* process_memory, bool* finished) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700165 if (!valid_) {
166 return false;
167 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700168
169 // The relative pc expectd by StepIfSignalHandler is relative to the start of the elf.
170 if (regs->StepIfSignalHandler(rel_pc + elf_offset, this, process_memory)) {
Christopher Ferrisb9de87f2017-09-20 13:37:24 -0700171 *finished = false;
172 return true;
173 }
Christopher Ferrise69f4702017-10-19 16:08:58 -0700174
Christopher Ferrisbe788d82017-11-27 14:50:38 -0800175 // Lock during the step which can update information in the object.
176 std::lock_guard<std::mutex> guard(lock_);
Christopher Ferrise7b66242017-12-15 11:17:45 -0800177 return interface_->Step(adjusted_rel_pc, load_bias_, regs, process_memory, finished);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700178}
179
Christopher Ferris3958f802017-02-01 15:44:40 -0800180bool Elf::IsValidElf(Memory* memory) {
181 if (memory == nullptr) {
182 return false;
183 }
184
185 // Verify that this is a valid elf file.
186 uint8_t e_ident[SELFMAG + 1];
Josh Gaoef35aa52017-10-18 11:44:51 -0700187 if (!memory->ReadFully(0, e_ident, SELFMAG)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800188 return false;
189 }
190
191 if (memcmp(e_ident, ELFMAG, SELFMAG) != 0) {
192 return false;
193 }
194 return true;
195}
196
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700197void Elf::GetInfo(Memory* memory, bool* valid, uint64_t* size) {
198 if (!IsValidElf(memory)) {
199 *valid = false;
200 return;
201 }
202 *size = 0;
203 *valid = true;
204
205 // Now read the section header information.
206 uint8_t class_type;
Josh Gaoef35aa52017-10-18 11:44:51 -0700207 if (!memory->ReadFully(EI_CLASS, &class_type, 1)) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700208 return;
209 }
210 if (class_type == ELFCLASS32) {
211 ElfInterface32::GetMaxSize(memory, size);
212 } else if (class_type == ELFCLASS64) {
213 ElfInterface64::GetMaxSize(memory, size);
214 } else {
215 *valid = false;
216 }
217}
218
Christopher Ferris150db122017-12-20 18:49:01 -0800219bool Elf::IsValidPc(uint64_t pc) {
220 if (!valid_ || pc < load_bias_) {
221 return false;
222 }
223 pc -= load_bias_;
224
225 if (interface_->IsValidPc(pc)) {
226 return true;
227 }
228
229 if (gnu_debugdata_interface_ != nullptr && gnu_debugdata_interface_->IsValidPc(pc)) {
230 return true;
231 }
232
233 return false;
234}
235
Christopher Ferris3958f802017-02-01 15:44:40 -0800236ElfInterface* Elf::CreateInterfaceFromMemory(Memory* memory) {
237 if (!IsValidElf(memory)) {
238 return nullptr;
239 }
240
241 std::unique_ptr<ElfInterface> interface;
Josh Gaoef35aa52017-10-18 11:44:51 -0700242 if (!memory->ReadFully(EI_CLASS, &class_type_, 1)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800243 return nullptr;
244 }
245 if (class_type_ == ELFCLASS32) {
246 Elf32_Half e_machine;
Josh Gaoef35aa52017-10-18 11:44:51 -0700247 if (!memory->ReadFully(EI_NIDENT + sizeof(Elf32_Half), &e_machine, sizeof(e_machine))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800248 return nullptr;
249 }
250
Christopher Ferris3958f802017-02-01 15:44:40 -0800251 machine_type_ = e_machine;
252 if (e_machine == EM_ARM) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800253 arch_ = ARCH_ARM;
Christopher Ferris3958f802017-02-01 15:44:40 -0800254 interface.reset(new ElfInterfaceArm(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700255 } else if (e_machine == EM_386) {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800256 arch_ = ARCH_X86;
Christopher Ferris3958f802017-02-01 15:44:40 -0800257 interface.reset(new ElfInterface32(memory));
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100258 } else if (e_machine == EM_MIPS) {
259 arch_ = ARCH_MIPS;
260 interface.reset(new ElfInterface32(memory));
Christopher Ferrisa0196652017-07-18 16:09:20 -0700261 } else {
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800262 // Unsupported.
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100263 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 -0700264 return nullptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800265 }
266 } else if (class_type_ == ELFCLASS64) {
267 Elf64_Half e_machine;
Josh Gaoef35aa52017-10-18 11:44:51 -0700268 if (!memory->ReadFully(EI_NIDENT + sizeof(Elf64_Half), &e_machine, sizeof(e_machine))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800269 return nullptr;
270 }
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800271
272 machine_type_ = e_machine;
273 if (e_machine == EM_AARCH64) {
274 arch_ = ARCH_ARM64;
275 } else if (e_machine == EM_X86_64) {
276 arch_ = ARCH_X86_64;
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100277 } else if (e_machine == EM_MIPS) {
278 arch_ = ARCH_MIPS64;
Christopher Ferrisd06001d2017-11-30 18:56:01 -0800279 } else {
Christopher Ferris3958f802017-02-01 15:44:40 -0800280 // Unsupported.
Douglas Leung61b1a1a2017-11-08 10:53:53 +0100281 ALOGI("64 bit elf that is neither aarch64 nor x86_64 nor mips64: e_machine = %d\n",
282 e_machine);
Christopher Ferris3958f802017-02-01 15:44:40 -0800283 return nullptr;
284 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800285 interface.reset(new ElfInterface64(memory));
286 }
287
288 return interface.release();
289}
Christopher Ferrisd226a512017-07-14 10:37:19 -0700290
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800291uint64_t Elf::GetLoadBias(Memory* memory) {
292 if (!IsValidElf(memory)) {
293 return 0;
294 }
295
296 uint8_t class_type;
297 if (!memory->Read(EI_CLASS, &class_type, 1)) {
298 return 0;
299 }
300
301 if (class_type == ELFCLASS32) {
302 return ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(memory);
303 } else if (class_type == ELFCLASS64) {
304 return ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(memory);
305 }
306 return 0;
307}
308
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800309void Elf::SetCachingEnabled(bool enable) {
310 if (!cache_enabled_ && enable) {
311 cache_enabled_ = true;
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800312 cache_ = new std::unordered_map<std::string, std::pair<std::shared_ptr<Elf>, bool>>;
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800313 cache_lock_ = new std::mutex;
314 } else if (cache_enabled_ && !enable) {
315 cache_enabled_ = false;
316 delete cache_;
317 delete cache_lock_;
318 }
319}
320
321void Elf::CacheLock() {
322 cache_lock_->lock();
323}
324
325void Elf::CacheUnlock() {
326 cache_lock_->unlock();
327}
328
329void Elf::CacheAdd(MapInfo* info) {
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800330 // If elf_offset != 0, then cache both name:offset and name.
331 // The cached name is used to do lookups if multiple maps for the same
332 // named elf file exist.
333 // For example, if there are two maps boot.odex:1000 and boot.odex:2000
334 // where each reference the entire boot.odex, the cache will properly
335 // use the same cached elf object.
336
337 if (info->offset == 0 || info->elf_offset != 0) {
338 (*cache_)[info->name] = std::make_pair(info->elf, true);
339 }
340
341 if (info->offset != 0) {
342 // The second element in the pair indicates whether elf_offset should
343 // be set to offset when getting out of the cache.
344 (*cache_)[info->name + ':' + std::to_string(info->offset)] =
345 std::make_pair(info->elf, info->elf_offset != 0);
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800346 }
347}
348
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800349bool Elf::CacheAfterCreateMemory(MapInfo* info) {
350 if (info->name.empty() || info->offset == 0 || info->elf_offset == 0) {
351 return false;
352 }
353
354 auto entry = cache_->find(info->name);
355 if (entry == cache_->end()) {
356 return false;
357 }
358
359 // In this case, the whole file is the elf, and the name has already
360 // been cached. Add an entry at name:offset to get this directly out
361 // of the cache next time.
362 info->elf = entry->second.first;
363 (*cache_)[info->name + ':' + std::to_string(info->offset)] = std::make_pair(info->elf, true);
364 return true;
365}
366
367bool Elf::CacheGet(MapInfo* info) {
368 std::string name(info->name);
369 if (info->offset != 0) {
370 name += ':' + std::to_string(info->offset);
371 }
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800372 auto entry = cache_->find(name);
373 if (entry != cache_->end()) {
Christopher Ferrisd9575b62018-02-16 13:48:19 -0800374 info->elf = entry->second.first;
375 if (entry->second.second) {
376 info->elf_offset = info->offset;
377 }
Christopher Ferris0b79ae12018-01-25 12:15:56 -0800378 return true;
379 }
380 return false;
381}
382
Christopher Ferrisd226a512017-07-14 10:37:19 -0700383} // namespace unwindstack