2017-02-02 00:44:40 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <elf.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2018-02-16 02:36:33 +01:00
|
|
|
#include <utility>
|
2017-02-02 00:44:40 +01:00
|
|
|
|
2017-06-28 23:51:54 +02:00
|
|
|
#include <7zCrc.h>
|
|
|
|
#include <Xz.h>
|
|
|
|
#include <XzCrc64.h>
|
|
|
|
|
2018-01-24 02:52:23 +01:00
|
|
|
#include <unwindstack/DwarfError.h>
|
2017-07-14 19:37:19 +02:00
|
|
|
#include <unwindstack/DwarfSection.h>
|
|
|
|
#include <unwindstack/ElfInterface.h>
|
|
|
|
#include <unwindstack/Log.h>
|
|
|
|
#include <unwindstack/Memory.h>
|
|
|
|
#include <unwindstack/Regs.h>
|
|
|
|
|
2017-06-13 04:14:20 +02:00
|
|
|
#include "DwarfDebugFrame.h"
|
|
|
|
#include "DwarfEhFrame.h"
|
2017-11-03 22:50:27 +01:00
|
|
|
#include "DwarfEhFrameWithHdr.h"
|
2017-06-20 22:54:08 +02:00
|
|
|
#include "Symbols.h"
|
|
|
|
|
2017-07-14 19:37:19 +02:00
|
|
|
namespace unwindstack {
|
|
|
|
|
2017-06-20 22:54:08 +02:00
|
|
|
ElfInterface::~ElfInterface() {
|
|
|
|
for (auto symbol : symbols_) {
|
|
|
|
delete symbol;
|
|
|
|
}
|
|
|
|
}
|
2017-02-02 00:44:40 +01:00
|
|
|
|
2017-12-21 03:49:01 +01:00
|
|
|
bool ElfInterface::IsValidPc(uint64_t pc) {
|
|
|
|
if (!pt_loads_.empty()) {
|
|
|
|
for (auto& entry : pt_loads_) {
|
|
|
|
uint64_t start = entry.second.table_offset;
|
|
|
|
uint64_t end = start + entry.second.table_size;
|
|
|
|
if (pc >= start && pc < end) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No PT_LOAD data, look for a fde for this pc in the section data.
|
|
|
|
if (debug_frame_ != nullptr && debug_frame_->GetFdeFromPc(pc) != nullptr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eh_frame_ != nullptr && eh_frame_->GetFdeFromPc(pc) != nullptr) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-28 23:51:54 +02:00
|
|
|
Memory* ElfInterface::CreateGnuDebugdataMemory() {
|
|
|
|
if (gnu_debugdata_offset_ == 0 || gnu_debugdata_size_ == 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Only call these initialization functions once.
|
|
|
|
CrcGenerateTable();
|
|
|
|
Crc64GenerateTable();
|
|
|
|
|
|
|
|
std::vector<uint8_t> src(gnu_debugdata_size_);
|
2017-10-18 20:44:51 +02:00
|
|
|
if (!memory_->ReadFully(gnu_debugdata_offset_, src.data(), gnu_debugdata_size_)) {
|
2017-06-28 23:51:54 +02:00
|
|
|
gnu_debugdata_offset_ = 0;
|
|
|
|
gnu_debugdata_size_ = static_cast<uint64_t>(-1);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ISzAlloc alloc;
|
|
|
|
CXzUnpacker state;
|
2018-05-04 21:56:04 +02:00
|
|
|
alloc.Alloc = [](ISzAllocPtr, size_t size) { return malloc(size); };
|
|
|
|
alloc.Free = [](ISzAllocPtr, void* ptr) { return free(ptr); };
|
2017-06-28 23:51:54 +02:00
|
|
|
|
|
|
|
XzUnpacker_Construct(&state, &alloc);
|
|
|
|
|
|
|
|
std::unique_ptr<MemoryBuffer> dst(new MemoryBuffer);
|
|
|
|
int return_val;
|
|
|
|
size_t src_offset = 0;
|
|
|
|
size_t dst_offset = 0;
|
|
|
|
ECoderStatus status;
|
|
|
|
dst->Resize(5 * gnu_debugdata_size_);
|
|
|
|
do {
|
|
|
|
size_t src_remaining = src.size() - src_offset;
|
|
|
|
size_t dst_remaining = dst->Size() - dst_offset;
|
|
|
|
if (dst_remaining < 2 * gnu_debugdata_size_) {
|
|
|
|
dst->Resize(dst->Size() + 2 * gnu_debugdata_size_);
|
|
|
|
dst_remaining += 2 * gnu_debugdata_size_;
|
|
|
|
}
|
|
|
|
return_val = XzUnpacker_Code(&state, dst->GetPtr(dst_offset), &dst_remaining, &src[src_offset],
|
2018-05-04 21:56:04 +02:00
|
|
|
&src_remaining, true, CODER_FINISH_ANY, &status);
|
2017-06-28 23:51:54 +02:00
|
|
|
src_offset += src_remaining;
|
|
|
|
dst_offset += dst_remaining;
|
|
|
|
} while (return_val == SZ_OK && status == CODER_STATUS_NOT_FINISHED);
|
|
|
|
XzUnpacker_Free(&state);
|
|
|
|
if (return_val != SZ_OK || !XzUnpacker_IsStreamWasFinished(&state)) {
|
|
|
|
gnu_debugdata_offset_ = 0;
|
|
|
|
gnu_debugdata_size_ = static_cast<uint64_t>(-1);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shrink back down to the exact size.
|
|
|
|
dst->Resize(dst_offset);
|
|
|
|
|
|
|
|
return dst.release();
|
|
|
|
}
|
|
|
|
|
2017-06-13 04:14:20 +02:00
|
|
|
template <typename AddressType>
|
2018-06-06 23:47:31 +02:00
|
|
|
void ElfInterface::InitHeadersWithTemplate(uint64_t load_bias) {
|
2017-11-03 22:50:27 +01:00
|
|
|
if (eh_frame_hdr_offset_ != 0) {
|
|
|
|
eh_frame_.reset(new DwarfEhFrameWithHdr<AddressType>(memory_));
|
2018-06-06 23:47:31 +02:00
|
|
|
if (!eh_frame_->Init(eh_frame_hdr_offset_, eh_frame_hdr_size_, load_bias)) {
|
2017-11-03 22:50:27 +01:00
|
|
|
eh_frame_.reset(nullptr);
|
|
|
|
}
|
2018-01-24 17:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (eh_frame_.get() == nullptr && eh_frame_offset_ != 0) {
|
|
|
|
// If there is an eh_frame section without an eh_frame_hdr section,
|
|
|
|
// or using the frame hdr object failed to init.
|
2017-06-13 04:14:20 +02:00
|
|
|
eh_frame_.reset(new DwarfEhFrame<AddressType>(memory_));
|
2018-06-06 23:47:31 +02:00
|
|
|
if (!eh_frame_->Init(eh_frame_offset_, eh_frame_size_, load_bias)) {
|
2017-06-13 04:14:20 +02:00
|
|
|
eh_frame_.reset(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 17:52:47 +01:00
|
|
|
if (eh_frame_.get() == nullptr) {
|
|
|
|
eh_frame_hdr_offset_ = 0;
|
|
|
|
eh_frame_hdr_size_ = static_cast<uint64_t>(-1);
|
|
|
|
eh_frame_offset_ = 0;
|
|
|
|
eh_frame_size_ = static_cast<uint64_t>(-1);
|
|
|
|
}
|
|
|
|
|
2017-06-13 04:14:20 +02:00
|
|
|
if (debug_frame_offset_ != 0) {
|
|
|
|
debug_frame_.reset(new DwarfDebugFrame<AddressType>(memory_));
|
2018-06-06 23:47:31 +02:00
|
|
|
if (!debug_frame_->Init(debug_frame_offset_, debug_frame_size_, load_bias)) {
|
2017-06-13 04:14:20 +02:00
|
|
|
debug_frame_.reset(nullptr);
|
|
|
|
debug_frame_offset_ = 0;
|
|
|
|
debug_frame_size_ = static_cast<uint64_t>(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 00:44:40 +01:00
|
|
|
template <typename EhdrType, typename PhdrType, typename ShdrType>
|
2017-10-20 01:08:58 +02:00
|
|
|
bool ElfInterface::ReadAllHeaders(uint64_t* load_bias) {
|
2017-02-02 00:44:40 +01:00
|
|
|
EhdrType ehdr;
|
2017-10-18 20:44:51 +02:00
|
|
|
if (!memory_->ReadFully(0, &ehdr, sizeof(ehdr))) {
|
2018-01-24 02:52:23 +01:00
|
|
|
last_error_.code = ERROR_MEMORY_INVALID;
|
|
|
|
last_error_.address = 0;
|
2017-02-02 00:44:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-01 22:10:46 +02:00
|
|
|
// If we have enough information that this is an elf file, then allow
|
|
|
|
// malformed program and section headers.
|
|
|
|
ReadProgramHeaders<EhdrType, PhdrType>(ehdr, load_bias);
|
|
|
|
ReadSectionHeaders<EhdrType, ShdrType>(ehdr);
|
2017-06-20 22:54:08 +02:00
|
|
|
return true;
|
2017-02-02 00:44:40 +01:00
|
|
|
}
|
|
|
|
|
2017-12-02 06:37:37 +01:00
|
|
|
template <typename EhdrType, typename PhdrType>
|
|
|
|
uint64_t ElfInterface::GetLoadBias(Memory* memory) {
|
|
|
|
EhdrType ehdr;
|
|
|
|
if (!memory->Read(0, &ehdr, sizeof(ehdr))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t offset = ehdr.e_phoff;
|
|
|
|
for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
|
|
|
|
PhdrType phdr;
|
|
|
|
if (!memory->Read(offset, &phdr, sizeof(phdr))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (phdr.p_type == PT_LOAD && phdr.p_offset == 0) {
|
|
|
|
return phdr.p_vaddr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-02 00:44:40 +01:00
|
|
|
template <typename EhdrType, typename PhdrType>
|
2018-08-01 22:10:46 +02:00
|
|
|
void ElfInterface::ReadProgramHeaders(const EhdrType& ehdr, uint64_t* load_bias) {
|
2017-02-02 00:44:40 +01:00
|
|
|
uint64_t offset = ehdr.e_phoff;
|
|
|
|
for (size_t i = 0; i < ehdr.e_phnum; i++, offset += ehdr.e_phentsize) {
|
|
|
|
PhdrType phdr;
|
2018-06-30 01:30:55 +02:00
|
|
|
if (!memory_->ReadFully(offset, &phdr, sizeof(phdr))) {
|
2018-08-01 22:10:46 +02:00
|
|
|
return;
|
2017-02-02 00:44:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (phdr.p_type) {
|
|
|
|
case PT_LOAD:
|
|
|
|
{
|
|
|
|
if ((phdr.p_flags & PF_X) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
pt_loads_[phdr.p_offset] = LoadInfo{phdr.p_offset, phdr.p_vaddr,
|
|
|
|
static_cast<size_t>(phdr.p_memsz)};
|
|
|
|
if (phdr.p_offset == 0) {
|
2017-10-20 01:08:58 +02:00
|
|
|
*load_bias = phdr.p_vaddr;
|
2017-02-02 00:44:40 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PT_GNU_EH_FRAME:
|
2017-11-03 22:50:27 +01:00
|
|
|
// This is really the pointer to the .eh_frame_hdr section.
|
|
|
|
eh_frame_hdr_offset_ = phdr.p_offset;
|
|
|
|
eh_frame_hdr_size_ = phdr.p_memsz;
|
2017-02-02 00:44:40 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PT_DYNAMIC:
|
|
|
|
dynamic_offset_ = phdr.p_offset;
|
2017-12-21 03:49:01 +01:00
|
|
|
dynamic_vaddr_ = phdr.p_vaddr;
|
2017-02-02 00:44:40 +01:00
|
|
|
dynamic_size_ = phdr.p_memsz;
|
|
|
|
break;
|
2018-06-30 01:30:55 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
HandleUnknownType(phdr.p_type, phdr.p_offset, phdr.p_filesz);
|
|
|
|
break;
|
2017-02-02 00:44:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-23 17:56:17 +01:00
|
|
|
template <typename NhdrType>
|
|
|
|
bool ElfInterface::ReadBuildID(std::string* build_id) {
|
|
|
|
// Ensure there is no overflow in any of the calulations below.
|
|
|
|
uint64_t tmp;
|
|
|
|
if (__builtin_add_overflow(gnu_build_id_offset_, gnu_build_id_size_, &tmp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t offset = 0;
|
|
|
|
while (offset < gnu_build_id_size_) {
|
|
|
|
if (gnu_build_id_size_ - offset < sizeof(NhdrType)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
NhdrType hdr;
|
|
|
|
if (!memory_->ReadFully(gnu_build_id_offset_ + offset, &hdr, sizeof(hdr))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
offset += sizeof(hdr);
|
|
|
|
|
|
|
|
if (gnu_build_id_size_ - offset < hdr.n_namesz) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (hdr.n_namesz > 0) {
|
|
|
|
std::string name(hdr.n_namesz, '\0');
|
|
|
|
if (!memory_->ReadFully(gnu_build_id_offset_ + offset, &(name[0]), hdr.n_namesz)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trim trailing \0 as GNU is stored as a C string in the ELF file.
|
|
|
|
if (name.back() == '\0')
|
|
|
|
name.resize(name.size() - 1);
|
|
|
|
|
|
|
|
// Align hdr.n_namesz to next power multiple of 4. See man 5 elf.
|
|
|
|
offset += (hdr.n_namesz + 3) & ~3;
|
|
|
|
|
|
|
|
if (name == "GNU" && hdr.n_type == NT_GNU_BUILD_ID) {
|
|
|
|
if (gnu_build_id_size_ - offset < hdr.n_descsz) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
build_id->resize(hdr.n_descsz);
|
|
|
|
return memory_->ReadFully(gnu_build_id_offset_ + offset, &(*build_id)[0],
|
|
|
|
hdr.n_descsz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Align hdr.n_descsz to next power multiple of 4. See man 5 elf.
|
|
|
|
offset += (hdr.n_descsz + 3) & ~3;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-02 00:44:40 +01:00
|
|
|
template <typename EhdrType, typename ShdrType>
|
2018-08-01 22:10:46 +02:00
|
|
|
void ElfInterface::ReadSectionHeaders(const EhdrType& ehdr) {
|
2017-02-02 00:44:40 +01:00
|
|
|
uint64_t offset = ehdr.e_shoff;
|
|
|
|
uint64_t sec_offset = 0;
|
|
|
|
uint64_t sec_size = 0;
|
|
|
|
|
|
|
|
// Get the location of the section header names.
|
|
|
|
// If something is malformed in the header table data, we aren't going
|
|
|
|
// to terminate, we'll simply ignore this part.
|
|
|
|
ShdrType shdr;
|
|
|
|
if (ehdr.e_shstrndx < ehdr.e_shnum) {
|
|
|
|
uint64_t sh_offset = offset + ehdr.e_shstrndx * ehdr.e_shentsize;
|
2018-06-30 01:30:55 +02:00
|
|
|
if (memory_->ReadFully(sh_offset, &shdr, sizeof(shdr))) {
|
2017-02-02 00:44:40 +01:00
|
|
|
sec_offset = shdr.sh_offset;
|
|
|
|
sec_size = shdr.sh_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip the first header, it's always going to be NULL.
|
2017-06-20 22:54:08 +02:00
|
|
|
offset += ehdr.e_shentsize;
|
2017-02-02 00:44:40 +01:00
|
|
|
for (size_t i = 1; i < ehdr.e_shnum; i++, offset += ehdr.e_shentsize) {
|
2018-02-16 02:36:33 +01:00
|
|
|
if (!memory_->Read(offset, &shdr, sizeof(shdr))) {
|
2018-08-01 22:10:46 +02:00
|
|
|
return;
|
2017-02-02 00:44:40 +01:00
|
|
|
}
|
|
|
|
|
2017-06-20 22:54:08 +02:00
|
|
|
if (shdr.sh_type == SHT_SYMTAB || shdr.sh_type == SHT_DYNSYM) {
|
|
|
|
// Need to go get the information about the section that contains
|
|
|
|
// the string terminated names.
|
|
|
|
ShdrType str_shdr;
|
|
|
|
if (shdr.sh_link >= ehdr.e_shnum) {
|
2018-08-01 22:10:46 +02:00
|
|
|
continue;
|
2017-06-20 22:54:08 +02:00
|
|
|
}
|
|
|
|
uint64_t str_offset = ehdr.e_shoff + shdr.sh_link * ehdr.e_shentsize;
|
2018-02-16 02:36:33 +01:00
|
|
|
if (!memory_->Read(str_offset, &str_shdr, sizeof(str_shdr))) {
|
2018-08-01 22:10:46 +02:00
|
|
|
continue;
|
2017-06-20 22:54:08 +02:00
|
|
|
}
|
|
|
|
if (str_shdr.sh_type != SHT_STRTAB) {
|
2018-08-01 22:10:46 +02:00
|
|
|
continue;
|
2017-06-20 22:54:08 +02:00
|
|
|
}
|
|
|
|
symbols_.push_back(new Symbols(shdr.sh_offset, shdr.sh_size, shdr.sh_entsize,
|
|
|
|
str_shdr.sh_offset, str_shdr.sh_size));
|
|
|
|
} else if (shdr.sh_type == SHT_PROGBITS && sec_size != 0) {
|
2017-02-02 00:44:40 +01:00
|
|
|
// Look for the .debug_frame and .gnu_debugdata.
|
|
|
|
if (shdr.sh_name < sec_size) {
|
|
|
|
std::string name;
|
|
|
|
if (memory_->ReadString(sec_offset + shdr.sh_name, &name)) {
|
2017-06-20 22:54:08 +02:00
|
|
|
uint64_t* offset_ptr = nullptr;
|
|
|
|
uint64_t* size_ptr = nullptr;
|
2017-02-02 00:44:40 +01:00
|
|
|
if (name == ".debug_frame") {
|
2017-06-20 22:54:08 +02:00
|
|
|
offset_ptr = &debug_frame_offset_;
|
|
|
|
size_ptr = &debug_frame_size_;
|
2017-02-02 00:44:40 +01:00
|
|
|
} else if (name == ".gnu_debugdata") {
|
2017-06-20 22:54:08 +02:00
|
|
|
offset_ptr = &gnu_debugdata_offset_;
|
|
|
|
size_ptr = &gnu_debugdata_size_;
|
2017-11-03 22:50:27 +01:00
|
|
|
} else if (name == ".eh_frame") {
|
|
|
|
offset_ptr = &eh_frame_offset_;
|
|
|
|
size_ptr = &eh_frame_size_;
|
|
|
|
} else if (eh_frame_hdr_offset_ == 0 && name == ".eh_frame_hdr") {
|
|
|
|
offset_ptr = &eh_frame_hdr_offset_;
|
|
|
|
size_ptr = &eh_frame_hdr_size_;
|
2017-06-20 22:54:08 +02:00
|
|
|
}
|
2018-02-16 02:36:33 +01:00
|
|
|
if (offset_ptr != nullptr) {
|
2017-06-20 22:54:08 +02:00
|
|
|
*offset_ptr = shdr.sh_offset;
|
|
|
|
*size_ptr = shdr.sh_size;
|
2017-02-02 00:44:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-16 02:36:33 +01:00
|
|
|
} else if (shdr.sh_type == SHT_STRTAB) {
|
|
|
|
// In order to read soname, keep track of address to offset mapping.
|
|
|
|
strtabs_.push_back(std::make_pair<uint64_t, uint64_t>(static_cast<uint64_t>(shdr.sh_addr),
|
|
|
|
static_cast<uint64_t>(shdr.sh_offset)));
|
2018-11-23 17:56:17 +01:00
|
|
|
} else if (shdr.sh_type == SHT_NOTE) {
|
|
|
|
if (shdr.sh_name < sec_size) {
|
|
|
|
std::string name;
|
|
|
|
if (memory_->ReadString(sec_offset + shdr.sh_name, &name) &&
|
|
|
|
name == ".note.gnu.build-id") {
|
|
|
|
gnu_build_id_offset_ = shdr.sh_offset;
|
|
|
|
gnu_build_id_size_ = shdr.sh_size;
|
|
|
|
}
|
|
|
|
}
|
2017-02-02 00:44:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename DynType>
|
|
|
|
bool ElfInterface::GetSonameWithTemplate(std::string* soname) {
|
|
|
|
if (soname_type_ == SONAME_INVALID) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (soname_type_ == SONAME_VALID) {
|
|
|
|
*soname = soname_;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
soname_type_ = SONAME_INVALID;
|
|
|
|
|
|
|
|
uint64_t soname_offset = 0;
|
2018-02-16 02:36:33 +01:00
|
|
|
uint64_t strtab_addr = 0;
|
2017-02-02 00:44:40 +01:00
|
|
|
uint64_t strtab_size = 0;
|
|
|
|
|
|
|
|
// Find the soname location from the dynamic headers section.
|
|
|
|
DynType dyn;
|
|
|
|
uint64_t offset = dynamic_offset_;
|
|
|
|
uint64_t max_offset = offset + dynamic_size_;
|
|
|
|
for (uint64_t offset = dynamic_offset_; offset < max_offset; offset += sizeof(DynType)) {
|
2017-10-18 20:44:51 +02:00
|
|
|
if (!memory_->ReadFully(offset, &dyn, sizeof(dyn))) {
|
2018-01-24 02:52:23 +01:00
|
|
|
last_error_.code = ERROR_MEMORY_INVALID;
|
|
|
|
last_error_.address = offset;
|
2017-02-02 00:44:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dyn.d_tag == DT_STRTAB) {
|
2018-02-16 02:36:33 +01:00
|
|
|
strtab_addr = dyn.d_un.d_ptr;
|
2017-02-02 00:44:40 +01:00
|
|
|
} else if (dyn.d_tag == DT_STRSZ) {
|
|
|
|
strtab_size = dyn.d_un.d_val;
|
|
|
|
} else if (dyn.d_tag == DT_SONAME) {
|
|
|
|
soname_offset = dyn.d_un.d_val;
|
|
|
|
} else if (dyn.d_tag == DT_NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-16 02:36:33 +01:00
|
|
|
// Need to map the strtab address to the real offset.
|
|
|
|
for (const auto& entry : strtabs_) {
|
|
|
|
if (entry.first == strtab_addr) {
|
|
|
|
soname_offset = entry.second + soname_offset;
|
|
|
|
if (soname_offset >= entry.second + strtab_size) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!memory_->ReadString(soname_offset, &soname_)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
soname_type_ = SONAME_VALID;
|
|
|
|
*soname = soname_;
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-02 00:44:40 +01:00
|
|
|
}
|
2018-02-16 02:36:33 +01:00
|
|
|
return false;
|
2017-02-02 00:44:40 +01:00
|
|
|
}
|
|
|
|
|
2017-06-20 22:54:08 +02:00
|
|
|
template <typename SymType>
|
2018-06-06 23:47:31 +02:00
|
|
|
bool ElfInterface::GetFunctionNameWithTemplate(uint64_t addr, std::string* name,
|
2017-06-20 22:54:08 +02:00
|
|
|
uint64_t* func_offset) {
|
|
|
|
if (symbols_.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto symbol : symbols_) {
|
2018-06-06 23:47:31 +02:00
|
|
|
if (symbol->GetName<SymType>(addr, memory_, name, func_offset)) {
|
2017-06-20 22:54:08 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-21 03:49:01 +01:00
|
|
|
template <typename SymType>
|
|
|
|
bool ElfInterface::GetGlobalVariableWithTemplate(const std::string& name, uint64_t* memory_address) {
|
|
|
|
if (symbols_.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto symbol : symbols_) {
|
|
|
|
if (symbol->GetGlobal<SymType>(memory_, name, memory_address)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-06 23:47:31 +02:00
|
|
|
bool ElfInterface::Step(uint64_t pc, Regs* regs, Memory* process_memory, bool* finished) {
|
2018-01-24 02:52:23 +01:00
|
|
|
last_error_.code = ERROR_NONE;
|
|
|
|
last_error_.address = 0;
|
|
|
|
|
2018-01-24 17:52:47 +01:00
|
|
|
// Try the debug_frame first since it contains the most specific unwind
|
|
|
|
// information.
|
|
|
|
DwarfSection* debug_frame = debug_frame_.get();
|
2018-06-06 23:47:31 +02:00
|
|
|
if (debug_frame != nullptr && debug_frame->Step(pc, regs, process_memory, finished)) {
|
2017-06-20 22:54:08 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-24 17:52:47 +01:00
|
|
|
// Try the eh_frame next.
|
|
|
|
DwarfSection* eh_frame = eh_frame_.get();
|
2018-06-06 23:47:31 +02:00
|
|
|
if (eh_frame != nullptr && eh_frame->Step(pc, regs, process_memory, finished)) {
|
2017-12-15 20:17:45 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gnu_debugdata_interface_ != nullptr &&
|
2018-06-06 23:47:31 +02:00
|
|
|
gnu_debugdata_interface_->Step(pc, regs, process_memory, finished)) {
|
2017-06-20 22:54:08 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-01-24 02:52:23 +01:00
|
|
|
|
|
|
|
// Set the error code based on the first error encountered.
|
|
|
|
DwarfSection* section = nullptr;
|
|
|
|
if (debug_frame_ != nullptr) {
|
|
|
|
section = debug_frame_.get();
|
|
|
|
} else if (eh_frame_ != nullptr) {
|
|
|
|
section = eh_frame_.get();
|
|
|
|
} else if (gnu_debugdata_interface_ != nullptr) {
|
|
|
|
last_error_ = gnu_debugdata_interface_->last_error();
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the DWARF ERROR to an external error.
|
|
|
|
DwarfErrorCode code = section->LastErrorCode();
|
|
|
|
switch (code) {
|
|
|
|
case DWARF_ERROR_NONE:
|
|
|
|
last_error_.code = ERROR_NONE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DWARF_ERROR_MEMORY_INVALID:
|
|
|
|
last_error_.code = ERROR_MEMORY_INVALID;
|
|
|
|
last_error_.address = section->LastErrorAddress();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DWARF_ERROR_ILLEGAL_VALUE:
|
|
|
|
case DWARF_ERROR_ILLEGAL_STATE:
|
|
|
|
case DWARF_ERROR_STACK_INDEX_NOT_VALID:
|
|
|
|
case DWARF_ERROR_TOO_MANY_ITERATIONS:
|
|
|
|
case DWARF_ERROR_CFA_NOT_DEFINED:
|
|
|
|
case DWARF_ERROR_NO_FDES:
|
|
|
|
last_error_.code = ERROR_UNWIND_INFO;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DWARF_ERROR_NOT_IMPLEMENTED:
|
|
|
|
case DWARF_ERROR_UNSUPPORTED_VERSION:
|
|
|
|
last_error_.code = ERROR_UNSUPPORTED;
|
|
|
|
break;
|
|
|
|
}
|
2017-02-02 00:44:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-30 22:15:19 +02:00
|
|
|
// This is an estimation of the size of the elf file using the location
|
|
|
|
// of the section headers and size. This assumes that the section headers
|
|
|
|
// are at the end of the elf file. If the elf has a load bias, the size
|
|
|
|
// will be too large, but this is acceptable.
|
|
|
|
template <typename EhdrType>
|
|
|
|
void ElfInterface::GetMaxSizeWithTemplate(Memory* memory, uint64_t* size) {
|
|
|
|
EhdrType ehdr;
|
2017-10-18 20:44:51 +02:00
|
|
|
if (!memory->ReadFully(0, &ehdr, sizeof(ehdr))) {
|
2017-08-30 22:15:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ehdr.e_shnum == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*size = ehdr.e_shoff + ehdr.e_shentsize * ehdr.e_shnum;
|
|
|
|
}
|
|
|
|
|
2017-02-02 00:44:40 +01:00
|
|
|
// Instantiate all of the needed template functions.
|
2018-06-06 23:47:31 +02:00
|
|
|
template void ElfInterface::InitHeadersWithTemplate<uint32_t>(uint64_t);
|
|
|
|
template void ElfInterface::InitHeadersWithTemplate<uint64_t>(uint64_t);
|
2017-06-13 04:14:20 +02:00
|
|
|
|
2017-10-20 01:08:58 +02:00
|
|
|
template bool ElfInterface::ReadAllHeaders<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr>(uint64_t*);
|
|
|
|
template bool ElfInterface::ReadAllHeaders<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr>(uint64_t*);
|
2017-02-02 00:44:40 +01:00
|
|
|
|
2018-08-01 22:10:46 +02:00
|
|
|
template void ElfInterface::ReadProgramHeaders<Elf32_Ehdr, Elf32_Phdr>(const Elf32_Ehdr&,
|
|
|
|
uint64_t*);
|
|
|
|
template void ElfInterface::ReadProgramHeaders<Elf64_Ehdr, Elf64_Phdr>(const Elf64_Ehdr&,
|
|
|
|
uint64_t*);
|
2017-02-02 00:44:40 +01:00
|
|
|
|
2018-08-01 22:10:46 +02:00
|
|
|
template void ElfInterface::ReadSectionHeaders<Elf32_Ehdr, Elf32_Shdr>(const Elf32_Ehdr&);
|
|
|
|
template void ElfInterface::ReadSectionHeaders<Elf64_Ehdr, Elf64_Shdr>(const Elf64_Ehdr&);
|
2017-02-02 00:44:40 +01:00
|
|
|
|
2018-11-23 17:56:17 +01:00
|
|
|
template bool ElfInterface::ReadBuildID<Elf32_Nhdr>(std::string*);
|
|
|
|
template bool ElfInterface::ReadBuildID<Elf64_Nhdr>(std::string*);
|
|
|
|
|
2017-02-02 00:44:40 +01:00
|
|
|
template bool ElfInterface::GetSonameWithTemplate<Elf32_Dyn>(std::string*);
|
|
|
|
template bool ElfInterface::GetSonameWithTemplate<Elf64_Dyn>(std::string*);
|
2017-06-20 22:54:08 +02:00
|
|
|
|
2018-06-06 23:47:31 +02:00
|
|
|
template bool ElfInterface::GetFunctionNameWithTemplate<Elf32_Sym>(uint64_t, std::string*,
|
2017-06-20 22:54:08 +02:00
|
|
|
uint64_t*);
|
2018-06-06 23:47:31 +02:00
|
|
|
template bool ElfInterface::GetFunctionNameWithTemplate<Elf64_Sym>(uint64_t, std::string*,
|
2017-06-20 22:54:08 +02:00
|
|
|
uint64_t*);
|
2017-07-14 19:37:19 +02:00
|
|
|
|
2017-12-21 03:49:01 +01:00
|
|
|
template bool ElfInterface::GetGlobalVariableWithTemplate<Elf32_Sym>(const std::string&, uint64_t*);
|
|
|
|
template bool ElfInterface::GetGlobalVariableWithTemplate<Elf64_Sym>(const std::string&, uint64_t*);
|
|
|
|
|
2017-08-30 22:15:19 +02:00
|
|
|
template void ElfInterface::GetMaxSizeWithTemplate<Elf32_Ehdr>(Memory*, uint64_t*);
|
|
|
|
template void ElfInterface::GetMaxSizeWithTemplate<Elf64_Ehdr>(Memory*, uint64_t*);
|
|
|
|
|
2017-12-02 06:37:37 +01:00
|
|
|
template uint64_t ElfInterface::GetLoadBias<Elf32_Ehdr, Elf32_Phdr>(Memory*);
|
|
|
|
template uint64_t ElfInterface::GetLoadBias<Elf64_Ehdr, Elf64_Phdr>(Memory*);
|
|
|
|
|
2017-07-14 19:37:19 +02:00
|
|
|
} // namespace unwindstack
|