|
|
|||
File indexing completed on 2025-12-16 10:36:01
0001 /* SFrame format description. 0002 Copyright (C) 2022-2024 Free Software Foundation, Inc. 0003 0004 This file is part of libsframe. 0005 0006 libsframe is free software; you can redistribute it and/or modify it 0007 under the terms of the GNU General Public License as published by the Free 0008 Software Foundation; either version 3, or (at your option) any later 0009 version. 0010 0011 This program is distributed in the hope that it will be useful, but 0012 WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 0014 See the GNU General Public License for more details. 0015 0016 You should have received a copy of the GNU General Public License 0017 along with this program; see the file COPYING. If not see 0018 <http://www.gnu.org/licenses/>. */ 0019 0020 #ifndef _SFRAME_H 0021 #define _SFRAME_H 0022 0023 #include <sys/types.h> 0024 #include <limits.h> 0025 #include <stdint.h> 0026 0027 #include "ansidecl.h" 0028 0029 #ifdef __cplusplus 0030 extern "C" 0031 { 0032 #endif 0033 0034 /* SFrame format. 0035 0036 SFrame format is a simple format to represent the information needed 0037 for generating vanilla backtraces. SFrame format keeps track of the 0038 minimal necessary information needed for stack tracing: 0039 - Canonical Frame Address (CFA) 0040 - Frame Pointer (FP) 0041 - Return Address (RA) 0042 0043 The SFrame section itself has the following structure: 0044 0045 +--------+------------+---------+ 0046 | file | function | frame | 0047 | header | descriptor | row | 0048 | | entries | entries | 0049 +--------+------------+---------+ 0050 0051 The file header stores a magic number and version information, flags, and 0052 the byte offset of each of the sections relative to the end of the header 0053 itself. The file header also specifies the total number of Function 0054 Descriptor Entries, Frame Row Entries and length of the FRE sub-section. 0055 0056 Following the header is a list of Function Descriptor Entries (FDEs). 0057 This list may be sorted if the flags in the file header indicate it to be 0058 so. The sort order, if applicable, is the order of functions in the 0059 .text.* sections in the resulting binary artifact. Each Function 0060 Descriptor Entry specifies the start PC of a function, the size in bytes 0061 of the function and an offset to its first Frame Row Entry (FRE). Each FDE 0062 additionally also specifies the type of FRE it uses to encode the stack 0063 trace information. 0064 0065 Next, the SFrame Frame Row Entry sub-section is a list of variable size 0066 records. Each entry represents stack trace information for a set of PCs 0067 of the function. A singular Frame Row Entry is a self-sufficient record 0068 which contains information on how to generate stack trace from the 0069 applicable set of PCs. 0070 0071 */ 0072 0073 0074 /* SFrame format versions. */ 0075 #define SFRAME_VERSION_1 1 0076 #define SFRAME_VERSION_2 2 0077 /* SFrame magic number. */ 0078 #define SFRAME_MAGIC 0xdee2 0079 /* Current version of SFrame format. */ 0080 #define SFRAME_VERSION SFRAME_VERSION_2 0081 0082 /* Various flags for SFrame. */ 0083 0084 /* Function Descriptor Entries are sorted on PC. */ 0085 #define SFRAME_F_FDE_SORTED 0x1 0086 /* Functions preserve frame pointer. */ 0087 #define SFRAME_F_FRAME_POINTER 0x2 0088 0089 #define SFRAME_CFA_FIXED_FP_INVALID 0 0090 #define SFRAME_CFA_FIXED_RA_INVALID 0 0091 0092 /* Supported ABIs/Arch. */ 0093 #define SFRAME_ABI_AARCH64_ENDIAN_BIG 1 /* AARCH64 big endian. */ 0094 #define SFRAME_ABI_AARCH64_ENDIAN_LITTLE 2 /* AARCH64 little endian. */ 0095 #define SFRAME_ABI_AMD64_ENDIAN_LITTLE 3 /* AMD64 little endian. */ 0096 0097 /* SFrame FRE types. */ 0098 #define SFRAME_FRE_TYPE_ADDR1 0 0099 #define SFRAME_FRE_TYPE_ADDR2 1 0100 #define SFRAME_FRE_TYPE_ADDR4 2 0101 0102 /* SFrame Function Descriptor Entry types. 0103 0104 The SFrame format has two possible representations for functions. The 0105 choice of which type to use is made according to the instruction patterns 0106 in the relevant program stub. 0107 0108 An SFrame FDE of type SFRAME_FDE_TYPE_PCINC is an indication 0109 that the PCs in the FREs should be treated as increments in bytes. This is 0110 used for a bulk of the executable code of a program, which contains 0111 instructions with no specific pattern. 0112 0113 An SFrame FDE of type SFRAME_FDE_TYPE_PCMASK is an indication 0114 that the PCs in the FREs should be treated as masks. This type is useful 0115 for the cases when a small pattern of instructions in a program stub is 0116 repeatedly to cover a specific functionality. Typical usescases are pltN 0117 entries, trampolines etc. */ 0118 0119 /* Unwinders perform a (PC >= FRE_START_ADDR) to look up a matching FRE. */ 0120 #define SFRAME_FDE_TYPE_PCINC 0 0121 /* Unwinders perform a (PC % REP_BLOCK_SIZE >= FRE_START_ADDR) to look up a 0122 matching FRE. */ 0123 #define SFRAME_FDE_TYPE_PCMASK 1 0124 0125 typedef struct sframe_preamble 0126 { 0127 uint16_t sfp_magic; /* Magic number (SFRAME_MAGIC). */ 0128 uint8_t sfp_version; /* Data format version number (SFRAME_VERSION). */ 0129 uint8_t sfp_flags; /* Flags. */ 0130 } ATTRIBUTE_PACKED sframe_preamble; 0131 0132 typedef struct sframe_header 0133 { 0134 sframe_preamble sfh_preamble; 0135 /* Information about the arch (endianness) and ABI. */ 0136 uint8_t sfh_abi_arch; 0137 /* Offset for the Frame Pointer (FP) from CFA may be fixed for some 0138 ABIs (e.g, in AMD64 when -fno-omit-frame-pointer is used). When fixed, 0139 this field specifies the fixed stack frame offset and the individual 0140 FREs do not need to track it. When not fixed, it is set to 0141 SFRAME_CFA_FIXED_FP_INVALID, and the individual FREs may provide 0142 the applicable stack frame offset, if any. */ 0143 int8_t sfh_cfa_fixed_fp_offset; 0144 /* Offset for the Return Address from CFA is fixed for some ABIs 0145 (e.g., AMD64 has it as CFA-8). When fixed, the header specifies the 0146 fixed stack frame offset and the individual FREs do not track it. When 0147 not fixed, it is set to SFRAME_CFA_FIXED_RA_INVALID, and individual 0148 FREs provide the applicable stack frame offset, if any. */ 0149 int8_t sfh_cfa_fixed_ra_offset; 0150 /* Number of bytes making up the auxiliary header, if any. 0151 Some ABI/arch, in the future, may use this space for extending the 0152 information in SFrame header. Auxiliary header is contained in 0153 bytes sequentially following the sframe_header. */ 0154 uint8_t sfh_auxhdr_len; 0155 /* Number of SFrame FDEs in this SFrame section. */ 0156 uint32_t sfh_num_fdes; 0157 /* Number of SFrame Frame Row Entries. */ 0158 uint32_t sfh_num_fres; 0159 /* Number of bytes in the SFrame Frame Row Entry section. */ 0160 uint32_t sfh_fre_len; 0161 /* Offset of SFrame Function Descriptor Entry section. */ 0162 uint32_t sfh_fdeoff; 0163 /* Offset of SFrame Frame Row Entry section. */ 0164 uint32_t sfh_freoff; 0165 } ATTRIBUTE_PACKED sframe_header; 0166 0167 #define SFRAME_V1_HDR_SIZE(sframe_hdr) \ 0168 ((sizeof (sframe_header) + (sframe_hdr).sfh_auxhdr_len)) 0169 0170 /* Two possible keys for executable (instruction) pointers signing. */ 0171 #define SFRAME_AARCH64_PAUTH_KEY_A 0 /* Key A. */ 0172 #define SFRAME_AARCH64_PAUTH_KEY_B 1 /* Key B. */ 0173 0174 typedef struct sframe_func_desc_entry 0175 { 0176 /* Function start address. Encoded as a signed offset, relative to the 0177 beginning of the current FDE. */ 0178 int32_t sfde_func_start_address; 0179 /* Size of the function in bytes. */ 0180 uint32_t sfde_func_size; 0181 /* Offset of the first SFrame Frame Row Entry of the function, relative to the 0182 beginning of the SFrame Frame Row Entry sub-section. */ 0183 uint32_t sfde_func_start_fre_off; 0184 /* Number of frame row entries for the function. */ 0185 uint32_t sfde_func_num_fres; 0186 /* Additional information for stack tracing from the function: 0187 - 4-bits: Identify the FRE type used for the function. 0188 - 1-bit: Identify the FDE type of the function - mask or inc. 0189 - 1-bit: PAC authorization A/B key (aarch64). 0190 - 2-bits: Unused. 0191 ------------------------------------------------------------------------ 0192 | Unused | PAC auth A/B key (aarch64) | FDE type | FRE type | 0193 | | Unused (amd64) | | | 0194 ------------------------------------------------------------------------ 0195 8 6 5 4 0 */ 0196 uint8_t sfde_func_info; 0197 /* Size of the block of repeating insns. Used for SFrame FDEs of type 0198 SFRAME_FDE_TYPE_PCMASK. */ 0199 uint8_t sfde_func_rep_size; 0200 uint16_t sfde_func_padding2; 0201 } ATTRIBUTE_PACKED sframe_func_desc_entry; 0202 0203 /* Macros to compose and decompose function info in FDE. */ 0204 0205 /* Note: Set PAC auth key to SFRAME_AARCH64_PAUTH_KEY_A by default. */ 0206 #define SFRAME_V1_FUNC_INFO(fde_type, fre_enc_type) \ 0207 (((SFRAME_AARCH64_PAUTH_KEY_A & 0x1) << 5) | \ 0208 (((fde_type) & 0x1) << 4) | ((fre_enc_type) & 0xf)) 0209 0210 #define SFRAME_V1_FUNC_FRE_TYPE(data) ((data) & 0xf) 0211 #define SFRAME_V1_FUNC_FDE_TYPE(data) (((data) >> 4) & 0x1) 0212 #define SFRAME_V1_FUNC_PAUTH_KEY(data) (((data) >> 5) & 0x1) 0213 0214 /* Set the pauth key as indicated. */ 0215 #define SFRAME_V1_FUNC_INFO_UPDATE_PAUTH_KEY(pauth_key, fde_info) \ 0216 ((((pauth_key) & 0x1) << 5) | ((fde_info) & 0xdf)) 0217 0218 /* Size of stack frame offsets in an SFrame Frame Row Entry. A single 0219 SFrame FRE has all offsets of the same size. Offset size may vary 0220 across frame row entries. */ 0221 #define SFRAME_FRE_OFFSET_1B 0 0222 #define SFRAME_FRE_OFFSET_2B 1 0223 #define SFRAME_FRE_OFFSET_4B 2 0224 0225 /* An SFrame Frame Row Entry can be SP or FP based. */ 0226 #define SFRAME_BASE_REG_FP 0 0227 #define SFRAME_BASE_REG_SP 1 0228 0229 /* The index at which a specific offset is presented in the variable length 0230 bytes of an FRE. */ 0231 #define SFRAME_FRE_CFA_OFFSET_IDX 0 0232 /* The RA stack offset, if present, will always be at index 1 in the variable 0233 length bytes of the FRE. */ 0234 #define SFRAME_FRE_RA_OFFSET_IDX 1 0235 /* The FP stack offset may appear at offset 1 or 2, depending on the ABI as RA 0236 may or may not be tracked. */ 0237 #define SFRAME_FRE_FP_OFFSET_IDX 2 0238 0239 typedef struct sframe_fre_info 0240 { 0241 /* Information about 0242 - 1 bit: base reg for CFA 0243 - 4 bits: Number of offsets (N). A value of upto 3 is allowed to track 0244 all three of CFA, FP and RA (fixed implicit order). 0245 - 2 bits: information about size of the offsets (S) in bytes. 0246 Valid values are SFRAME_FRE_OFFSET_1B, SFRAME_FRE_OFFSET_2B, 0247 SFRAME_FRE_OFFSET_4B 0248 - 1 bit: Mangled RA state bit (aarch64 only). 0249 ---------------------------------------------------------------------------------- 0250 | Mangled-RA (aarch64) | Size of offsets | Number of offsets | base_reg | 0251 | Unused (amd64) | | | | 0252 ---------------------------------------------------------------------------------- 0253 8 7 5 1 0 0254 0255 */ 0256 uint8_t fre_info; 0257 } sframe_fre_info; 0258 0259 /* Macros to compose and decompose FRE info. */ 0260 0261 /* Note: Set mangled_ra_p to zero by default. */ 0262 #define SFRAME_V1_FRE_INFO(base_reg_id, offset_num, offset_size) \ 0263 (((0 & 0x1) << 7) | (((offset_size) & 0x3) << 5) | \ 0264 (((offset_num) & 0xf) << 1) | ((base_reg_id) & 0x1)) 0265 0266 /* Set the mangled_ra_p bit as indicated. */ 0267 #define SFRAME_V1_FRE_INFO_UPDATE_MANGLED_RA_P(mangled_ra_p, fre_info) \ 0268 ((((mangled_ra_p) & 0x1) << 7) | ((fre_info) & 0x7f)) 0269 0270 #define SFRAME_V1_FRE_CFA_BASE_REG_ID(data) ((data) & 0x1) 0271 #define SFRAME_V1_FRE_OFFSET_COUNT(data) (((data) >> 1) & 0xf) 0272 #define SFRAME_V1_FRE_OFFSET_SIZE(data) (((data) >> 5) & 0x3) 0273 #define SFRAME_V1_FRE_MANGLED_RA_P(data) (((data) >> 7) & 0x1) 0274 0275 /* SFrame Frame Row Entry definitions. 0276 0277 Used for both AMD64 and AARCH64. 0278 0279 An SFrame Frame Row Entry is a self-sufficient record which contains 0280 information on how to generate the stack trace for the specified range of 0281 PCs. Each SFrame Frame Row Entry is followed by S*N bytes, where: 0282 S is the size of the stack frame offset for the FRE, and 0283 N is the number of stack frame offsets in the FRE 0284 0285 The interpretation of FRE stack offsets is ABI-specific: 0286 0287 AMD64: 0288 offset1 (interpreted as CFA = BASE_REG + offset1) 0289 if FP is being tracked 0290 offset2 (intrepreted as FP = CFA + offset2) 0291 fi 0292 0293 AARCH64: 0294 offset1 (interpreted as CFA = BASE_REG + offset1) 0295 if FP is being tracked (in other words, if frame record created) 0296 offset2 (interpreted as RA = CFA + offset2) 0297 offset3 (intrepreted as FP = CFA + offset3) 0298 fi 0299 Note that in AAPCS64, a frame record, if created, will save both FP and 0300 LR on stack. 0301 */ 0302 0303 /* Used when SFRAME_FRE_TYPE_ADDR1 is specified as FRE type. */ 0304 typedef struct sframe_frame_row_entry_addr1 0305 { 0306 /* Start address of the frame row entry. Encoded as an 1-byte unsigned 0307 offset, relative to the start address of the function. */ 0308 uint8_t sfre_start_address; 0309 sframe_fre_info sfre_info; 0310 } ATTRIBUTE_PACKED sframe_frame_row_entry_addr1; 0311 0312 /* Upper limit of start address in sframe_frame_row_entry_addr1 0313 is 0x100 (not inclusive). */ 0314 #define SFRAME_FRE_TYPE_ADDR1_LIMIT \ 0315 (1ULL << ((SFRAME_FRE_TYPE_ADDR1 + 1) * 8)) 0316 0317 /* Used when SFRAME_FRE_TYPE_ADDR2 is specified as FRE type. */ 0318 typedef struct sframe_frame_row_entry_addr2 0319 { 0320 /* Start address of the frame row entry. Encoded as an 2-byte unsigned 0321 offset, relative to the start address of the function. */ 0322 uint16_t sfre_start_address; 0323 sframe_fre_info sfre_info; 0324 } ATTRIBUTE_PACKED sframe_frame_row_entry_addr2; 0325 0326 /* Upper limit of start address in sframe_frame_row_entry_addr2 0327 is 0x10000 (not inclusive). */ 0328 #define SFRAME_FRE_TYPE_ADDR2_LIMIT \ 0329 (1ULL << ((SFRAME_FRE_TYPE_ADDR2 * 2) * 8)) 0330 0331 /* Used when SFRAME_FRE_TYPE_ADDR4 is specified as FRE type. */ 0332 typedef struct sframe_frame_row_entry_addr4 0333 { 0334 /* Start address of the frame row entry. Encoded as a 4-byte unsigned 0335 offset, relative to the start address of the function. */ 0336 uint32_t sfre_start_address; 0337 sframe_fre_info sfre_info; 0338 } ATTRIBUTE_PACKED sframe_frame_row_entry_addr4; 0339 0340 /* Upper limit of start address in sframe_frame_row_entry_addr2 0341 is 0x100000000 (not inclusive). */ 0342 #define SFRAME_FRE_TYPE_ADDR4_LIMIT \ 0343 (1ULL << ((SFRAME_FRE_TYPE_ADDR4 * 2) * 8)) 0344 0345 #ifdef __cplusplus 0346 } 0347 #endif 0348 0349 #endif /* _SFRAME_H */
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|