2017-06-13 04:14:20 +02: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 <stdint.h>
|
|
|
|
|
2018-01-24 02:52:23 +01:00
|
|
|
#include <unwindstack/DwarfError.h>
|
2017-07-14 19:37:19 +02:00
|
|
|
#include <unwindstack/DwarfStructs.h>
|
|
|
|
#include <unwindstack/Memory.h>
|
|
|
|
|
2017-06-29 03:56:52 +02:00
|
|
|
#include "Check.h"
|
2017-11-03 22:50:27 +01:00
|
|
|
#include "DwarfEhFrameWithHdr.h"
|
2018-06-06 23:47:31 +02:00
|
|
|
#include "DwarfEncoding.h"
|
2017-07-14 19:37:19 +02:00
|
|
|
|
|
|
|
namespace unwindstack {
|
2017-06-13 04:14:20 +02:00
|
|
|
|
2018-06-06 23:47:31 +02:00
|
|
|
static inline bool IsEncodingRelative(uint8_t encoding) {
|
|
|
|
encoding >>= 4;
|
|
|
|
return encoding > 0 && encoding <= DW_EH_PE_funcrel;
|
|
|
|
}
|
|
|
|
|
2017-06-13 04:14:20 +02:00
|
|
|
template <typename AddressType>
|
2019-10-29 18:21:11 +01:00
|
|
|
bool DwarfEhFrameWithHdr<AddressType>::EhFrameInit(uint64_t offset, uint64_t size,
|
|
|
|
int64_t section_bias) {
|
|
|
|
return DwarfSectionImpl<AddressType>::Init(offset, size, section_bias);
|
|
|
|
}
|
2017-06-13 04:14:20 +02:00
|
|
|
|
2019-10-29 18:21:11 +01:00
|
|
|
template <typename AddressType>
|
|
|
|
bool DwarfEhFrameWithHdr<AddressType>::Init(uint64_t offset, uint64_t, int64_t section_bias) {
|
2017-06-13 04:14:20 +02:00
|
|
|
memory_.clear_func_offset();
|
|
|
|
memory_.clear_text_offset();
|
|
|
|
memory_.set_data_offset(offset);
|
|
|
|
memory_.set_cur_offset(offset);
|
2019-10-29 18:21:11 +01:00
|
|
|
|
|
|
|
hdr_section_bias_ = section_bias;
|
2017-06-13 04:14:20 +02:00
|
|
|
|
|
|
|
// Read the first four bytes all at once.
|
2018-06-06 23:47:31 +02:00
|
|
|
uint8_t data[4];
|
2017-06-13 04:14:20 +02:00
|
|
|
if (!memory_.ReadBytes(data, 4)) {
|
2018-01-24 02:52:23 +01:00
|
|
|
last_error_.code = DWARF_ERROR_MEMORY_INVALID;
|
|
|
|
last_error_.address = memory_.cur_offset();
|
2017-06-13 04:14:20 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
version_ = data[0];
|
|
|
|
if (version_ != 1) {
|
|
|
|
// Unknown version.
|
2018-01-24 02:52:23 +01:00
|
|
|
last_error_.code = DWARF_ERROR_UNSUPPORTED_VERSION;
|
2017-06-13 04:14:20 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:21:11 +01:00
|
|
|
uint8_t ptr_encoding = data[1];
|
2017-06-13 04:14:20 +02:00
|
|
|
uint8_t fde_count_encoding = data[2];
|
|
|
|
table_encoding_ = data[3];
|
|
|
|
table_entry_size_ = memory_.template GetEncodedSize<AddressType>(table_encoding_);
|
|
|
|
|
2019-03-29 20:34:58 +01:00
|
|
|
// If we can't perform a binary search on the entries, it's not worth
|
|
|
|
// using this object. The calling code will fall back to the DwarfEhFrame
|
|
|
|
// object in this case.
|
|
|
|
if (table_entry_size_ == 0) {
|
|
|
|
last_error_.code = DWARF_ERROR_ILLEGAL_VALUE;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-13 04:14:20 +02:00
|
|
|
memory_.set_pc_offset(memory_.cur_offset());
|
2019-10-29 18:21:11 +01:00
|
|
|
uint64_t ptr_offset;
|
|
|
|
if (!memory_.template ReadEncodedValue<AddressType>(ptr_encoding, &ptr_offset)) {
|
2018-01-24 02:52:23 +01:00
|
|
|
last_error_.code = DWARF_ERROR_MEMORY_INVALID;
|
|
|
|
last_error_.address = memory_.cur_offset();
|
2017-06-13 04:14:20 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memory_.set_pc_offset(memory_.cur_offset());
|
|
|
|
if (!memory_.template ReadEncodedValue<AddressType>(fde_count_encoding, &fde_count_)) {
|
2018-01-24 02:52:23 +01:00
|
|
|
last_error_.code = DWARF_ERROR_MEMORY_INVALID;
|
|
|
|
last_error_.address = memory_.cur_offset();
|
2017-06-13 04:14:20 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-24 17:52:47 +01:00
|
|
|
if (fde_count_ == 0) {
|
2018-01-24 02:52:23 +01:00
|
|
|
last_error_.code = DWARF_ERROR_NO_FDES;
|
2018-01-24 17:52:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:21:11 +01:00
|
|
|
hdr_entries_offset_ = memory_.cur_offset();
|
|
|
|
hdr_entries_data_offset_ = offset;
|
2017-06-13 04:14:20 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename AddressType>
|
2018-06-21 19:44:02 +02:00
|
|
|
const DwarfFde* DwarfEhFrameWithHdr<AddressType>::GetFdeFromPc(uint64_t pc) {
|
|
|
|
uint64_t fde_offset;
|
|
|
|
if (!GetFdeOffsetFromPc(pc, &fde_offset)) {
|
2017-06-13 04:14:20 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-06-21 19:44:02 +02:00
|
|
|
const DwarfFde* fde = this->GetFdeFromOffset(fde_offset);
|
|
|
|
if (fde == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:21:11 +01:00
|
|
|
// There is a possibility that this entry points to a zero length FDE
|
|
|
|
// due to a bug. If this happens, try and find the non-zero length FDE
|
|
|
|
// from eh_frame directly. See b/142483624.
|
|
|
|
if (fde->pc_start == fde->pc_end) {
|
|
|
|
fde = DwarfSectionImpl<AddressType>::GetFdeFromPc(pc);
|
|
|
|
if (fde == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 19:44:02 +02:00
|
|
|
// Guaranteed pc >= pc_start, need to check pc in the fde range.
|
|
|
|
if (pc < fde->pc_end) {
|
|
|
|
return fde;
|
|
|
|
}
|
|
|
|
last_error_.code = DWARF_ERROR_ILLEGAL_STATE;
|
|
|
|
return nullptr;
|
2017-06-13 04:14:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename AddressType>
|
2017-11-03 22:50:27 +01:00
|
|
|
const typename DwarfEhFrameWithHdr<AddressType>::FdeInfo*
|
|
|
|
DwarfEhFrameWithHdr<AddressType>::GetFdeInfoFromIndex(size_t index) {
|
2017-06-13 04:14:20 +02:00
|
|
|
auto entry = fde_info_.find(index);
|
|
|
|
if (entry != fde_info_.end()) {
|
|
|
|
return &fde_info_[index];
|
|
|
|
}
|
|
|
|
FdeInfo* info = &fde_info_[index];
|
|
|
|
|
2019-10-29 18:21:11 +01:00
|
|
|
memory_.set_data_offset(hdr_entries_data_offset_);
|
|
|
|
memory_.set_cur_offset(hdr_entries_offset_ + 2 * index * table_entry_size_);
|
2018-06-06 23:47:31 +02:00
|
|
|
memory_.set_pc_offset(0);
|
2017-06-13 04:14:20 +02:00
|
|
|
uint64_t value;
|
|
|
|
if (!memory_.template ReadEncodedValue<AddressType>(table_encoding_, &value) ||
|
|
|
|
!memory_.template ReadEncodedValue<AddressType>(table_encoding_, &info->offset)) {
|
2018-01-24 02:52:23 +01:00
|
|
|
last_error_.code = DWARF_ERROR_MEMORY_INVALID;
|
|
|
|
last_error_.address = memory_.cur_offset();
|
2017-06-13 04:14:20 +02:00
|
|
|
fde_info_.erase(index);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-06-06 23:47:31 +02:00
|
|
|
|
|
|
|
// Relative encodings require adding in the load bias.
|
|
|
|
if (IsEncodingRelative(table_encoding_)) {
|
2019-10-29 18:21:11 +01:00
|
|
|
value += hdr_section_bias_;
|
2018-06-06 23:47:31 +02:00
|
|
|
}
|
2018-02-10 00:57:39 +01:00
|
|
|
info->pc = value;
|
2017-06-13 04:14:20 +02:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename AddressType>
|
2019-03-29 20:34:58 +01:00
|
|
|
bool DwarfEhFrameWithHdr<AddressType>::GetFdeOffsetFromPc(uint64_t pc, uint64_t* fde_offset) {
|
|
|
|
if (fde_count_ == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-13 04:14:20 +02:00
|
|
|
|
|
|
|
size_t first = 0;
|
2019-03-29 20:34:58 +01:00
|
|
|
size_t last = fde_count_;
|
2017-06-13 04:14:20 +02:00
|
|
|
while (first < last) {
|
|
|
|
size_t current = (first + last) / 2;
|
|
|
|
const FdeInfo* info = GetFdeInfoFromIndex(current);
|
2017-11-08 20:01:18 +01:00
|
|
|
if (info == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-13 04:14:20 +02:00
|
|
|
if (pc == info->pc) {
|
|
|
|
*fde_offset = info->offset;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (pc < info->pc) {
|
|
|
|
last = current;
|
|
|
|
} else {
|
|
|
|
first = current + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (last != 0) {
|
|
|
|
const FdeInfo* info = GetFdeInfoFromIndex(last - 1);
|
2017-11-08 20:01:18 +01:00
|
|
|
if (info == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-13 04:14:20 +02:00
|
|
|
*fde_offset = info->offset;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-21 19:44:02 +02:00
|
|
|
template <typename AddressType>
|
|
|
|
void DwarfEhFrameWithHdr<AddressType>::GetFdes(std::vector<const DwarfFde*>* fdes) {
|
|
|
|
for (size_t i = 0; i < fde_count_; i++) {
|
|
|
|
const FdeInfo* info = GetFdeInfoFromIndex(i);
|
|
|
|
if (info == nullptr) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const DwarfFde* fde = this->GetFdeFromOffset(info->offset);
|
|
|
|
if (fde == nullptr) {
|
|
|
|
break;
|
|
|
|
}
|
2019-10-29 18:21:11 +01:00
|
|
|
|
|
|
|
// There is a possibility that this entry points to a zero length FDE
|
|
|
|
// due to a bug. If this happens, try and find the non-zero length FDE
|
|
|
|
// from eh_frame directly. See b/142483624.
|
|
|
|
if (fde->pc_start == fde->pc_end) {
|
|
|
|
const DwarfFde* fde_real = DwarfSectionImpl<AddressType>::GetFdeFromPc(fde->pc_start);
|
|
|
|
if (fde_real != nullptr) {
|
|
|
|
fde = fde_real;
|
|
|
|
}
|
|
|
|
}
|
2018-06-21 19:44:02 +02:00
|
|
|
fdes->push_back(fde);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-03 22:50:27 +01:00
|
|
|
// Explicitly instantiate DwarfEhFrameWithHdr
|
|
|
|
template class DwarfEhFrameWithHdr<uint32_t>;
|
|
|
|
template class DwarfEhFrameWithHdr<uint64_t>;
|
2017-07-14 19:37:19 +02:00
|
|
|
|
|
|
|
} // namespace unwindstack
|