|
| 1 | +//===-- llvm/BinaryFormat/SFrame.h ---SFrame Data Structures ----*- C++ -*-===// |
| 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 | +/// Naming conventions follow the spec document. #defines converted to constants |
| 15 | +/// and enums for better C++ compatibility. |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | + |
| 18 | +#ifndef LLVM_BINARYFORMAT_SFRAME_H |
| 19 | +#define LLVM_BINARYFORMAT_SFRAME_H |
| 20 | + |
| 21 | +#include "llvm/Support/Compiler.h" |
| 22 | +#include "llvm/Support/DataTypes.h" |
| 23 | + |
| 24 | +namespace llvm { |
| 25 | + |
| 26 | +namespace sframe { |
| 27 | + |
| 28 | +//===----------------------------------------------------------------------===// |
| 29 | + |
| 30 | +constexpr uint16_t SFRAME_MAGIC = 0xDEE2; |
| 31 | + |
| 32 | +enum : uint8_t { |
| 33 | + SFRAME_VERSION_1 = 1, |
| 34 | + SFRAME_VERSION_2 = 2, |
| 35 | +}; |
| 36 | + |
| 37 | +/// sframe_preable.sfp_flags flags. |
| 38 | +enum : uint8_t { |
| 39 | + SFRAME_F_FDE_SORTED = 0x1, |
| 40 | + SFRAME_F_FRAME_POINTER = 0x2, |
| 41 | +}; |
| 42 | + |
| 43 | +/// Possible values for sframe_header.sfh_abi_arch. |
| 44 | +enum : uint8_t { |
| 45 | + SFRAME_ABI_AARCH64_ENDIAN_BIG = 1, |
| 46 | + SFRAME_ABI_AARCH64_ENDIAN_LITTLE = 2, |
| 47 | + SFRAME_ABI_AMD64_ENDIAN_LITTLE = 3 |
| 48 | +}; |
| 49 | + |
| 50 | +/// SFrame FRE Types. Bits 0-3 of sframe_func_desc_entry.sfde_func_info. |
| 51 | +enum : uint8_t { |
| 52 | + SFRAME_FRE_TYPE_ADDR1 = 0, |
| 53 | + SFRAME_FRE_TYPE_ADDR2 = 1, |
| 54 | + SFRAME_FRE_TYPE_ADDR4 = 2, |
| 55 | +}; |
| 56 | + |
| 57 | +/// SFrame FDE Types. Bit 4 of sframe_func_desc_entry.sfde_func_info. |
| 58 | +enum : uint8_t { |
| 59 | + SFRAME_FDE_TYPE_PCINC = 0, |
| 60 | + SFRAME_FDE_TYPE_PCMASK = 1, |
| 61 | +}; |
| 62 | + |
| 63 | +/// Speficies key used for signing return addresses. Bit 5 of |
| 64 | +/// sframe_func_desc_entry.sfde_func_info. |
| 65 | +enum : uint8_t { |
| 66 | + SFRAME_AARCH64_PAUTH_KEY_A = 0, |
| 67 | + SFRAME_AARCH64_PAUTH_KEY_B = 1, |
| 68 | +}; |
| 69 | + |
| 70 | +/// Size of stack offsets. Bits 5-6 of sframe_fre_info.fre_info. |
| 71 | +enum : uint8_t { |
| 72 | + SFRAME_FRE_OFFSET_1B = 0, |
| 73 | + SFRAME_FRE_OFFSET_2B = 1, |
| 74 | + SFRAME_FRE_OFFSET_4B = 2, |
| 75 | +}; |
| 76 | + |
| 77 | +/// Stack frame base register. Bit 0 of sframe_fre_info.fre_info. |
| 78 | +enum : uint8_t { SFRAME_BASE_REG_FP = 0, SFRAME_BASE_REG_SP = 1 }; |
| 79 | + |
| 80 | +LLVM_PACKED_START |
| 81 | + |
| 82 | +struct sframe_preamble { |
| 83 | + uint16_t sfp_magic; |
| 84 | + uint8_t sfp_version; |
| 85 | + uint8_t sfp_flags; |
| 86 | +}; |
| 87 | + |
| 88 | +struct sframe_header { |
| 89 | + sframe_preamble sfh_preamble; |
| 90 | + uint8_t sfh_abi_arch; |
| 91 | + int8_t sfh_cfa_fixed_fp_offset; |
| 92 | + int8_t sfh_cfa_fixed_ra_offset; |
| 93 | + uint8_t sfh_auxhdr_len; |
| 94 | + uint32_t sfh_num_fdes; |
| 95 | + uint32_t sfh_num_fres; |
| 96 | + uint32_t sfh_fre_len; |
| 97 | + uint32_t sfh_fdeoff; |
| 98 | + uint32_t sfh_freoff; |
| 99 | +}; |
| 100 | + |
| 101 | +struct sframe_func_desc_entry { |
| 102 | + int32_t sfde_func_start_address; |
| 103 | + uint32_t sfde_func_size; |
| 104 | + uint32_t sfde_func_start_fre_off; |
| 105 | + uint32_t sfde_func_num_fres; |
| 106 | + uint8_t sfde_func_info; |
| 107 | + uint8_t sfde_func_rep_size; |
| 108 | + uint16_t sfde_func_padding2; |
| 109 | + |
| 110 | + uint8_t getPAuthKey() const { return (sfde_func_info >> 5) & 1; } |
| 111 | + uint8_t getFDEType() const { return (sfde_func_info >> 4) & 1; } |
| 112 | + uint8_t getFREType() const { return sfde_func_info & 0xf; } |
| 113 | + void setPAuthKey(uint8_t P) { setFuncInfo(P, getFDEType(), getFREType()); } |
| 114 | + void setFDEType(uint8_t D) { setFuncInfo(getPAuthKey(), D, getFREType()); } |
| 115 | + void setFREType(uint8_t R) { setFuncInfo(getPAuthKey(), getFDEType(), R); } |
| 116 | + void setFuncInfo(uint8_t PAuthKey, uint8_t FDEType, uint8_t FREType) { |
| 117 | + sfde_func_info = |
| 118 | + ((PAuthKey & 1) << 5) | ((FDEType & 1) << 4) | (FREType & 0xf); |
| 119 | + } |
| 120 | +}; |
| 121 | + |
| 122 | +struct sframe_fre_info { |
| 123 | + uint8_t fre_info; |
| 124 | + |
| 125 | + bool isReturnAddressSigned() const { return fre_info >> 7; } |
| 126 | + uint8_t getOffsetSize() const { return (fre_info >> 5) & 3; } |
| 127 | + uint8_t getOffsetCount() const { return (fre_info >> 1) & 0xf; } |
| 128 | + uint8_t getBaseRegister() const { return fre_info & 1; } |
| 129 | + void setReturnAddressSigned(bool RA) { |
| 130 | + setFREInfo(RA, getOffsetSize(), getOffsetCount(), getBaseRegister()); |
| 131 | + } |
| 132 | + void setOffsetSize(uint8_t Sz) { |
| 133 | + setFREInfo(isReturnAddressSigned(), Sz, getOffsetCount(), |
| 134 | + getBaseRegister()); |
| 135 | + } |
| 136 | + void setOffsetCount(uint8_t N) { |
| 137 | + setFREInfo(isReturnAddressSigned(), getOffsetSize(), N, getBaseRegister()); |
| 138 | + } |
| 139 | + void setBaseRegister(uint8_t Reg) { |
| 140 | + setFREInfo(isReturnAddressSigned(), getOffsetSize(), getOffsetCount(), Reg); |
| 141 | + } |
| 142 | + void setFREInfo(bool RA, uint8_t Sz, uint8_t N, uint8_t Reg) { |
| 143 | + fre_info = ((RA & 1) << 7) | ((Sz & 3) << 5) | ((N & 0xf) << 1) | (Reg & 1); |
| 144 | + } |
| 145 | +}; |
| 146 | + |
| 147 | +struct sframe_frame_row_entry_addr1 { |
| 148 | + uint8_t sfre_start_address; |
| 149 | + sframe_fre_info sfre_info; |
| 150 | +}; |
| 151 | + |
| 152 | +struct sframe_frame_row_entry_addr2 { |
| 153 | + uint16_t sfre_start_address; |
| 154 | + sframe_fre_info sfre_info; |
| 155 | +}; |
| 156 | + |
| 157 | +struct sframe_frame_row_entry_addr4 { |
| 158 | + uint32_t sfre_start_address; |
| 159 | + sframe_fre_info sfre_info; |
| 160 | +}; |
| 161 | + |
| 162 | +LLVM_PACKED_END |
| 163 | + |
| 164 | +} // namespace sframe |
| 165 | +} // namespace llvm |
| 166 | + |
| 167 | +#endif // LLVM_BINARYFORMAT_SFRAME_H |
0 commit comments