blob: d0af94af1729fd21542fbb8b5d7f97bccbdb01f4 [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 Ferris5acf0692018-08-01 13:10:46 -0700170 // If we have enough information that this is an elf file, then allow
171 // malformed program and section headers.
172 ReadProgramHeaders<EhdrType, PhdrType>(ehdr, load_bias);
173 ReadSectionHeaders<EhdrType, ShdrType>(ehdr);
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700174 return true;
Christopher Ferris3958f802017-02-01 15:44:40 -0800175}
176
177template <typename EhdrType, typename PhdrType>
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800178uint64_t ElfInterface::GetLoadBias(Memory* memory) {
179 EhdrType ehdr;
180 if (!memory->Read(0, &ehdr, sizeof(ehdr))) {
181 return false;
182 }
183
184 uint64_t offset = ehdr.e_phoff;
185 for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
186 PhdrType phdr;
187 if (!memory->Read(offset, &phdr, sizeof(phdr))) {
188 return 0;
189 }
190 if (phdr.p_type == PT_LOAD && phdr.p_offset == 0) {
191 return phdr.p_vaddr;
192 }
193 }
194 return 0;
195}
196
197template <typename EhdrType, typename PhdrType>
Christopher Ferris5acf0692018-08-01 13:10:46 -0700198void ElfInterface::ReadProgramHeaders(const EhdrType& ehdr, uint64_t* load_bias) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800199 uint64_t offset = ehdr.e_phoff;
200 for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
201 PhdrType phdr;
Christopher Ferris5afddb02018-06-29 16:30:55 -0700202 if (!memory_->ReadFully(offset, &phdr, sizeof(phdr))) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700203 return;
Christopher Ferris3958f802017-02-01 15:44:40 -0800204 }
205
Christopher Ferris3958f802017-02-01 15:44:40 -0800206 switch (phdr.p_type) {
207 case PT_LOAD:
208 {
Christopher Ferris3958f802017-02-01 15:44:40 -0800209 if ((phdr.p_flags & PF_X) == 0) {
210 continue;
211 }
212
Christopher Ferris3958f802017-02-01 15:44:40 -0800213 pt_loads_[phdr.p_offset] = LoadInfo{phdr.p_offset, phdr.p_vaddr,
214 static_cast<size_t>(phdr.p_memsz)};
215 if (phdr.p_offset == 0) {
Christopher Ferrise69f4702017-10-19 16:08:58 -0700216 *load_bias = phdr.p_vaddr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800217 }
218 break;
219 }
220
221 case PT_GNU_EH_FRAME:
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700222 // This is really the pointer to the .eh_frame_hdr section.
223 eh_frame_hdr_offset_ = phdr.p_offset;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700224 eh_frame_hdr_size_ = phdr.p_memsz;
Christopher Ferris3958f802017-02-01 15:44:40 -0800225 break;
226
227 case PT_DYNAMIC:
Christopher Ferris3958f802017-02-01 15:44:40 -0800228 dynamic_offset_ = phdr.p_offset;
Christopher Ferris150db122017-12-20 18:49:01 -0800229 dynamic_vaddr_ = phdr.p_vaddr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800230 dynamic_size_ = phdr.p_memsz;
231 break;
Christopher Ferris5afddb02018-06-29 16:30:55 -0700232
233 default:
234 HandleUnknownType(phdr.p_type, phdr.p_offset, phdr.p_filesz);
235 break;
Christopher Ferris3958f802017-02-01 15:44:40 -0800236 }
237 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800238}
239
Florian Mayerda459e52018-11-23 16:56:17 +0000240template <typename NhdrType>
241bool ElfInterface::ReadBuildID(std::string* build_id) {
242 // Ensure there is no overflow in any of the calulations below.
243 uint64_t tmp;
244 if (__builtin_add_overflow(gnu_build_id_offset_, gnu_build_id_size_, &tmp)) {
245 return false;
246 }
247
248 uint64_t offset = 0;
249 while (offset < gnu_build_id_size_) {
250 if (gnu_build_id_size_ - offset < sizeof(NhdrType)) {
251 return false;
252 }
253 NhdrType hdr;
254 if (!memory_->ReadFully(gnu_build_id_offset_ + offset, &hdr, sizeof(hdr))) {
255 return false;
256 }
257 offset += sizeof(hdr);
258
259 if (gnu_build_id_size_ - offset < hdr.n_namesz) {
260 return false;
261 }
262 if (hdr.n_namesz > 0) {
263 std::string name(hdr.n_namesz, '\0');
264 if (!memory_->ReadFully(gnu_build_id_offset_ + offset, &(name[0]), hdr.n_namesz)) {
265 return false;
266 }
267
268 // Trim trailing \0 as GNU is stored as a C string in the ELF file.
269 if (name.back() == '\0')
270 name.resize(name.size() - 1);
271
272 // Align hdr.n_namesz to next power multiple of 4. See man 5 elf.
273 offset += (hdr.n_namesz + 3) & ~3;
274
275 if (name == "GNU" && hdr.n_type == NT_GNU_BUILD_ID) {
276 if (gnu_build_id_size_ - offset < hdr.n_descsz) {
277 return false;
278 }
279 build_id->resize(hdr.n_descsz);
280 return memory_->ReadFully(gnu_build_id_offset_ + offset, &(*build_id)[0],
281 hdr.n_descsz);
282 }
283 }
284 // Align hdr.n_descsz to next power multiple of 4. See man 5 elf.
285 offset += (hdr.n_descsz + 3) & ~3;
286 }
287 return false;
288}
289
Christopher Ferris3958f802017-02-01 15:44:40 -0800290template <typename EhdrType, typename ShdrType>
Christopher Ferris5acf0692018-08-01 13:10:46 -0700291void ElfInterface::ReadSectionHeaders(const EhdrType& ehdr) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800292 uint64_t offset = ehdr.e_shoff;
293 uint64_t sec_offset = 0;
294 uint64_t sec_size = 0;
295
296 // Get the location of the section header names.
297 // If something is malformed in the header table data, we aren't going
298 // to terminate, we'll simply ignore this part.
299 ShdrType shdr;
300 if (ehdr.e_shstrndx < ehdr.e_shnum) {
301 uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize;
Christopher Ferris5afddb02018-06-29 16:30:55 -0700302 if (memory_->ReadFully(sh_offset, &shdr, sizeof(shdr))) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800303 sec_offset = shdr.sh_offset;
304 sec_size = shdr.sh_size;
305 }
306 }
307
308 // Skip the first header, it's always going to be NULL.
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700309 offset += ehdr.e_shentsize;
Christopher Ferris3958f802017-02-01 15:44:40 -0800310 for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800311 if (!memory_->Read(offset, &shdr, sizeof(shdr))) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700312 return;
Christopher Ferris3958f802017-02-01 15:44:40 -0800313 }
314
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700315 if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700316 // Need to go get the information about the section that contains
317 // the string terminated names.
318 ShdrType str_shdr;
319 if (shdr.sh_link >= ehdr.e_shnum) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700320 continue;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700321 }
322 uint64_t str_offset = ehdr.e_shoff + shdr.sh_link * ehdr.e_shentsize;
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800323 if (!memory_->Read(str_offset, &str_shdr, sizeof(str_shdr))) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700324 continue;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700325 }
326 if (str_shdr.sh_type != SHT_STRTAB) {
Christopher Ferris5acf0692018-08-01 13:10:46 -0700327 continue;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700328 }
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700329 symbols_.push_back(new Symbols(shdr.sh_offset, shdr.sh_size, shdr.sh_entsize,
330 str_shdr.sh_offset, str_shdr.sh_size));
331 } else if (shdr.sh_type == SHT_PROGBITS && sec_size != 0) {
Christopher Ferris3958f802017-02-01 15:44:40 -0800332 // Look for the .debug_frame and .gnu_debugdata.
Christopher Ferris3958f802017-02-01 15:44:40 -0800333 if (shdr.sh_name < sec_size) {
334 std::string name;
335 if (memory_->ReadString(sec_offset + shdr.sh_name, &name)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700336 uint64_t* offset_ptr = nullptr;
337 uint64_t* size_ptr = nullptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800338 if (name == ".debug_frame") {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700339 offset_ptr = &debug_frame_offset_;
340 size_ptr = &debug_frame_size_;
Christopher Ferris3958f802017-02-01 15:44:40 -0800341 } else if (name == ".gnu_debugdata") {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700342 offset_ptr = &gnu_debugdata_offset_;
343 size_ptr = &gnu_debugdata_size_;
Christopher Ferrisc9dee842017-11-03 14:50:27 -0700344 } else if (name == ".eh_frame") {
345 offset_ptr = &eh_frame_offset_;
346 size_ptr = &eh_frame_size_;
347 } else if (eh_frame_hdr_offset_ == 0 && name == ".eh_frame_hdr") {
348 offset_ptr = &eh_frame_hdr_offset_;
349 size_ptr = &eh_frame_hdr_size_;
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700350 }
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800351 if (offset_ptr != nullptr) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700352 *offset_ptr = shdr.sh_offset;
353 *size_ptr = shdr.sh_size;
Christopher Ferris3958f802017-02-01 15:44:40 -0800354 }
355 }
356 }
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800357 } else if (shdr.sh_type == SHT_STRTAB) {
358 // In order to read soname, keep track of address to offset mapping.
359 strtabs_.push_back(std::make_pair<uint64_t, uint64_t>(static_cast<uint64_t>(shdr.sh_addr),
360 static_cast<uint64_t>(shdr.sh_offset)));
Florian Mayerda459e52018-11-23 16:56:17 +0000361 } else if (shdr.sh_type == SHT_NOTE) {
362 if (shdr.sh_name < sec_size) {
363 std::string name;
364 if (memory_->ReadString(sec_offset + shdr.sh_name, &name) &&
365 name == ".note.gnu.build-id") {
366 gnu_build_id_offset_ = shdr.sh_offset;
367 gnu_build_id_size_ = shdr.sh_size;
368 }
369 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800370 }
371 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800372}
373
374template <typename DynType>
375bool ElfInterface::GetSonameWithTemplate(std::string* soname) {
376 if (soname_type_ == SONAME_INVALID) {
377 return false;
378 }
379 if (soname_type_ == SONAME_VALID) {
380 *soname = soname_;
381 return true;
382 }
383
384 soname_type_ = SONAME_INVALID;
385
386 uint64_t soname_offset = 0;
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800387 uint64_t strtab_addr = 0;
Christopher Ferris3958f802017-02-01 15:44:40 -0800388 uint64_t strtab_size = 0;
389
390 // Find the soname location from the dynamic headers section.
391 DynType dyn;
392 uint64_t offset = dynamic_offset_;
393 uint64_t max_offset = offset + dynamic_size_;
394 for (uint64_t offset = dynamic_offset_; offset < max_offset; offset += sizeof(DynType)) {
Josh Gaoef35aa52017-10-18 11:44:51 -0700395 if (!memory_->ReadFully(offset, &dyn, sizeof(dyn))) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800396 last_error_.code = ERROR_MEMORY_INVALID;
397 last_error_.address = offset;
Christopher Ferris3958f802017-02-01 15:44:40 -0800398 return false;
399 }
400
401 if (dyn.d_tag == DT_STRTAB) {
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800402 strtab_addr = dyn.d_un.d_ptr;
Christopher Ferris3958f802017-02-01 15:44:40 -0800403 } else if (dyn.d_tag == DT_STRSZ) {
404 strtab_size = dyn.d_un.d_val;
405 } else if (dyn.d_tag == DT_SONAME) {
406 soname_offset = dyn.d_un.d_val;
407 } else if (dyn.d_tag == DT_NULL) {
408 break;
409 }
410 }
411
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800412 // Need to map the strtab address to the real offset.
413 for (const auto& entry : strtabs_) {
414 if (entry.first == strtab_addr) {
415 soname_offset = entry.second + soname_offset;
416 if (soname_offset >= entry.second + strtab_size) {
417 return false;
418 }
419 if (!memory_->ReadString(soname_offset, &soname_)) {
420 return false;
421 }
422 soname_type_ = SONAME_VALID;
423 *soname = soname_;
424 return true;
425 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800426 }
Christopher Ferrisbeae42b2018-02-15 17:36:33 -0800427 return false;
Christopher Ferris3958f802017-02-01 15:44:40 -0800428}
429
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700430template <typename SymType>
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700431bool ElfInterface::GetFunctionNameWithTemplate(uint64_t addr, std::string* name,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700432 uint64_t* func_offset) {
433 if (symbols_.empty()) {
434 return false;
435 }
436
437 for (const auto symbol : symbols_) {
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700438 if (symbol->GetName<SymType>(addr, memory_, name, func_offset)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700439 return true;
440 }
441 }
442 return false;
443}
444
Christopher Ferris150db122017-12-20 18:49:01 -0800445template <typename SymType>
446bool ElfInterface::GetGlobalVariableWithTemplate(const std::string& name, uint64_t* memory_address) {
447 if (symbols_.empty()) {
448 return false;
449 }
450
451 for (const auto symbol : symbols_) {
452 if (symbol->GetGlobal<SymType>(memory_, name, memory_address)) {
453 return true;
454 }
455 }
456 return false;
457}
458
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700459bool ElfInterface::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800460 last_error_.code = ERROR_NONE;
461 last_error_.address = 0;
462
Christopher Ferris1a141a02018-01-24 08:52:47 -0800463 // Try the debug_frame first since it contains the most specific unwind
464 // information.
465 DwarfSection* debug_frame = debug_frame_.get();
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700466 if (debug_frame != nullptr && debug_frame->Step(pc, regs, process_memory, finished)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700467 return true;
468 }
469
Christopher Ferris1a141a02018-01-24 08:52:47 -0800470 // Try the eh_frame next.
471 DwarfSection* eh_frame = eh_frame_.get();
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700472 if (eh_frame != nullptr && eh_frame->Step(pc, regs, process_memory, finished)) {
Christopher Ferrise7b66242017-12-15 11:17:45 -0800473 return true;
474 }
475
Christopher Ferrise7b66242017-12-15 11:17:45 -0800476 if (gnu_debugdata_interface_ != nullptr &&
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700477 gnu_debugdata_interface_->Step(pc, regs, process_memory, finished)) {
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700478 return true;
479 }
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -0800480
481 // Set the error code based on the first error encountered.
482 DwarfSection* section = nullptr;
483 if (debug_frame_ != nullptr) {
484 section = debug_frame_.get();
485 } else if (eh_frame_ != nullptr) {
486 section = eh_frame_.get();
487 } else if (gnu_debugdata_interface_ != nullptr) {
488 last_error_ = gnu_debugdata_interface_->last_error();
489 return false;
490 } else {
491 return false;
492 }
493
494 // Convert the DWARF ERROR to an external error.
495 DwarfErrorCode code = section->LastErrorCode();
496 switch (code) {
497 case DWARF_ERROR_NONE:
498 last_error_.code = ERROR_NONE;
499 break;
500
501 case DWARF_ERROR_MEMORY_INVALID:
502 last_error_.code = ERROR_MEMORY_INVALID;
503 last_error_.address = section->LastErrorAddress();
504 break;
505
506 case DWARF_ERROR_ILLEGAL_VALUE:
507 case DWARF_ERROR_ILLEGAL_STATE:
508 case DWARF_ERROR_STACK_INDEX_NOT_VALID:
509 case DWARF_ERROR_TOO_MANY_ITERATIONS:
510 case DWARF_ERROR_CFA_NOT_DEFINED:
511 case DWARF_ERROR_NO_FDES:
512 last_error_.code = ERROR_UNWIND_INFO;
513 break;
514
515 case DWARF_ERROR_NOT_IMPLEMENTED:
516 case DWARF_ERROR_UNSUPPORTED_VERSION:
517 last_error_.code = ERROR_UNSUPPORTED;
518 break;
519 }
Christopher Ferris3958f802017-02-01 15:44:40 -0800520 return false;
521}
522
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700523// This is an estimation of the size of the elf file using the location
524// of the section headers and size. This assumes that the section headers
525// are at the end of the elf file. If the elf has a load bias, the size
526// will be too large, but this is acceptable.
527template <typename EhdrType>
528void ElfInterface::GetMaxSizeWithTemplate(Memory* memory, uint64_t* size) {
529 EhdrType ehdr;
Josh Gaoef35aa52017-10-18 11:44:51 -0700530 if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700531 return;
532 }
533 if (ehdr.e_shnum == 0) {
534 return;
535 }
536 *size = ehdr.e_shoff + ehdr.e_shentsize * ehdr.e_shnum;
537}
538
Christopher Ferris3958f802017-02-01 15:44:40 -0800539// Instantiate all of the needed template functions.
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700540template void ElfInterface::InitHeadersWithTemplate<uint32_t>(uint64_t);
541template void ElfInterface::InitHeadersWithTemplate<uint64_t>(uint64_t);
Christopher Ferris61d40972017-06-12 19:14:20 -0700542
Christopher Ferrise69f4702017-10-19 16:08:58 -0700543template bool ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(uint64_t*);
544template bool ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(uint64_t*);
Christopher Ferris3958f802017-02-01 15:44:40 -0800545
Christopher Ferris5acf0692018-08-01 13:10:46 -0700546template void ElfInterface::ReadProgramHeaders<Elf32_Ehdr, Elf32_Phdr>(const Elf32_Ehdr&,
547 uint64_t*);
548template void ElfInterface::ReadProgramHeaders<Elf64_Ehdr, Elf64_Phdr>(const Elf64_Ehdr&,
549 uint64_t*);
Christopher Ferris3958f802017-02-01 15:44:40 -0800550
Christopher Ferris5acf0692018-08-01 13:10:46 -0700551template void ElfInterface::ReadSectionHeaders<Elf32_Ehdr, Elf32_Shdr>(const Elf32_Ehdr&);
552template void ElfInterface::ReadSectionHeaders<Elf64_Ehdr, Elf64_Shdr>(const Elf64_Ehdr&);
Christopher Ferris3958f802017-02-01 15:44:40 -0800553
Florian Mayerda459e52018-11-23 16:56:17 +0000554template bool ElfInterface::ReadBuildID<Elf32_Nhdr>(std::string*);
555template bool ElfInterface::ReadBuildID<Elf64_Nhdr>(std::string*);
556
Christopher Ferris3958f802017-02-01 15:44:40 -0800557template bool ElfInterface::GetSonameWithTemplate<Elf32_Dyn>(std::string*);
558template bool ElfInterface::GetSonameWithTemplate<Elf64_Dyn>(std::string*);
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700559
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700560template bool ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(uint64_t, std::string*,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700561 uint64_t*);
Christopher Ferris4cc36d22018-06-06 14:47:31 -0700562template bool ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(uint64_t, std::string*,
Christopher Ferris8098b1c2017-06-20 13:54:08 -0700563 uint64_t*);
Christopher Ferrisd226a512017-07-14 10:37:19 -0700564
Christopher Ferris150db122017-12-20 18:49:01 -0800565template bool ElfInterface::GetGlobalVariableWithTemplate<Elf32_Sym>(const std::string&, uint64_t*);
566template bool ElfInterface::GetGlobalVariableWithTemplate<Elf64_Sym>(const std::string&, uint64_t*);
567
Christopher Ferris3f805ac2017-08-30 13:15:19 -0700568template void ElfInterface::GetMaxSizeWithTemplate<Elf32_Ehdr>(Memory*, uint64_t*);
569template void ElfInterface::GetMaxSizeWithTemplate<Elf64_Ehdr>(Memory*, uint64_t*);
570
Christopher Ferrisb7de5f52017-12-01 21:37:37 -0800571template uint64_t ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(Memory*);
572template uint64_t ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(Memory*);
573
Christopher Ferrisd226a512017-07-14 10:37:19 -0700574} // namespace unwindstack