2013-04-16 20:02:21 +08:00
|
|
|
//===-- ARMUnwindOpAsm.cpp - ARM Unwind Opcodes Assembler -------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-04-16 20:02:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2020-01-06 18:15:44 +08:00
|
|
|
// This file implements the unwind opcode assembler for ARM exception handling
|
2013-04-16 20:02:21 +08:00
|
|
|
// table.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ARMUnwindOpAsm.h"
|
2014-01-06 08:15:00 +08:00
|
|
|
#include "llvm/Support/ARMEHABI.h"
|
2013-04-16 20:02:21 +08:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2017-01-31 08:56:17 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include <cassert>
|
2013-04-16 20:02:21 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2013-06-09 20:22:30 +08:00
|
|
|
namespace {
|
2017-01-31 08:56:17 +08:00
|
|
|
|
2013-06-09 20:22:30 +08:00
|
|
|
/// UnwindOpcodeStreamer - The simple wrapper over SmallVector to emit bytes
|
|
|
|
/// with MSB to LSB per uint32_t ordering. For example, the first byte will
|
|
|
|
/// be placed in Vec[3], and the following bytes will be placed in 2, 1, 0,
|
|
|
|
/// 7, 6, 5, 4, 11, 10, 9, 8, and so on.
|
|
|
|
class UnwindOpcodeStreamer {
|
|
|
|
private:
|
|
|
|
SmallVectorImpl<uint8_t> &Vec;
|
2017-01-31 08:56:17 +08:00
|
|
|
size_t Pos = 3;
|
2013-06-09 20:22:30 +08:00
|
|
|
|
|
|
|
public:
|
2017-01-31 08:56:17 +08:00
|
|
|
UnwindOpcodeStreamer(SmallVectorImpl<uint8_t> &V) : Vec(V) {}
|
2013-06-09 20:22:30 +08:00
|
|
|
|
|
|
|
/// Emit the byte in MSB to LSB per uint32_t order.
|
2017-01-31 08:56:17 +08:00
|
|
|
void EmitByte(uint8_t elem) {
|
2013-06-09 20:22:30 +08:00
|
|
|
Vec[Pos] = elem;
|
|
|
|
Pos = (((Pos ^ 0x3u) + 1) ^ 0x3u);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit the size prefix.
|
2017-01-31 08:56:17 +08:00
|
|
|
void EmitSize(size_t Size) {
|
2013-06-09 20:22:30 +08:00
|
|
|
size_t SizeInWords = (Size + 3) / 4;
|
|
|
|
assert(SizeInWords <= 0x100u &&
|
|
|
|
"Only 256 additional words are allowed for unwind opcodes");
|
|
|
|
EmitByte(static_cast<uint8_t>(SizeInWords - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit the personality index prefix.
|
2017-01-31 08:56:17 +08:00
|
|
|
void EmitPersonalityIndex(unsigned PI) {
|
2014-01-06 08:15:00 +08:00
|
|
|
assert(PI < ARM::EHABI::NUM_PERSONALITY_INDEX &&
|
|
|
|
"Invalid personality prefix");
|
|
|
|
EmitByte(ARM::EHABI::EHT_COMPACT | PI);
|
2013-06-09 20:22:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fill the rest of bytes with FINISH opcode.
|
2017-01-31 08:56:17 +08:00
|
|
|
void FillFinishOpcode() {
|
2013-06-09 20:22:30 +08:00
|
|
|
while (Pos < Vec.size())
|
2014-01-06 08:15:00 +08:00
|
|
|
EmitByte(ARM::EHABI::UNWIND_OPCODE_FINISH);
|
2013-06-09 20:22:30 +08:00
|
|
|
}
|
|
|
|
};
|
2017-01-31 08:56:17 +08:00
|
|
|
|
|
|
|
} // end anonymous namespace
|
2013-06-09 20:22:30 +08:00
|
|
|
|
2013-04-16 20:02:21 +08:00
|
|
|
void UnwindOpcodeAssembler::EmitRegSave(uint32_t RegSave) {
|
|
|
|
if (RegSave == 0u)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// One byte opcode to save register r14 and r11-r4
|
|
|
|
if (RegSave & (1u << 4)) {
|
|
|
|
// The one byte opcode will always save r4, thus we can't use the one byte
|
|
|
|
// opcode when r4 is not in .save directive.
|
|
|
|
|
|
|
|
// Compute the consecutive registers from r4 to r11.
|
2015-03-25 23:27:58 +08:00
|
|
|
uint32_t Mask = RegSave & 0xff0u;
|
|
|
|
uint32_t Range = countTrailingOnes(Mask >> 5); // Exclude r4.
|
|
|
|
// Mask off non-consecutive registers. Keep r4.
|
|
|
|
Mask &= ~(0xffffffe0u << Range);
|
2013-04-16 20:02:21 +08:00
|
|
|
|
|
|
|
// Emit this opcode when the mask covers every registers.
|
|
|
|
uint32_t UnmaskedReg = RegSave & 0xfff0u & (~Mask);
|
|
|
|
if (UnmaskedReg == 0u) {
|
|
|
|
// Pop r[4 : (4 + n)]
|
2014-01-06 08:15:00 +08:00
|
|
|
EmitInt8(ARM::EHABI::UNWIND_OPCODE_POP_REG_RANGE_R4 | Range);
|
2013-04-16 20:02:21 +08:00
|
|
|
RegSave &= 0x000fu;
|
|
|
|
} else if (UnmaskedReg == (1u << 14)) {
|
|
|
|
// Pop r[14] + r[4 : (4 + n)]
|
2014-01-06 08:15:00 +08:00
|
|
|
EmitInt8(ARM::EHABI::UNWIND_OPCODE_POP_REG_RANGE_R4_R14 | Range);
|
2013-04-16 20:02:21 +08:00
|
|
|
RegSave &= 0x000fu;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Two bytes opcode to save register r15-r4
|
2013-06-09 20:22:30 +08:00
|
|
|
if ((RegSave & 0xfff0u) != 0)
|
2014-01-06 08:15:00 +08:00
|
|
|
EmitInt16(ARM::EHABI::UNWIND_OPCODE_POP_REG_MASK_R4 | (RegSave >> 4));
|
2013-04-16 20:02:21 +08:00
|
|
|
|
|
|
|
// Opcode to save register r3-r0
|
2013-06-09 20:22:30 +08:00
|
|
|
if ((RegSave & 0x000fu) != 0)
|
2014-01-06 08:15:00 +08:00
|
|
|
EmitInt16(ARM::EHABI::UNWIND_OPCODE_POP_REG_MASK | (RegSave & 0x000fu));
|
2013-04-16 20:02:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit unwind opcodes for .vsave directives
|
|
|
|
void UnwindOpcodeAssembler::EmitVFPRegSave(uint32_t VFPRegSave) {
|
2015-03-25 23:27:58 +08:00
|
|
|
// We only have 4 bits to save the offset in the opcode so look at the lower
|
|
|
|
// and upper 16 bits separately.
|
|
|
|
for (uint32_t Regs : {VFPRegSave & 0xffff0000u, VFPRegSave & 0x0000ffffu}) {
|
|
|
|
while (Regs) {
|
|
|
|
// Now look for a run of set bits. Remember the MSB and LSB of the run.
|
|
|
|
auto RangeMSB = 32 - countLeadingZeros(Regs);
|
|
|
|
auto RangeLen = countLeadingOnes(Regs << (32 - RangeMSB));
|
|
|
|
auto RangeLSB = RangeMSB - RangeLen;
|
|
|
|
|
|
|
|
int Opcode = RangeLSB >= 16
|
|
|
|
? ARM::EHABI::UNWIND_OPCODE_POP_VFP_REG_RANGE_FSTMFDD_D16
|
|
|
|
: ARM::EHABI::UNWIND_OPCODE_POP_VFP_REG_RANGE_FSTMFDD;
|
|
|
|
|
|
|
|
EmitInt16(Opcode | ((RangeLSB % 16) << 4) | (RangeLen - 1));
|
|
|
|
|
|
|
|
// Zero out bits we're done with.
|
|
|
|
Regs &= ~(-1u << RangeLSB);
|
2013-04-16 20:02:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-09 20:22:30 +08:00
|
|
|
/// Emit unwind opcodes to copy address from source register to $sp.
|
|
|
|
void UnwindOpcodeAssembler::EmitSetSP(uint16_t Reg) {
|
2014-01-06 08:15:00 +08:00
|
|
|
EmitInt8(ARM::EHABI::UNWIND_OPCODE_SET_VSP | Reg);
|
2013-04-16 20:02:21 +08:00
|
|
|
}
|
|
|
|
|
2013-06-09 20:22:30 +08:00
|
|
|
/// Emit unwind opcodes to add $sp with an offset.
|
2013-04-16 20:02:21 +08:00
|
|
|
void UnwindOpcodeAssembler::EmitSPOffset(int64_t Offset) {
|
|
|
|
if (Offset > 0x200) {
|
2013-06-09 20:22:30 +08:00
|
|
|
uint8_t Buff[16];
|
2014-01-06 08:15:00 +08:00
|
|
|
Buff[0] = ARM::EHABI::UNWIND_OPCODE_INC_VSP_ULEB128;
|
2013-06-09 20:22:30 +08:00
|
|
|
size_t ULEBSize = encodeULEB128((Offset - 0x204) >> 2, Buff + 1);
|
2020-02-15 10:16:24 +08:00
|
|
|
emitBytes(Buff, ULEBSize + 1);
|
2013-04-16 20:02:21 +08:00
|
|
|
} else if (Offset > 0) {
|
|
|
|
if (Offset > 0x100) {
|
2014-01-06 08:15:00 +08:00
|
|
|
EmitInt8(ARM::EHABI::UNWIND_OPCODE_INC_VSP | 0x3fu);
|
2013-04-16 20:02:21 +08:00
|
|
|
Offset -= 0x100;
|
|
|
|
}
|
2014-01-06 08:15:00 +08:00
|
|
|
EmitInt8(ARM::EHABI::UNWIND_OPCODE_INC_VSP |
|
|
|
|
static_cast<uint8_t>((Offset - 4) >> 2));
|
2013-04-16 20:02:21 +08:00
|
|
|
} else if (Offset < 0) {
|
|
|
|
while (Offset < -0x100) {
|
2014-01-06 08:15:00 +08:00
|
|
|
EmitInt8(ARM::EHABI::UNWIND_OPCODE_DEC_VSP | 0x3fu);
|
2013-04-16 20:02:21 +08:00
|
|
|
Offset += 0x100;
|
|
|
|
}
|
2014-01-06 08:15:00 +08:00
|
|
|
EmitInt8(ARM::EHABI::UNWIND_OPCODE_DEC_VSP |
|
2013-06-09 20:22:30 +08:00
|
|
|
static_cast<uint8_t>(((-Offset) - 4) >> 2));
|
2013-04-16 20:02:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-09 20:22:30 +08:00
|
|
|
void UnwindOpcodeAssembler::Finalize(unsigned &PersonalityIndex,
|
|
|
|
SmallVectorImpl<uint8_t> &Result) {
|
|
|
|
UnwindOpcodeStreamer OpStreamer(Result);
|
2013-04-16 20:02:21 +08:00
|
|
|
|
|
|
|
if (HasPersonality) {
|
2013-06-09 20:22:30 +08:00
|
|
|
// User-specifed personality routine: [ SIZE , OP1 , OP2 , ... ]
|
2014-01-06 08:15:00 +08:00
|
|
|
PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
|
2013-06-09 20:22:30 +08:00
|
|
|
size_t TotalSize = Ops.size() + 1;
|
|
|
|
size_t RoundUpSize = (TotalSize + 3) / 4 * 4;
|
|
|
|
Result.resize(RoundUpSize);
|
|
|
|
OpStreamer.EmitSize(RoundUpSize);
|
2013-04-16 20:02:21 +08:00
|
|
|
} else {
|
2014-01-21 10:33:02 +08:00
|
|
|
// If no personalityindex is specified, select ane
|
|
|
|
if (PersonalityIndex == ARM::EHABI::NUM_PERSONALITY_INDEX)
|
|
|
|
PersonalityIndex = (Ops.size() <= 3) ? ARM::EHABI::AEABI_UNWIND_CPP_PR0
|
|
|
|
: ARM::EHABI::AEABI_UNWIND_CPP_PR1;
|
|
|
|
if (PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0) {
|
2013-04-16 20:02:21 +08:00
|
|
|
// __aeabi_unwind_cpp_pr0: [ 0x80 , OP1 , OP2 , OP3 ]
|
2014-01-21 10:33:02 +08:00
|
|
|
assert(Ops.size() <= 3 && "too many opcodes for __aeabi_unwind_cpp_pr0");
|
2013-06-09 20:22:30 +08:00
|
|
|
Result.resize(4);
|
|
|
|
OpStreamer.EmitPersonalityIndex(PersonalityIndex);
|
2013-04-16 20:02:21 +08:00
|
|
|
} else {
|
2014-01-21 10:33:02 +08:00
|
|
|
// __aeabi_unwind_cpp_pr{1,2}: [ {0x81,0x82} , SIZE , OP1 , OP2 , ... ]
|
2013-06-09 20:22:30 +08:00
|
|
|
size_t TotalSize = Ops.size() + 2;
|
|
|
|
size_t RoundUpSize = (TotalSize + 3) / 4 * 4;
|
|
|
|
Result.resize(RoundUpSize);
|
|
|
|
OpStreamer.EmitPersonalityIndex(PersonalityIndex);
|
|
|
|
OpStreamer.EmitSize(RoundUpSize);
|
2013-04-16 20:02:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-09 20:22:30 +08:00
|
|
|
// Copy the unwind opcodes
|
|
|
|
for (size_t i = OpBegins.size() - 1; i > 0; --i)
|
|
|
|
for (size_t j = OpBegins[i - 1], end = OpBegins[i]; j < end; ++j)
|
|
|
|
OpStreamer.EmitByte(Ops[j]);
|
2013-04-16 20:02:21 +08:00
|
|
|
|
2013-06-09 20:22:30 +08:00
|
|
|
// Emit the padding finish opcodes if the size is not multiple of 4.
|
|
|
|
OpStreamer.FillFinishOpcode();
|
|
|
|
|
|
|
|
// Reset the assembler state
|
|
|
|
Reset();
|
2013-04-16 20:02:21 +08:00
|
|
|
}
|