blob: 2c0045691eec6c3f3ca2dc5fe4a34cca85ec6724 [file] [log] [blame]
Christopher Ferris3958f802017-02-01 15:44:40 -08001/*
2 * Copyright (C) 2017 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 <stdint.h>
19
20#include <memory>
21#include <string>
Christopher Ferrisbeae42b2018-02-15 17:36:33 -080022#include <utility>
Christopher Ferris3958f802017-02-01 15:44:40 -080023
Christopher Ferrisbae69f12017-06-28 14:51:54 -070024#include <7zCrc.h>
25#include <Xz.h>
26#include <XzCrc64.h>
27
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080028#include <unwindstack/DwarfError.h>
Christopher Ferrisd226a512017-07-14 10:37:19 -070029#include <unwindstack/DwarfSection.h>
30#include <unwindstack/ElfInterface.h>
31#include <unwindstack/Log.h>
32#include <unwindstack/Memory.h>
33#include <unwindstack/Regs.h>
34
Christopher Ferris61d40972017-06-12 19:14:20 -070035#include "DwarfDebugFrame.h"
36#include "DwarfEhFrame.h"
Christopher Ferrisc9dee842017-11-03 14:50:27 -070037#include "DwarfEhFrameWithHdr.h"
Christopher Ferris8098b1c2017-06-20 13:54:08 -070038#include "Symbols.h"
39
Christopher Ferrisd226a512017-07-14 10:37:19 -070040namespace unwindstack {
41
Christopher Ferris8098b1c2017-06-20 13:54:08 -070042ElfInterface::~ElfInterface() {
43 for (auto symbol : symbols_) {
44 delete symbol;
45 }
46}
Christopher Ferris3958f802017-02-01 15:44:40 -080047
Christopher Ferris150db122017-12-20 18:49:01 -080048bool ElfInterface::IsValidPc(uint64_t pc) {
49 if (!pt_loads_.empty()) {
50 for (auto& entry : pt_loads_) {
51 uint64_t start = entry.second.table_offset;
52 uint64_t end = start + entry.second.table_size;
53 if (pc >= start && pc < end) {
54 return true;
55 }
56 }
57 return false;
58 }
59
60 // No PT_LOAD data, look for a fde for this pc in the section data.
61 if (debug_frame_ != nullptr && debug_frame_->GetFdeFromPc(pc) != nullptr) {
62 return true;
63 }
64
65 if (eh_frame_ != nullptr && eh_frame_->GetFdeFromPc(pc) != nullptr) {
66 return true;
67 }
68
69 return false;
70}
71
Christopher Ferrisbae69f12017-06-28 14:51:54 -070072Memory* ElfInterface::CreateGnuDebugdataMemory() {
73 if (gnu_debugdata_offset_ == 0 || gnu_debugdata_size_ == 0) {
74 return nullptr;
75 }
76
77 // TODO: Only call these initialization functions once.
78 CrcGenerateTable();
79 Crc64GenerateTable();
80
81 std::vector<uint8_t> src(gnu_debugdata_size_);
Josh Gaoef35aa52017-10-18 11:44:51 -070082 if (!memory_->ReadFully(gnu_debugdata_offset_, src.data(), gnu_debugdata_size_)) {
Christopher Ferrisbae69f12017-06-28 14:51:54 -070083 gnu_debugdata_offset_ = 0;
84 gnu_debugdata_size_ = static_cast<uint64_t>(-1);
85 return nullptr;
86 }
87
88 ISzAlloc alloc;
89 CXzUnpacker state;
Sen Jiang2ef03e02018-05-04 12:56:04 -070090 alloc.Alloc = [](ISzAllocPtr, size_t size) { return malloc(size); };
91 alloc.Free = [](ISzAllocPtr, void* ptr) { return free(ptr); };
Christopher Ferrisbae69f12017-06-28 14:51:54 -070092
93 XzUnpacker_Construct(&state, &alloc);
94
95 std::unique_ptr<MemoryBuffer> dst(new MemoryBuffer);
96 int return_val;
97 size_t src_offset = 0;
98 size_t dst_offset = 0;
99 ECoderStatus status;
100 dst->Resize(5 * gnu_debugdata_size_);
101 do {
102 size_t src_remaining = src.size() - src_offset;
103 size_t dst_remaining = dst->Size() - dst_offset;
104 if (dst_remaining < 2 * gnu_debugdata_size_) {
105 dst->Resize(dst->Size() + 2 * gnu_debugdata_size_);
106 dst_remaining += 2 * gnu_debugdata_size_;
107 }
108 return_val = XzUnpacker_Code(&state, dst->GetPtr(dst_offset), &dst_remaining, &src[src_offset],
Sen Jiang2ef03e02018-05-04 12:56:04 -0700109 &src_remaining, true, CODER_FINISH_ANY, &status);
Christopher Ferrisbae69f12017-06-28 14:51:54 -0700110 src_offset += src_remaining;
111 dst_offset += dst_remaining;
112 } while (return_val == SZ_OK && status == CODER_STATUS_NOT_FINISHED);
113 XzUnpacker_Free(&state);
114 if (return_val != SZ_OK || !XzUnpacker_IsStreamWasFinished(&state)) {
115 gnu_debugdata_offset_ = 0;
116 gnu_debugdata_size_ = static_cast<uint64_t>(-1);
117 return nullptr;
118 }
119
120 // Shrink back down to the exact size.
121 dst->Resize(dst_offset);
122
123 return dst.release();
124}
125
Christopher Ferris61d40972017-06-12 19:14:20 -0700126template <typename AddressType>
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700127void ElfInterface::InitHeadersWithTemplate(uint64_t load_bias) {
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700128 if (eh_frame_hdr_offset_ != 0) {
129 eh_frame_.reset(new DwarfEhFrameWithHdr<AddressType>(memory_));
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700130 if (!eh_frame_->Init(eh_frame_hdr_offset_, eh_frame_hdr_size_, load_bias)) {
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700131 eh_frame_.reset(nullptr);
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700132 }
Christopher Ferris1a141a02018-01-24 08:52:47 -0800133 }
134
135 if (eh_frame_.get() == nullptr && eh_frame_offset_ != 0) {
136 // If there is an eh_frame section without an eh_frame_hdr section,
137 // or using the frame hdr object failed to init.
Christopher Ferris61d40972017-06-12 19:14:20 -0700138 eh_frame_.reset(new DwarfEhFrame<AddressType>(memory_));
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700139 if (!eh_frame_->Init(eh_frame_offset_, eh_frame_size_, load_bias)) {
Christopher Ferris61d40972017-06-12 19:14:20 -0700140 eh_frame_.reset(nullptr);
Christopher Ferris61d40972017-06-12 19:14:20 -0700141 }
142 }
143
Christopher Ferris1a141a02018-01-24 08:52:47 -0800144 if (eh_frame_.get() == nullptr) {
145 eh_frame_hdr_offset_ = 0;
146 eh_frame_hdr_size_ = static_cast<uint64_t>(-1);
147 eh_frame_offset_ = 0;
148 eh_frame_size_ = static_cast<uint64_t>(-1);
149 }
150
Christopher Ferris61d40972017-06-12 19:14:20 -0700151 if (debug_frame_offset_ != 0) {
152 debug_frame_.reset(new DwarfDebugFrame<AddressType>(memory_));
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700153 if (!debug_frame_->Init(debug_frame_offset_, debug_frame_size_, load_bias)) {
Christopher Ferris61d40972017-06-12 19:14:20 -0700154 debug_frame_.reset(nullptr);
155 debug_frame_offset_ = 0;
156 debug_frame_size_ = static_cast<uint64_t>(-1);
157 }
158 }
159}
160
Christopher Ferris3958f802017-02-01 15:44:40 -0800161template <typename EhdrType, typename PhdrType, typename ShdrType>
Christopher Ferrise69f4702017-10-19 16:08:58 -0700162bool ElfInterface::ReadAllHeaders(uint64_t* load_bias) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800163 EhdrType ehdr;
Josh Gaoef35aa52017-10-18 11:44:51 -0700164 if (!memory_->ReadFully(0, &ehdr, sizeof(ehdr))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800165 last_error_.code = ERROR_MEMORY_INVALID;
166 last_error_.address = 0;
Christopher Ferris3958f802017-02-01 15:44:40 -0800167 return false;
168 }
169
Christopher Ferrise69f4702017-10-19 16:08:58 -0700170 if (!ReadProgramHeaders<EhdrType, PhdrType>(ehdr, load_bias)) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800171 return false;
172 }
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700173
174 // We could still potentially unwind without the section header
175 // information, so ignore any errors.
176 if (!ReadSectionHeaders<EhdrType, ShdrType>(ehdr)) {
177 log(0, "Malformed section header found, ignoring...");
178 }
179 return true;
Christopher Ferris3958f802017-02-01 15:44:40 -0800180}
181
182template <typename EhdrType, typename PhdrType>
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800183uint64_t ElfInterface::GetLoadBias(Memory* memory) {
184 EhdrType ehdr;
185 if (!memory->Read(0, &ehdr, sizeof(ehdr))) {
186 return false;
187 }
188
189 uint64_t offset = ehdr.e_phoff;
190 for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
191 PhdrType phdr;
192 if (!memory->Read(offset, &phdr, sizeof(phdr))) {
193 return 0;
194 }
195 if (phdr.p_type == PT_LOAD && phdr.p_offset == 0) {
196 return phdr.p_vaddr;
197 }
198 }
199 return 0;
200}
201
202template <typename EhdrType, typename PhdrType>
Christopher Ferrise69f4702017-10-19 16:08:58 -0700203bool ElfInterface::ReadProgramHeaders(const EhdrType& ehdr, uint64_t* load_bias) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800204 uint64_t offset = ehdr.e_phoff;
205 for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
206 PhdrType phdr;
Christopher Ferris5afddb02018-06-29 16:30:55 -0700207 if (!memory_->ReadFully(offset, &phdr, sizeof(phdr))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800208 last_error_.code = ERROR_MEMORY_INVALID;
Christopher Ferris5afddb02018-06-29 16:30:55 -0700209 last_error_.address = offset;
Christopher Ferris3958f802017-02-01 15:44:40 -0800210 return false;
211 }
212
Christopher Ferris3958f802017-02-01 15:44:40 -0800213 switch (phdr.p_type) {
214 case PT_LOAD:
215 {
Christopher Ferris3958f802017-02-01 15:44:40 -0800216 if ((phdr.p_flags & PF_X) == 0) {
217 continue;
218 }
219
Christopher Ferris3958f802017-02-01 15:44:40 -0800220 pt_loads_[phdr.p_offset] = LoadInfo{phdr.p_offset, phdr.p_vaddr,
221 static_cast<size_t>(phdr.p_memsz)};
222 if (phdr.p_offset == 0) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700223 *load_bias = phdr.p_vaddr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800224 }
225 break;
226 }
227
228 case PT_GNU_EH_FRAME:
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700229 // This is really the pointer to the .eh_frame_hdr section.
230 eh_frame_hdr_offset_ = phdr.p_offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700231 eh_frame_hdr_size_ = phdr.p_memsz;
Christopher Ferris3958f802017-02-01 15:44:40 -0800232 break;
233
234 case PT_DYNAMIC:
Christopher Ferris3958f802017-02-01 15:44:40 -0800235 dynamic_offset_ = phdr.p_offset;
Christopher Ferris150db122017-12-20 18:49:01 -0800236 dynamic_vaddr_ = phdr.p_vaddr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800237 dynamic_size_ = phdr.p_memsz;
238 break;
Christopher Ferris5afddb02018-06-29 16:30:55 -0700239
240 default:
241 HandleUnknownType(phdr.p_type, phdr.p_offset, phdr.p_filesz);
242 break;
Christopher Ferris3958f802017-02-01 15:44:40 -0800243 }
244 }
245 return true;
246}
247
248template <typename EhdrType, typename ShdrType>
249bool ElfInterface::ReadSectionHeaders(const EhdrType& ehdr) {
250 uint64_t offset = ehdr.e_shoff;
251 uint64_t sec_offset = 0;
252 uint64_t sec_size = 0;
253
254 // Get the location of the section header names.
255 // If something is malformed in the header table data, we aren't going
256 // to terminate, we'll simply ignore this part.
257 ShdrType shdr;
258 if (ehdr.e_shstrndx < ehdr.e_shnum) {
259 uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize;
Christopher Ferris5afddb02018-06-29 16:30:55 -0700260 if (memory_->ReadFully(sh_offset, &shdr, sizeof(shdr))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800261 sec_offset = shdr.sh_offset;
262 sec_size = shdr.sh_size;
263 }
264 }
265
266 // Skip the first header, it's always going to be NULL.
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700267 offset += ehdr.e_shentsize;
Christopher Ferris3958f802017-02-01 15:44:40 -0800268 for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800269 if (!memory_->Read(offset, &shdr, sizeof(shdr))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800270 last_error_.code = ERROR_MEMORY_INVALID;
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800271 last_error_.address = offset;
Christopher Ferris3958f802017-02-01 15:44:40 -0800272 return false;
273 }
274
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700275 if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700276 // Need to go get the information about the section that contains
277 // the string terminated names.
278 ShdrType str_shdr;
279 if (shdr.sh_link >= ehdr.e_shnum) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800280 last_error_.code = ERROR_UNWIND_INFO;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700281 return false;
282 }
283 uint64_t str_offset = ehdr.e_shoff + shdr.sh_link * ehdr.e_shentsize;
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800284 if (!memory_->Read(str_offset, &str_shdr, sizeof(str_shdr))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800285 last_error_.code = ERROR_MEMORY_INVALID;
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800286 last_error_.address = str_offset;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700287 return false;
288 }
289 if (str_shdr.sh_type != SHT_STRTAB) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800290 last_error_.code = ERROR_UNWIND_INFO;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700291 return false;
292 }
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700293 symbols_.push_back(new Symbols(shdr.sh_offset, shdr.sh_size, shdr.sh_entsize,
294 str_shdr.sh_offset, str_shdr.sh_size));
295 } else if (shdr.sh_type == SHT_PROGBITS && sec_size != 0) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800296 // Look for the .debug_frame and .gnu_debugdata.
Christopher Ferris3958f802017-02-01 15:44:40 -0800297 if (shdr.sh_name < sec_size) {
298 std::string name;
299 if (memory_->ReadString(sec_offset + shdr.sh_name, &name)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700300 uint64_t* offset_ptr = nullptr;
301 uint64_t* size_ptr = nullptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800302 if (name == ".debug_frame") {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700303 offset_ptr = &debug_frame_offset_;
304 size_ptr = &debug_frame_size_;
Christopher Ferris3958f802017-02-01 15:44:40 -0800305 } else if (name == ".gnu_debugdata") {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700306 offset_ptr = &gnu_debugdata_offset_;
307 size_ptr = &gnu_debugdata_size_;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700308 } else if (name == ".eh_frame") {
309 offset_ptr = &eh_frame_offset_;
310 size_ptr = &eh_frame_size_;
311 } else if (eh_frame_hdr_offset_ == 0 && name == ".eh_frame_hdr") {
312 offset_ptr = &eh_frame_hdr_offset_;
313 size_ptr = &eh_frame_hdr_size_;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700314 }
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800315 if (offset_ptr != nullptr) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700316 *offset_ptr = shdr.sh_offset;
317 *size_ptr = shdr.sh_size;
Christopher Ferris3958f802017-02-01 15:44:40 -0800318 }
319 }
320 }
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800321 } else if (shdr.sh_type == SHT_STRTAB) {
322 // In order to read soname, keep track of address to offset mapping.
323 strtabs_.push_back(std::make_pair<uint64_t, uint64_t>(static_cast<uint64_t>(shdr.sh_addr),
324 static_cast<uint64_t>(shdr.sh_offset)));
Christopher Ferris3958f802017-02-01 15:44:40 -0800325 }
326 }
327 return true;
328}
329
330template <typename DynType>
331bool ElfInterface::GetSonameWithTemplate(std::string* soname) {
332 if (soname_type_ == SONAME_INVALID) {
333 return false;
334 }
335 if (soname_type_ == SONAME_VALID) {
336 *soname = soname_;
337 return true;
338 }
339
340 soname_type_ = SONAME_INVALID;
341
342 uint64_t soname_offset = 0;
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800343 uint64_t strtab_addr = 0;
Christopher Ferris3958f802017-02-01 15:44:40 -0800344 uint64_t strtab_size = 0;
345
346 // Find the soname location from the dynamic headers section.
347 DynType dyn;
348 uint64_t offset = dynamic_offset_;
349 uint64_t max_offset = offset + dynamic_size_;
350 for (uint64_t offset = dynamic_offset_; offset < max_offset; offset += sizeof(DynType)) {
Josh Gaoef35aa52017-10-18 11:44:51 -0700351 if (!memory_->ReadFully(offset, &dyn, sizeof(dyn))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800352 last_error_.code = ERROR_MEMORY_INVALID;
353 last_error_.address = offset;
Christopher Ferris3958f802017-02-01 15:44:40 -0800354 return false;
355 }
356
357 if (dyn.d_tag == DT_STRTAB) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800358 strtab_addr = dyn.d_un.d_ptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800359 } else if (dyn.d_tag == DT_STRSZ) {
360 strtab_size = dyn.d_un.d_val;
361 } else if (dyn.d_tag == DT_SONAME) {
362 soname_offset = dyn.d_un.d_val;
363 } else if (dyn.d_tag == DT_NULL) {
364 break;
365 }
366 }
367
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800368 // Need to map the strtab address to the real offset.
369 for (const auto& entry : strtabs_) {
370 if (entry.first == strtab_addr) {
371 soname_offset = entry.second + soname_offset;
372 if (soname_offset >= entry.second + strtab_size) {
373 return false;
374 }
375 if (!memory_->ReadString(soname_offset, &soname_)) {
376 return false;
377 }
378 soname_type_ = SONAME_VALID;
379 *soname = soname_;
380 return true;
381 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800382 }
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800383 return false;
Christopher Ferris3958f802017-02-01 15:44:40 -0800384}
385
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700386template <typename SymType>
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700387bool ElfInterface::GetFunctionNameWithTemplate(uint64_t addr, std::string* name,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700388 uint64_t* func_offset) {
389 if (symbols_.empty()) {
390 return false;
391 }
392
393 for (const auto symbol : symbols_) {
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700394 if (symbol->GetName<SymType>(addr, memory_, name, func_offset)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700395 return true;
396 }
397 }
398 return false;
399}
400
Christopher Ferris150db122017-12-20 18:49:01 -0800401template <typename SymType>
402bool ElfInterface::GetGlobalVariableWithTemplate(const std::string& name, uint64_t* memory_address) {
403 if (symbols_.empty()) {
404 return false;
405 }
406
407 for (const auto symbol : symbols_) {
408 if (symbol->GetGlobal<SymType>(memory_, name, memory_address)) {
409 return true;
410 }
411 }
412 return false;
413}
414
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700415bool ElfInterface::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800416 last_error_.code = ERROR_NONE;
417 last_error_.address = 0;
418
Christopher Ferris1a141a02018-01-24 08:52:47 -0800419 // Try the debug_frame first since it contains the most specific unwind
420 // information.
421 DwarfSection* debug_frame = debug_frame_.get();
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700422 if (debug_frame != nullptr && debug_frame->Step(pc, regs, process_memory, finished)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700423 return true;
424 }
425
Christopher Ferris1a141a02018-01-24 08:52:47 -0800426 // Try the eh_frame next.
427 DwarfSection* eh_frame = eh_frame_.get();
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700428 if (eh_frame != nullptr && eh_frame->Step(pc, regs, process_memory, finished)) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800429 return true;
430 }
431
Christopher Ferrise7b66242017-12-15 11:17:45 -0800432 if (gnu_debugdata_interface_ != nullptr &&
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700433 gnu_debugdata_interface_->Step(pc, regs, process_memory, finished)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700434 return true;
435 }
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800436
437 // Set the error code based on the first error encountered.
438 DwarfSection* section = nullptr;
439 if (debug_frame_ != nullptr) {
440 section = debug_frame_.get();
441 } else if (eh_frame_ != nullptr) {
442 section = eh_frame_.get();
443 } else if (gnu_debugdata_interface_ != nullptr) {
444 last_error_ = gnu_debugdata_interface_->last_error();
445 return false;
446 } else {
447 return false;
448 }
449
450 // Convert the DWARF ERROR to an external error.
451 DwarfErrorCode code = section->LastErrorCode();
452 switch (code) {
453 case DWARF_ERROR_NONE:
454 last_error_.code = ERROR_NONE;
455 break;
456
457 case DWARF_ERROR_MEMORY_INVALID:
458 last_error_.code = ERROR_MEMORY_INVALID;
459 last_error_.address = section->LastErrorAddress();
460 break;
461
462 case DWARF_ERROR_ILLEGAL_VALUE:
463 case DWARF_ERROR_ILLEGAL_STATE:
464 case DWARF_ERROR_STACK_INDEX_NOT_VALID:
465 case DWARF_ERROR_TOO_MANY_ITERATIONS:
466 case DWARF_ERROR_CFA_NOT_DEFINED:
467 case DWARF_ERROR_NO_FDES:
468 last_error_.code = ERROR_UNWIND_INFO;
469 break;
470
471 case DWARF_ERROR_NOT_IMPLEMENTED:
472 case DWARF_ERROR_UNSUPPORTED_VERSION:
473 last_error_.code = ERROR_UNSUPPORTED;
474 break;
475 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800476 return false;
477}
478
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700479// This is an estimation of the size of the elf file using the location
480// of the section headers and size. This assumes that the section headers
481// are at the end of the elf file. If the elf has a load bias, the size
482// will be too large, but this is acceptable.
483template <typename EhdrType>
484void ElfInterface::GetMaxSizeWithTemplate(Memory* memory, uint64_t* size) {
485 EhdrType ehdr;
Josh Gaoef35aa52017-10-18 11:44:51 -0700486 if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700487 return;
488 }
489 if (ehdr.e_shnum == 0) {
490 return;
491 }
492 *size = ehdr.e_shoff + ehdr.e_shentsize * ehdr.e_shnum;
493}
494
Christopher Ferris3958f802017-02-01 15:44:40 -0800495// Instantiate all of the needed template functions.
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700496template void ElfInterface::InitHeadersWithTemplate<uint32_t>(uint64_t);
497template void ElfInterface::InitHeadersWithTemplate<uint64_t>(uint64_t);
Christopher Ferris61d40972017-06-12 19:14:20 -0700498
Christopher Ferrise69f4702017-10-19 16:08:58 -0700499template bool ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(uint64_t*);
500template bool ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(uint64_t*);
Christopher Ferris3958f802017-02-01 15:44:40 -0800501
Christopher Ferrise69f4702017-10-19 16:08:58 -0700502template bool ElfInterface::ReadProgramHeaders<Elf32_Ehdr, Elf32_Phdr>(const Elf32_Ehdr&, uint64_t*);
503template bool ElfInterface::ReadProgramHeaders<Elf64_Ehdr, Elf64_Phdr>(const Elf64_Ehdr&, uint64_t*);
Christopher Ferris3958f802017-02-01 15:44:40 -0800504
505template bool ElfInterface::ReadSectionHeaders<Elf32_Ehdr, Elf32_Shdr>(const Elf32_Ehdr&);
506template bool ElfInterface::ReadSectionHeaders<Elf64_Ehdr, Elf64_Shdr>(const Elf64_Ehdr&);
507
508template bool ElfInterface::GetSonameWithTemplate<Elf32_Dyn>(std::string*);
509template bool ElfInterface::GetSonameWithTemplate<Elf64_Dyn>(std::string*);
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700510
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700511template bool ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(uint64_t, std::string*,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700512 uint64_t*);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700513template bool ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(uint64_t, std::string*,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700514 uint64_t*);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700515
Christopher Ferris150db122017-12-20 18:49:01 -0800516template bool ElfInterface::GetGlobalVariableWithTemplate<Elf32_Sym>(const std::string&, uint64_t*);
517template bool ElfInterface::GetGlobalVariableWithTemplate<Elf64_Sym>(const std::string&, uint64_t*);
518
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700519template void ElfInterface::GetMaxSizeWithTemplate<Elf32_Ehdr>(Memory*, uint64_t*);
520template void ElfInterface::GetMaxSizeWithTemplate<Elf64_Ehdr>(Memory*, uint64_t*);
521
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800522template uint64_t ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(Memory*);
523template uint64_t ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(Memory*);
524
Christopher Ferrisd226a512017-07-14 10:37:19 -0700525} // namespace unwindstack