Chromium Code Reviews| Index: src/eh-frame.cc |
| diff --git a/src/eh-frame.cc b/src/eh-frame.cc |
| index af85e0b8d5f7143beb3c5243513a69fef0f5f919..033fa8abae762bc689d6d8739897f5362e14d643 100644 |
| --- a/src/eh-frame.cc |
| +++ b/src/eh-frame.cc |
| @@ -3,21 +3,375 @@ |
| // found in the LICENSE file. |
| #include "src/eh-frame.h" |
| -#include "src/objects-inl.h" |
| -#include "src/objects.h" |
| + |
| +#include <iomanip> |
| +#include <ostream> |
| + |
| +#if V8_TARGET_ARCH_X64 |
| +#include "src/x64/eh-frame-x64.h" |
| +#elif V8_TARGET_ARCH_ARM |
| +#include "src/arm/eh-frame-arm.h" |
| +#elif V8_TARGET_ARCH_ARM64 |
| +#include "src/arm64/eh-frame-arm64.h" |
| +#else |
| + |
| +// Dummy placeholders |
| namespace v8 { |
| namespace internal { |
| -static const int DW_EH_PE_pcrel = 0x10; |
| -static const int DW_EH_PE_datarel = 0x30; |
| -static const int DW_EH_PE_udata4 = 0x03; |
| -static const int DW_EH_PE_sdata4 = 0x0b; |
| +static const Register kInitialBaseRegister = {Register::kCode_no_reg}; |
| +static const int kInitialBaseOffset = 0; |
| +static const int kDataAlignmentFactor = 1; |
| +static const byte kCIE[8] = {0}; |
| +#if ENABLE_DISASSEMBLER |
| +static const int kInitialStateOffsetInCIE = 0; |
| +#endif |
| + |
| +const char* DwarfRegisterCodeToString(int) { |
| + UNIMPLEMENTED(); |
| + return nullptr; |
| +} |
| + |
| +int RegisterToDwarfCode(Register) { |
| + UNIMPLEMENTED(); |
| + return -1; |
| +} |
| + |
| +} // namespace internal |
| +} // namespace v8 |
| + |
| +#endif |
| + |
| +namespace v8 { |
| +namespace internal { |
| + |
| +STATIC_CONST_MEMBER_DEFINITION const uint32_t EhFrameWriter::kInt32Placeholder; |
| + |
| +STATIC_CONST_MEMBER_DEFINITION const int EhFrameWriter::kCIESize = sizeof(kCIE); |
| +static_assert(EhFrameWriter::kCIESize % kPointerSize == 0, |
| + "CIE size must be a multiple of pointer size"); |
| + |
| +STATIC_CONST_MEMBER_DEFINITION const int EhFrameWriter::kFDEOffset = kCIESize; |
| +STATIC_CONST_MEMBER_DEFINITION const int |
| + EhFrameWriter::kProcedureAddressOffset = kFDEOffset + 2 * kInt32Size; |
|
rmcilroy
2016/06/30 15:23:10
I think it would be better if these were relative
Stefano Sanfilippo
2016/07/04 18:21:14
Done.
|
| +STATIC_CONST_MEMBER_DEFINITION const int EhFrameWriter::kProcedureSizeOffset = |
| + kFDEOffset + 3 * kInt32Size; |
| +#ifdef ENABLE_DISASSEMBLER |
| +STATIC_CONST_MEMBER_DEFINITION const int EhFrameWriter::kFDEDirectivesOffset = |
|
rmcilroy
2016/06/30 15:23:10
This is only used in DisassembleToStream, just def
Stefano Sanfilippo
2016/07/04 18:21:14
Done.
|
| + kFDEOffset + 4 * kInt32Size + 1; |
| +#endif |
| + |
| +const char* DwarfRegisterCodeToString(int code); |
| +int RegisterToDwarfCode(Register name); |
|
rmcilroy
2016/06/30 15:23:10
these should be methods in EhFrameWriter
Stefano Sanfilippo
2016/07/04 18:21:14
Done.
|
| + |
| +EhFrameWriter::EhFrameWriter() |
| + : last_pc_offset_(0), |
| +#if DEBUG |
| + eh_frame_finalised_(false), |
| +#endif |
| + base_register_(kInitialBaseRegister), |
| + base_offset_(kInitialBaseOffset) { |
| + WriteBytes(&kCIE[0], sizeof(kCIE)); |
| + WriteFDEHeader(); |
| +} |
| + |
| +void EhFrameWriter::WriteFDEHeader() { |
| + DCHECK_EQ(eh_frame_offset(), kFDEOffset); |
| + WriteInt32(kInt32Placeholder); // Placeholder for size of the FDE |
| + WriteInt32(kCIESize + kInt32Size); // Backwards offset to the CIE |
| + DCHECK_EQ(eh_frame_offset(), kProcedureAddressOffset); |
| + WriteInt32(kInt32Placeholder); // Placeholder for pointer to procedure |
| + DCHECK_EQ(eh_frame_offset(), kProcedureSizeOffset); |
| + WriteInt32(kInt32Placeholder); // Placeholder for size of the procedure |
| + WriteByte(0); // No augmentation data |
| +} |
| + |
| +void EhFrameWriter::AdvanceLocation(int pc_offset) { |
| + DCHECK_GE(pc_offset, last_pc_offset_); |
| + uint32_t delta = pc_offset - last_pc_offset_; |
| + |
| + if (delta <= kLocationMask) { |
| + WriteByte((kLocationTag << kLocationMaskSize) | (delta & kLocationMask)); |
| + } else if (delta <= kMaxUInt8) { |
| + WriteByte(kAdvanceLoc1); |
| + WriteByte(delta); |
| + } else if (delta <= kMaxUInt16) { |
| + WriteByte(kAdvanceLoc2); |
| + WriteInt16(delta); |
| + } else { |
| + WriteByte(kAdvanceLoc4); |
| + WriteInt32(delta); |
| + } |
| + |
| + last_pc_offset_ = pc_offset; |
| +} |
| + |
| +void EhFrameWriter::DefineBaseAddressOffset(int base_offset) { |
| + DCHECK_GE(base_offset, 0); |
| + WriteByte(kDefCFAOffset); |
| + WriteULEB128(base_offset); |
| + base_offset_ = base_offset; |
| +} |
| + |
| +void EhFrameWriter::DefineBaseAddressRegister(Register name) { |
| + int code = RegisterToDwarfCode(name); |
| + WriteByte(kDefCFARegister); |
| + WriteULEB128(code); |
| + base_register_ = name; |
| +} |
| + |
| +void EhFrameWriter::DefineBaseAddressRegisterOffset(Register name, |
| + int base_offset) { |
| + int code = RegisterToDwarfCode(name); |
| + WriteByte(kDefCFA); |
| + WriteULEB128(code); |
| + WriteULEB128(base_offset); |
| + base_offset_ = base_offset; |
| + base_register_ = name; |
| +} |
| + |
| +void EhFrameWriter::SaveRegisterToStack(Register name, int offset) { |
| + int code = RegisterToDwarfCode(name); |
| + DCHECK_GE(code, 0); |
| + DCHECK_LE(code, kSavedRegisterMask); |
| + DCHECK_EQ(offset % kDataAlignmentFactor, 0); |
| + WriteByte((kSavedRegisterTag << kSavedRegisterMaskSize) | |
| + (code & kSavedRegisterMask)); |
| + WriteULEB128(offset / std::abs(kDataAlignmentFactor)); |
| +} |
| + |
| +void EhFrameWriter::RegisterIsValid(Register name) { |
| + int code = RegisterToDwarfCode(name); |
| + DCHECK_GE(code, 0); |
| + WriteByte(kSameValue); |
| + WriteULEB128(code); |
| +} |
| + |
| +void EhFrameWriter::Finish(int code_size) { |
| + DCHECK_GE(eh_frame_buffer_.length(), kCIESize); |
| + |
|
rmcilroy
2016/06/30 15:23:10
DCHECK(!eh_frame_finalized_)
Stefano Sanfilippo
2016/07/04 18:21:14
Done.
|
| + // Add padding |
| + int unpadded_fde_size = eh_frame_buffer_.length() - kCIESize; |
| + int padded_fde_size = RoundUp(unpadded_fde_size, 8); |
| + int fde_padding_size = padded_fde_size - unpadded_fde_size; |
| + |
| + static const byte kFDEPadding[] = {kNop, kNop, kNop, kNop, |
| + kNop, kNop, kNop, kNop}; |
| + DCHECK_LT(fde_padding_size, static_cast<int>(sizeof(kFDEPadding))); |
| + WriteBytes(&kFDEPadding[0], fde_padding_size); |
| + |
| + // Write the size of the FDE now that we know it. |
| + PatchInt32(kFDEOffset, padded_fde_size); |
| -const int EhFrameHdr::kCIESize = 0; |
| + // Write the size and offset to procedure. |
| + PatchInt32(kProcedureAddressOffset, |
| + -(RoundUp(code_size, 8) + kProcedureAddressOffset)); |
| + PatchInt32(kProcedureSizeOffset, code_size); |
| -static const int kVersionSize = 1; |
| -static const int kEncodingSpecifiersSize = 3; |
| + // Terminate the .eh_frame. |
| + static const byte kEhFrameTerminator[kEhFrameTerminatorSize] = {0}; |
| + WriteBytes(&kEhFrameTerminator[0], kEhFrameTerminatorSize); |
| + |
| + // Write .eh_frame_hdr |
| + EhFrameHdr eh_frame_hdr(code_size, eh_frame_offset()); |
| + WriteBytes(reinterpret_cast<const byte*>(&eh_frame_hdr), |
| + EhFrameHdr::kRecordSize); |
| + |
| +#if DEBUG |
| + eh_frame_finalised_ = true; |
| +#endif |
| +} |
| + |
| +void EhFrameWriter::GetEhFrame(CodeDesc* desc) { |
| + DCHECK(eh_frame_finalised_); |
| + desc->unwinding_info_size = eh_frame_buffer_.length(); |
| + desc->unwinding_info = eh_frame_buffer_.begin(); |
| +} |
| + |
| +void EhFrameWriter::WriteULEB128(uint32_t value) { |
| + do { |
| + byte chunk = value & 0x7f; |
| + value >>= 7; |
| + if (value != 0) chunk |= 0x80; |
| + eh_frame_buffer_.Add(chunk); |
| + } while (value != 0); |
| +} |
| + |
| +#ifdef ENABLE_DISASSEMBLER |
| + |
| +namespace { |
| + |
| +uint32_t DecodeULEB128(const byte* encoded, int* encoded_size) { |
| + const byte* cur = encoded; |
| + uint32_t decoded_value = 0; |
| + |
| + do { |
| + decoded_value <<= 7; |
| + decoded_value += static_cast<uint32_t>(static_cast<unsigned>(*cur & 0x7f)); |
| + } while (*cur++ >= 0x80); |
| + |
| + *encoded_size = static_cast<int>(cur - encoded); |
| + return decoded_value; |
| +} |
| + |
| +class StreamModifiersScope final { |
| + public: |
| + explicit StreamModifiersScope(std::ostream* stream) |
| + : stream_(stream), flags_(stream->flags()) {} |
| + ~StreamModifiersScope() { stream_->flags(flags_); } |
| + |
| + private: |
| + std::ostream* stream_; |
| + std::ios::fmtflags flags_; |
| +}; |
| + |
| +} // namespace |
| + |
| +// static |
| +void EhFrameWriter::DumpDWARFDirectives(std::ostream& stream, // NOLINT |
| + const byte* begin, const byte* end) { |
| + StreamModifiersScope modifiers_scope(&stream); |
| + |
| + const byte* cur = begin; |
| + uint32_t offset_in_procedure = 0; |
| + |
| + while (cur != end) { |
| + stream << reinterpret_cast<const void*>(cur) << " "; |
| + |
| + if (((*cur >> kLocationMaskSize) & 0xff) == kLocationTag) { |
| + int value = *cur & kLocationMask; |
| + cur += sizeof(byte); |
| + offset_in_procedure += value; |
| + stream << "| pc_offset=" << std::dec << offset_in_procedure |
| + << " (delta=0x" << std::hex << value << ")\n"; |
| + continue; |
| + } |
| + |
| + if (((*cur >> kSavedRegisterMaskSize) & 0xff) == kSavedRegisterTag) { |
| + stream << "| " << DwarfRegisterCodeToString(*cur & kLocationMask); |
| + cur += sizeof(byte); |
| + int decoded_size = 0; |
| + int decoded_offset = static_cast<int>(DecodeULEB128(cur, &decoded_size)); |
| + cur += decoded_size; |
| + stream << " saved at base" << std::showpos << std::dec |
| + << decoded_offset * kDataAlignmentFactor << '\n'; |
| + continue; |
| + } |
| + |
| + uint8_t bytecode = *cur; |
| + cur += sizeof(byte); |
| + |
| + switch (bytecode) { |
| + case kAdvanceLoc1: { |
| + unsigned value = *reinterpret_cast<const uint8_t*>(cur); |
| + cur += sizeof(uint8_t); |
| + offset_in_procedure += value; |
| + stream << "| pc_offset=" << std::dec << offset_in_procedure |
| + << " (delta=0x" << std::hex << value << ")\n"; |
| + break; |
| + } |
| + case kAdvanceLoc2: { |
| + uint16_t value = ReadUnalignedUInt16(cur); |
| + cur += sizeof(uint16_t); |
| + offset_in_procedure += value; |
| + stream << "| pc_offset=" << std::dec << offset_in_procedure |
| + << " (delta=0x" << std::hex << value << ")\n"; |
| + break; |
| + } |
| + case kAdvanceLoc4: { |
| + uint32_t value = ReadUnalignedUInt32(cur); |
| + offset_in_procedure += value; |
| + cur += sizeof(uint32_t); |
| + stream << "| pc_offset=" << std::dec << offset_in_procedure |
| + << " (delta=0x" << std::hex << value << ")\n"; |
| + break; |
| + } |
| + case kDefCFA: { |
| + int decoded_size = 0; |
| + int base_register = DecodeULEB128(cur, &decoded_size); |
| + cur += decoded_size; |
| + int base_offset = DecodeULEB128(cur, &decoded_size); |
| + cur += decoded_size; |
| + stream << "| base_register=" << DwarfRegisterCodeToString(base_register) |
| + << ", base_offset=0x" << std::hex << base_offset << '\n'; |
| + break; |
| + } |
| + case kDefCFAOffset: { |
| + int decoded_size = 0; |
| + stream << "| base_offset=0x" << std::hex |
| + << DecodeULEB128(cur, &decoded_size) << '\n'; |
| + cur += decoded_size; |
| + break; |
| + } |
| + case kDefCFARegister: { |
| + int decoded_size = 0; |
| + stream << "| base_register=" |
| + << DwarfRegisterCodeToString(DecodeULEB128(cur, &decoded_size)) |
| + << '\n'; |
| + cur += decoded_size; |
| + break; |
| + } |
| + case kSameValue: { |
| + int decoded_size = 0; |
| + stream << "| " |
| + << DwarfRegisterCodeToString(DecodeULEB128(cur, &decoded_size)) |
| + << " to initial value\n"; |
| + cur += decoded_size; |
| + break; |
| + } |
| + case kNop: |
| + stream << "| nop\n"; |
| + break; |
| + default: |
| + UNREACHABLE(); |
| + return; |
| + } |
| + } |
| +} |
| + |
| +// static |
| +void EhFrameWriter::DisassembleToStream(std::ostream& stream, // NOLINT |
| + const byte* start, const byte* end) { |
| + const byte* cie_directives_start = start + kInitialStateOffsetInCIE; |
| + const byte* cie_directives_end = start + kCIESize; |
| + DCHECK_LE(cie_directives_start, cie_directives_end); |
| + |
| + stream << reinterpret_cast<const void*>(start) << " .eh_frame: CIE\n"; |
| + DumpDWARFDirectives(stream, cie_directives_start, cie_directives_end); |
| + |
| + const byte* procedure_offset_address = start + kProcedureAddressOffset; |
| + int32_t procedure_offset = |
| + ReadUnalignedValue<int32_t>(procedure_offset_address); |
| + |
| + const byte* procedure_size_address = start + kProcedureSizeOffset; |
| + uint32_t procedure_size = ReadUnalignedUInt32(procedure_size_address); |
| + |
| + const byte* fde_start = start + kCIESize; |
| + stream << reinterpret_cast<const void*>(fde_start) << " .eh_frame: FDE\n" |
| + << reinterpret_cast<const void*>(procedure_offset_address) |
| + << " | procedure offset=" << procedure_offset << '\n' |
| + << reinterpret_cast<const void*>(procedure_size_address) |
| + << " | procedure size=" << procedure_size << '\n'; |
| + |
| + const byte* fde_directives_start = start + kFDEDirectivesOffset; |
| + const byte* fde_directives_end = |
| + end - EhFrameHdr::kRecordSize - kEhFrameTerminatorSize; |
| + DCHECK_LE(fde_directives_start, fde_directives_end); |
| + |
| + DumpDWARFDirectives(stream, fde_directives_start, fde_directives_end); |
| + |
| + const byte* fde_terminator_start = fde_directives_end; |
| + stream << reinterpret_cast<const void*>(fde_terminator_start) |
| + << " .eh_frame: terminator\n"; |
| + |
| + const byte* eh_frame_hdr_start = |
| + fde_terminator_start + kEhFrameTerminatorSize; |
| + stream << reinterpret_cast<const void*>(eh_frame_hdr_start) |
| + << " .eh_frame_hdr: placeholder\n"; |
| +} |
| + |
| +#endif |
| // |
| // In order to calculate offsets in the .eh_frame_hdr, we must know the layout |
| @@ -51,45 +405,47 @@ static const int kEncodingSpecifiersSize = 3; |
| // +---------------+ | |
| // | ... | ... |
| // |
| -// (F) is aligned at a 16-byte boundary. |
| -// (D) is aligned at a 8-byte boundary. |
| -// (B) is aligned at a 4-byte boundary. |
| -// (E), (C) and (A) have no alignment requirements. |
| +// (F) is aligned to a 16-byte boundary. |
| +// (D) is aligned to a 8-byte boundary. |
| +// (B) is aligned to a 4-byte boundary. |
| +// (C) is aligned to an addressing unit size boundary. |
| +// (E) and (A) have no alignment requirements. |
| // |
| // The distance between (A) and (B) is 4 bytes. |
| // |
| -// The size of the .eh_frame is required to be a multiple of the pointer size, |
| -// which means that (B) will be naturally aligned to a 4-byte boundary on all |
| -// the architectures we support. |
| +// The size of the FDE is required to be a multiple of the pointer size, which |
| +// means that (B) will be naturally aligned to a 4-byte boundary on all the |
| +// architectures we support. |
| // |
| // Because (E) has no alignment requirements, there is padding between (E) and |
| // (D). (F) is aligned at a 16-byte boundary, thus to a 8-byte one as well. |
| // |
| -EhFrameHdr::EhFrameHdr(Code* code) { |
| - int code_size = code->is_crankshafted() ? code->safepoint_table_offset() |
| - : code->instruction_size(); |
| - version_ = 1; |
| - eh_frame_ptr_encoding_ = DW_EH_PE_sdata4 | DW_EH_PE_pcrel; |
| - lut_size_encoding_ = DW_EH_PE_udata4; |
| - lut_entries_encoding_ = DW_EH_PE_sdata4 | DW_EH_PE_datarel; |
| - |
| - // .eh_frame pointer and LUT |
| - if (code->has_unwinding_info()) { |
| - DCHECK_GE(code->unwinding_info_size(), EhFrameHdr::kRecordSize); |
| - int eh_frame_size = code->unwinding_info_size() - EhFrameHdr::kRecordSize; |
| - |
| - offset_to_eh_frame_ = |
| - -(eh_frame_size + kVersionSize + kEncodingSpecifiersSize); // A -> D |
| - lut_entries_number_ = 1; |
| - offset_to_procedure_ = -(RoundUp(code_size, 8) + eh_frame_size); // B -> F |
| - offset_to_fde_ = -(eh_frame_size - kCIESize); // B -> C |
| - } else { |
| - // Create a dummy table |
| - offset_to_eh_frame_ = 0; |
| - lut_entries_number_ = 0; |
| - offset_to_procedure_ = 0; |
| - offset_to_fde_ = 0; |
| - } |
| +EhFrameHdr::EhFrameHdr(int code_size, int eh_frame_size) { |
| + version_ = kEhFrameHdrVersion; |
| + |
| + eh_frame_ptr_encoding_ = kSData4 | kPcRel; |
| + lut_size_encoding_ = kUData4; |
| + lut_entries_encoding_ = kSData4 | kDataRel; |
| + |
| + offset_to_eh_frame_ = -(eh_frame_size + kFDEVersionSize + |
| + kFDEEncodingSpecifiersSize); // A -> D |
| + lut_entries_number_ = 1; |
| + offset_to_procedure_ = -(RoundUp(code_size, 8) + eh_frame_size); // B -> F |
| + offset_to_fde_ = -(eh_frame_size - EhFrameWriter::kCIESize); // B -> C |
| +} |
| + |
| +// static |
| +EhFrameHdr EhFrameHdr::MakeDummy() { |
| + EhFrameHdr dummy_frame; |
| + dummy_frame.version_ = kEhFrameHdrVersion; |
| + dummy_frame.eh_frame_ptr_encoding_ = kSData4 | kPcRel; |
| + dummy_frame.lut_size_encoding_ = kUData4; |
| + dummy_frame.lut_entries_encoding_ = kSData4 | kDataRel; |
| + dummy_frame.offset_to_eh_frame_ = 0; |
| + dummy_frame.lut_entries_number_ = 0; |
| + dummy_frame.offset_to_procedure_ = 0; |
| + dummy_frame.offset_to_fde_ = 0; |
| + return dummy_frame; |
| } |
| } // namespace internal |