|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +/// \file |
| 10 | +/// This file contains data-structure definitions and constants to support |
| 11 | +/// unwinding based on .sframe sections. This only supports SFRAME_VERSION_2 |
| 12 | +/// as described at https://sourceware.org/binutils/docs/sframe-spec.html |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#ifndef LLVM_BINARYFORMAT_SFRAME_H |
| 16 | +#define LLVM_BINARYFORMAT_SFRAME_H |
| 17 | + |
| 18 | +#include "llvm/ADT/BitmaskEnum.h" |
| 19 | +#include "llvm/Support/DataTypes.h" |
| 20 | +#include "llvm/Support/Endian.h" |
| 21 | + |
| 22 | +namespace llvm::sframe { |
| 23 | + |
| 24 | +LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE(); |
| 25 | + |
| 26 | +constexpr uint16_t Magic = 0xdee2; |
| 27 | + |
| 28 | +enum class Version : uint8_t { |
| 29 | + V1 = 1, |
| 30 | + V2 = 2, |
| 31 | +}; |
| 32 | + |
| 33 | +enum class Flags : uint8_t { |
| 34 | + FDESorted = 0x01, |
| 35 | + FramePointer = 0x02, |
| 36 | + FDEFuncStartPCRel = 0x04, |
| 37 | + V2AllFlags = FDESorted | FramePointer | FDEFuncStartPCRel, |
| 38 | + LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/0xff), |
| 39 | +}; |
| 40 | + |
| 41 | +enum class ABI : uint8_t { |
| 42 | + AArch64EndianBig = 1, |
| 43 | + AArch64EndianLittle = 2, |
| 44 | + AMD64EndianLittle = 3, |
| 45 | +}; |
| 46 | + |
| 47 | +/// SFrame FRE Types. Bits 0-3 of FuncDescEntry.Info. |
| 48 | +enum class FREType : uint8_t { |
| 49 | + Addr1 = 0, |
| 50 | + Addr2 = 1, |
| 51 | + Addr4 = 2, |
| 52 | +}; |
| 53 | + |
| 54 | +/// SFrame FDE Types. Bit 4 of FuncDescEntry.Info. |
| 55 | +enum class FDEType : uint8_t { |
| 56 | + PCInc = 0, |
| 57 | + PCMask = 1, |
| 58 | +}; |
| 59 | + |
| 60 | +/// Speficies key used for signing return addresses. Bit 5 of |
| 61 | +/// FuncDescEntry.Info. |
| 62 | +enum class AArch64PAuthKey : uint8_t { |
| 63 | + A = 0, |
| 64 | + B = 1, |
| 65 | +}; |
| 66 | + |
| 67 | +/// Size of stack offsets. Bits 5-6 of FREInfo.Info. |
| 68 | +enum class FREOffset : uint8_t { |
| 69 | + B1 = 0, |
| 70 | + B2 = 1, |
| 71 | + B4 = 2, |
| 72 | +}; |
| 73 | + |
| 74 | +/// Stack frame base register. Bit 0 of FREInfo.Info. |
| 75 | +enum class BaseReg : uint8_t { |
| 76 | + FP = 0, |
| 77 | + SP = 1, |
| 78 | +}; |
| 79 | + |
| 80 | +namespace detail { |
| 81 | +template <typename T, endianness E> |
| 82 | +using packed = |
| 83 | + support::detail::packed_endian_specific_integral<T, E, support::unaligned>; |
| 84 | +} |
| 85 | + |
| 86 | +template <endianness E> struct Preamble { |
| 87 | + detail::packed<uint16_t, E> Magic; |
| 88 | + detail::packed<enum Version, E> Version; |
| 89 | + detail::packed<enum Flags, E> Flags; |
| 90 | +}; |
| 91 | + |
| 92 | +template <endianness E> struct Header { |
| 93 | + struct Preamble<E> Preamble; |
| 94 | + detail::packed<ABI, E> ABIArch; |
| 95 | + detail::packed<int8_t, E> CFAFixedFPOffset; |
| 96 | + detail::packed<int8_t, E> CFAFixedRAOffset; |
| 97 | + detail::packed<uint8_t, E> AuxHdrLen; |
| 98 | + detail::packed<uint32_t, E> NumFDEs; |
| 99 | + detail::packed<uint32_t, E> NumFREs; |
| 100 | + detail::packed<uint32_t, E> FRELen; |
| 101 | + detail::packed<uint32_t, E> FDEOff; |
| 102 | + detail::packed<uint32_t, E> FREOff; |
| 103 | +}; |
| 104 | + |
| 105 | +template <endianness E> struct FuncDescEntry { |
| 106 | + detail::packed<int32_t, E> StartAddress; |
| 107 | + detail::packed<uint32_t, E> Size; |
| 108 | + detail::packed<uint32_t, E> StartFREOff; |
| 109 | + detail::packed<uint32_t, E> NumFREs; |
| 110 | + detail::packed<uint8_t, E> Info; |
| 111 | + detail::packed<uint8_t, E> RepSize; |
| 112 | + detail::packed<uint16_t, E> Padding2; |
| 113 | + |
| 114 | + uint8_t getPAuthKey() const { return (Info >> 5) & 1; } |
| 115 | + FDEType getFDEType() const { return static_cast<FDEType>((Info >> 4) & 1); } |
| 116 | + FREType getFREType() const { return static_cast<FREType>(Info & 0xf); } |
| 117 | + void setPAuthKey(uint8_t P) { setFuncInfo(P, getFDEType(), getFREType()); } |
| 118 | + void setFDEType(FDEType D) { setFuncInfo(getPAuthKey(), D, getFREType()); } |
| 119 | + void setFREType(FREType R) { setFuncInfo(getPAuthKey(), getFDEType(), R); } |
| 120 | + void setFuncInfo(uint8_t PAuthKey, FDEType FDE, FREType FRE) { |
| 121 | + Info = ((PAuthKey & 1) << 5) | ((static_cast<uint8_t>(FDE) & 1) << 4) | |
| 122 | + (static_cast<uint8_t>(FRE) & 0xf); |
| 123 | + } |
| 124 | +}; |
| 125 | + |
| 126 | +template <endianness E> struct FREInfo { |
| 127 | + detail::packed<uint8_t, E> Info; |
| 128 | + |
| 129 | + bool isReturnAddressSigned() const { return Info >> 7; } |
| 130 | + FREOffset getOffsetSize() const { |
| 131 | + return static_cast<FREOffset>((Info >> 5) & 3); |
| 132 | + } |
| 133 | + uint8_t getOffsetCount() const { return (Info >> 1) & 0xf; } |
| 134 | + BaseReg getBaseRegister() const { return static_cast<BaseReg>(Info & 1); } |
| 135 | + void setReturnAddressSigned(bool RA) { |
| 136 | + setFREInfo(RA, getOffsetSize(), getOffsetCount(), getBaseRegister()); |
| 137 | + } |
| 138 | + void setOffsetSize(FREOffset Sz) { |
| 139 | + setFREInfo(isReturnAddressSigned(), Sz, getOffsetCount(), |
| 140 | + getBaseRegister()); |
| 141 | + } |
| 142 | + void setOffsetCount(uint8_t N) { |
| 143 | + setFREInfo(isReturnAddressSigned(), getOffsetSize(), N, getBaseRegister()); |
| 144 | + } |
| 145 | + void setBaseRegister(BaseReg Reg) { |
| 146 | + setFREInfo(isReturnAddressSigned(), getOffsetSize(), getOffsetCount(), Reg); |
| 147 | + } |
| 148 | + void setFREInfo(bool RA, FREOffset Sz, uint8_t N, BaseReg Reg) { |
| 149 | + Info = ((RA & 1) << 7) | ((static_cast<uint8_t>(Sz) & 3) << 5) | |
| 150 | + ((N & 0xf) << 1) | (static_cast<uint8_t>(Reg) & 1); |
| 151 | + } |
| 152 | +}; |
| 153 | + |
| 154 | +template <typename T, endianness E> struct FrameRowEntry { |
| 155 | + detail::packed<T, E> StartAddress; |
| 156 | + FREInfo<E> Info; |
| 157 | +}; |
| 158 | + |
| 159 | +template <endianness E> using FrameRowEntryAddr1 = FrameRowEntry<uint8_t, E>; |
| 160 | +template <endianness E> using FrameRowEntryAddr2 = FrameRowEntry<uint16_t, E>; |
| 161 | +template <endianness E> using FrameRowEntryAddr4 = FrameRowEntry<uint32_t, E>; |
| 162 | + |
| 163 | +} // namespace llvm::sframe |
| 164 | + |
| 165 | +#endif // LLVM_BINARYFORMAT_SFRAME_H |
0 commit comments