sframe-api.h revision 1.1.1.1.4.2 1 1.1.1.1.4.2 perseant /* Public API to SFrame.
2 1.1.1.1.4.2 perseant
3 1.1.1.1.4.2 perseant Copyright (C) 2022 Free Software Foundation, Inc.
4 1.1.1.1.4.2 perseant
5 1.1.1.1.4.2 perseant This file is part of libsframe.
6 1.1.1.1.4.2 perseant
7 1.1.1.1.4.2 perseant This program is free software; you can redistribute it and/or modify
8 1.1.1.1.4.2 perseant it under the terms of the GNU General Public License as published by
9 1.1.1.1.4.2 perseant the Free Software Foundation; either version 3 of the License, or
10 1.1.1.1.4.2 perseant (at your option) any later version.
11 1.1.1.1.4.2 perseant
12 1.1.1.1.4.2 perseant This program is distributed in the hope that it will be useful,
13 1.1.1.1.4.2 perseant but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1.1.1.4.2 perseant MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1.1.1.4.2 perseant GNU General Public License for more details.
16 1.1.1.1.4.2 perseant
17 1.1.1.1.4.2 perseant You should have received a copy of the GNU General Public License
18 1.1.1.1.4.2 perseant along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1.1.1.4.2 perseant
20 1.1.1.1.4.2 perseant #ifndef _SFRAME_API_H
21 1.1.1.1.4.2 perseant #define _SFRAME_API_H
22 1.1.1.1.4.2 perseant
23 1.1.1.1.4.2 perseant #include <sframe.h>
24 1.1.1.1.4.2 perseant #include <stdbool.h>
25 1.1.1.1.4.2 perseant
26 1.1.1.1.4.2 perseant #ifdef __cplusplus
27 1.1.1.1.4.2 perseant extern "C"
28 1.1.1.1.4.2 perseant {
29 1.1.1.1.4.2 perseant #endif
30 1.1.1.1.4.2 perseant
31 1.1.1.1.4.2 perseant typedef struct sframe_decoder_ctx sframe_decoder_ctx;
32 1.1.1.1.4.2 perseant typedef struct sframe_encoder_ctx sframe_encoder_ctx;
33 1.1.1.1.4.2 perseant
34 1.1.1.1.4.2 perseant #define MAX_OFFSET_BYTES (SFRAME_FRE_OFFSET_4B * 2 * 3)
35 1.1.1.1.4.2 perseant
36 1.1.1.1.4.2 perseant /* User interfacing SFrame Row Entry.
37 1.1.1.1.4.2 perseant An abstraction provided by libsframe so the consumer is decoupled from
38 1.1.1.1.4.2 perseant the binary format representation of the same.
39 1.1.1.1.4.2 perseant
40 1.1.1.1.4.2 perseant The members are best ordered such that they are aligned at their natural
41 1.1.1.1.4.2 perseant boundaries. This helps avoid usage of undesirable misaligned memory
42 1.1.1.1.4.2 perseant accesses. See PR libsframe/29856. */
43 1.1.1.1.4.2 perseant
44 1.1.1.1.4.2 perseant typedef struct sframe_frame_row_entry
45 1.1.1.1.4.2 perseant {
46 1.1.1.1.4.2 perseant uint32_t fre_start_addr;
47 1.1.1.1.4.2 perseant unsigned char fre_offsets[MAX_OFFSET_BYTES];
48 1.1.1.1.4.2 perseant unsigned char fre_info;
49 1.1.1.1.4.2 perseant } sframe_frame_row_entry;
50 1.1.1.1.4.2 perseant
51 1.1.1.1.4.2 perseant #define SFRAME_ERR ((int) -1)
52 1.1.1.1.4.2 perseant
53 1.1.1.1.4.2 perseant /* This macro holds information about all the available SFrame
54 1.1.1.1.4.2 perseant errors. It is used to form both an enum holding all the error
55 1.1.1.1.4.2 perseant constants, and also the error strings themselves. To use, define
56 1.1.1.1.4.2 perseant _SFRAME_FIRST and _SFRAME_ITEM to expand as you like, then
57 1.1.1.1.4.2 perseant mention the macro name. See the enum after this for an example. */
58 1.1.1.1.4.2 perseant #define _SFRAME_ERRORS \
59 1.1.1.1.4.2 perseant _SFRAME_FIRST (SFRAME_ERR_VERSION_INVAL, "SFrame version not supported.") \
60 1.1.1.1.4.2 perseant _SFRAME_ITEM (SFRAME_ERR_NOMEM, "Out of Memory.") \
61 1.1.1.1.4.2 perseant _SFRAME_ITEM (SFRAME_ERR_INVAL, "Corrupt SFrame.") \
62 1.1.1.1.4.2 perseant _SFRAME_ITEM (SFRAME_ERR_BUF_INVAL, "Buffer does not contain SFrame data.") \
63 1.1.1.1.4.2 perseant _SFRAME_ITEM (SFRAME_ERR_DCTX_INVAL, "Corrupt SFrame decoder.") \
64 1.1.1.1.4.2 perseant _SFRAME_ITEM (SFRAME_ERR_ECTX_INVAL, "Corrupt SFrame encoder.") \
65 1.1.1.1.4.2 perseant _SFRAME_ITEM (SFRAME_ERR_FDE_INVAL, "Corrput FDE.") \
66 1.1.1.1.4.2 perseant _SFRAME_ITEM (SFRAME_ERR_FRE_INVAL, "Corrupt FRE.") \
67 1.1.1.1.4.2 perseant _SFRAME_ITEM (SFRAME_ERR_FDE_NOTFOUND,"FDE not found.") \
68 1.1.1.1.4.2 perseant _SFRAME_ITEM (SFRAME_ERR_FDE_NOTSORTED, "FDEs not sorted.") \
69 1.1.1.1.4.2 perseant _SFRAME_ITEM (SFRAME_ERR_FRE_NOTFOUND,"FRE not found.") \
70 1.1.1.1.4.2 perseant _SFRAME_ITEM (SFRAME_ERR_FREOFFSET_NOPRESENT,"FRE offset not present.")
71 1.1.1.1.4.2 perseant
72 1.1.1.1.4.2 perseant #define SFRAME_ERR_BASE 2000 /* Base value for libsframe errnos. */
73 1.1.1.1.4.2 perseant
74 1.1.1.1.4.2 perseant enum
75 1.1.1.1.4.2 perseant {
76 1.1.1.1.4.2 perseant #define _SFRAME_FIRST(NAME, STR) NAME = SFRAME_ERR_BASE
77 1.1.1.1.4.2 perseant #define _SFRAME_ITEM(NAME, STR) , NAME
78 1.1.1.1.4.2 perseant _SFRAME_ERRORS
79 1.1.1.1.4.2 perseant #undef _SFRAME_ITEM
80 1.1.1.1.4.2 perseant #undef _SFRAME_FIRST
81 1.1.1.1.4.2 perseant };
82 1.1.1.1.4.2 perseant
83 1.1.1.1.4.2 perseant /* Count of SFrame errors. */
84 1.1.1.1.4.2 perseant #define SFRAME_ERR_NERR (SFRAME_ERR_FREOFFSET_NOPRESENT - SFRAME_ERR_BASE + 1)
85 1.1.1.1.4.2 perseant
86 1.1.1.1.4.2 perseant /* Get the error message string. */
87 1.1.1.1.4.2 perseant
88 1.1.1.1.4.2 perseant extern const char *
89 1.1.1.1.4.2 perseant sframe_errmsg (int error);
90 1.1.1.1.4.2 perseant
91 1.1.1.1.4.2 perseant /* Create an FDE function info bye given an FRE_TYPE and an FDE_TYPE. */
92 1.1.1.1.4.2 perseant
93 1.1.1.1.4.2 perseant extern unsigned char
94 1.1.1.1.4.2 perseant sframe_fde_create_func_info (unsigned int fre_type, unsigned int fde_type);
95 1.1.1.1.4.2 perseant
96 1.1.1.1.4.2 perseant /* Gather the FRE type given the function size. */
97 1.1.1.1.4.2 perseant
98 1.1.1.1.4.2 perseant extern unsigned int
99 1.1.1.1.4.2 perseant sframe_calc_fre_type (unsigned int func_size);
100 1.1.1.1.4.2 perseant
101 1.1.1.1.4.2 perseant /* The SFrame Decoder. */
102 1.1.1.1.4.2 perseant
103 1.1.1.1.4.2 perseant /* Decode the specified SFrame buffer CF_BUF of size CF_SIZE and return the
104 1.1.1.1.4.2 perseant new SFrame decoder context. Sets ERRP for the caller if any error. */
105 1.1.1.1.4.2 perseant extern sframe_decoder_ctx *
106 1.1.1.1.4.2 perseant sframe_decode (const char *cf_buf, size_t cf_size, int *errp);
107 1.1.1.1.4.2 perseant
108 1.1.1.1.4.2 perseant /* Free the decoder context. */
109 1.1.1.1.4.2 perseant extern void
110 1.1.1.1.4.2 perseant sframe_decoder_free (sframe_decoder_ctx **dctx);
111 1.1.1.1.4.2 perseant
112 1.1.1.1.4.2 perseant /* Get the size of the SFrame header from the decoder context DCTX. */
113 1.1.1.1.4.2 perseant extern unsigned int
114 1.1.1.1.4.2 perseant sframe_decoder_get_hdr_size (sframe_decoder_ctx *dctx);
115 1.1.1.1.4.2 perseant
116 1.1.1.1.4.2 perseant /* Get the SFrame's abi/arch info. */
117 1.1.1.1.4.2 perseant extern unsigned char
118 1.1.1.1.4.2 perseant sframe_decoder_get_abi_arch (sframe_decoder_ctx *dctx);
119 1.1.1.1.4.2 perseant
120 1.1.1.1.4.2 perseant /* Return the number of function descriptor entries in the SFrame decoder
121 1.1.1.1.4.2 perseant DCTX. */
122 1.1.1.1.4.2 perseant unsigned int
123 1.1.1.1.4.2 perseant sframe_decoder_get_num_fidx (sframe_decoder_ctx *dctx);
124 1.1.1.1.4.2 perseant
125 1.1.1.1.4.2 perseant /* Get the fixed FP offset from the decoder context DCTX. */
126 1.1.1.1.4.2 perseant extern int8_t
127 1.1.1.1.4.2 perseant sframe_decoder_get_fixed_fp_offset (sframe_decoder_ctx *dctx);
128 1.1.1.1.4.2 perseant
129 1.1.1.1.4.2 perseant /* Get the fixed RA offset from the decoder context DCTX. */
130 1.1.1.1.4.2 perseant extern int8_t
131 1.1.1.1.4.2 perseant sframe_decoder_get_fixed_ra_offset (sframe_decoder_ctx *dctx);
132 1.1.1.1.4.2 perseant
133 1.1.1.1.4.2 perseant /* Find the function descriptor entry which contains the specified address. */
134 1.1.1.1.4.2 perseant extern sframe_func_desc_entry *
135 1.1.1.1.4.2 perseant sframe_get_funcdesc_with_addr (sframe_decoder_ctx *dctx,
136 1.1.1.1.4.2 perseant int32_t addr, int *errp);
137 1.1.1.1.4.2 perseant
138 1.1.1.1.4.2 perseant /* Find the SFrame Frame Row Entry which contains the PC. Returns
139 1.1.1.1.4.2 perseant SFRAME_ERR if failure. */
140 1.1.1.1.4.2 perseant
141 1.1.1.1.4.2 perseant extern int
142 1.1.1.1.4.2 perseant sframe_find_fre (sframe_decoder_ctx *ctx, int32_t pc,
143 1.1.1.1.4.2 perseant sframe_frame_row_entry *frep);
144 1.1.1.1.4.2 perseant
145 1.1.1.1.4.2 perseant /* Get the FRE_IDX'th FRE of the function at FUNC_IDX'th function
146 1.1.1.1.4.2 perseant index entry in the SFrame decoder CTX. Returns error code as
147 1.1.1.1.4.2 perseant applicable. */
148 1.1.1.1.4.2 perseant extern int
149 1.1.1.1.4.2 perseant sframe_decoder_get_fre (sframe_decoder_ctx *ctx,
150 1.1.1.1.4.2 perseant unsigned int func_idx,
151 1.1.1.1.4.2 perseant unsigned int fre_idx,
152 1.1.1.1.4.2 perseant sframe_frame_row_entry *fre);
153 1.1.1.1.4.2 perseant
154 1.1.1.1.4.2 perseant /* Get the data (NUM_FRES, FUNC_START_ADDRESS) from the function
155 1.1.1.1.4.2 perseant descriptor entry at index I'th in the decoder CTX. If failed,
156 1.1.1.1.4.2 perseant return error code. */
157 1.1.1.1.4.2 perseant extern int
158 1.1.1.1.4.2 perseant sframe_decoder_get_funcdesc (sframe_decoder_ctx *ctx,
159 1.1.1.1.4.2 perseant unsigned int i,
160 1.1.1.1.4.2 perseant uint32_t *num_fres,
161 1.1.1.1.4.2 perseant uint32_t *func_size,
162 1.1.1.1.4.2 perseant int32_t *func_start_address,
163 1.1.1.1.4.2 perseant unsigned char *func_info);
164 1.1.1.1.4.2 perseant
165 1.1.1.1.4.2 perseant /* SFrame textual dump. */
166 1.1.1.1.4.2 perseant extern void
167 1.1.1.1.4.2 perseant dump_sframe (sframe_decoder_ctx *decoder, uint64_t addr);
168 1.1.1.1.4.2 perseant
169 1.1.1.1.4.2 perseant /* Get the base reg id from the FRE info. Sets errp if fails. */
170 1.1.1.1.4.2 perseant extern unsigned int
171 1.1.1.1.4.2 perseant sframe_fre_get_base_reg_id (sframe_frame_row_entry *fre, int *errp);
172 1.1.1.1.4.2 perseant
173 1.1.1.1.4.2 perseant /* Get the CFA offset from the FRE. If the offset is invalid, sets errp. */
174 1.1.1.1.4.2 perseant extern int32_t
175 1.1.1.1.4.2 perseant sframe_fre_get_cfa_offset (sframe_decoder_ctx *dtcx,
176 1.1.1.1.4.2 perseant sframe_frame_row_entry *fre, int *errp);
177 1.1.1.1.4.2 perseant
178 1.1.1.1.4.2 perseant /* Get the FP offset from the FRE. If the offset is invalid, sets errp. */
179 1.1.1.1.4.2 perseant extern int32_t
180 1.1.1.1.4.2 perseant sframe_fre_get_fp_offset (sframe_decoder_ctx *dctx,
181 1.1.1.1.4.2 perseant sframe_frame_row_entry *fre, int *errp);
182 1.1.1.1.4.2 perseant
183 1.1.1.1.4.2 perseant /* Get the RA offset from the FRE. If the offset is invalid, sets errp. */
184 1.1.1.1.4.2 perseant extern int32_t
185 1.1.1.1.4.2 perseant sframe_fre_get_ra_offset (sframe_decoder_ctx *dctx,
186 1.1.1.1.4.2 perseant sframe_frame_row_entry *fre, int *errp);
187 1.1.1.1.4.2 perseant
188 1.1.1.1.4.2 perseant /* Get whether the RA is mangled. */
189 1.1.1.1.4.2 perseant
190 1.1.1.1.4.2 perseant extern bool
191 1.1.1.1.4.2 perseant sframe_fre_get_ra_mangled_p (sframe_decoder_ctx *dctx,
192 1.1.1.1.4.2 perseant sframe_frame_row_entry *fre, int *errp);
193 1.1.1.1.4.2 perseant
194 1.1.1.1.4.2 perseant /* The SFrame Encoder. */
195 1.1.1.1.4.2 perseant
196 1.1.1.1.4.2 perseant /* Create an encoder context with the given SFrame format version VER, FLAGS
197 1.1.1.1.4.2 perseant and ABI information. Sets errp if failure. */
198 1.1.1.1.4.2 perseant extern sframe_encoder_ctx *
199 1.1.1.1.4.2 perseant sframe_encode (unsigned char ver, unsigned char flags, int abi,
200 1.1.1.1.4.2 perseant int8_t fixed_fp_offset, int8_t fixed_ra_offset, int *errp);
201 1.1.1.1.4.2 perseant
202 1.1.1.1.4.2 perseant /* Free the encoder context. */
203 1.1.1.1.4.2 perseant extern void
204 1.1.1.1.4.2 perseant sframe_encoder_free (sframe_encoder_ctx **encoder);
205 1.1.1.1.4.2 perseant
206 1.1.1.1.4.2 perseant /* Get the size of the SFrame header from the encoder ctx ENCODER. */
207 1.1.1.1.4.2 perseant extern unsigned int
208 1.1.1.1.4.2 perseant sframe_encoder_get_hdr_size (sframe_encoder_ctx *encoder);
209 1.1.1.1.4.2 perseant
210 1.1.1.1.4.2 perseant /* Get the abi/arch info from the SFrame encoder context CTX. */
211 1.1.1.1.4.2 perseant extern unsigned char
212 1.1.1.1.4.2 perseant sframe_encoder_get_abi_arch (sframe_encoder_ctx *encoder);
213 1.1.1.1.4.2 perseant
214 1.1.1.1.4.2 perseant /* Return the number of function descriptor entries in the SFrame encoder
215 1.1.1.1.4.2 perseant ENCODER. */
216 1.1.1.1.4.2 perseant extern unsigned int
217 1.1.1.1.4.2 perseant sframe_encoder_get_num_fidx (sframe_encoder_ctx *encoder);
218 1.1.1.1.4.2 perseant
219 1.1.1.1.4.2 perseant /* Add an FRE to function at FUNC_IDX'th function descriptor index entry in
220 1.1.1.1.4.2 perseant the encoder context. */
221 1.1.1.1.4.2 perseant extern int
222 1.1.1.1.4.2 perseant sframe_encoder_add_fre (sframe_encoder_ctx *encoder,
223 1.1.1.1.4.2 perseant unsigned int func_idx,
224 1.1.1.1.4.2 perseant sframe_frame_row_entry *frep);
225 1.1.1.1.4.2 perseant
226 1.1.1.1.4.2 perseant /* Add a new function descriptor entry with START_ADDR, FUNC_SIZE and NUM_FRES
227 1.1.1.1.4.2 perseant to the encoder. */
228 1.1.1.1.4.2 perseant extern int
229 1.1.1.1.4.2 perseant sframe_encoder_add_funcdesc (sframe_encoder_ctx *encoder,
230 1.1.1.1.4.2 perseant int32_t start_addr,
231 1.1.1.1.4.2 perseant uint32_t func_size,
232 1.1.1.1.4.2 perseant unsigned char func_info,
233 1.1.1.1.4.2 perseant uint32_t num_fres);
234 1.1.1.1.4.2 perseant
235 1.1.1.1.4.2 perseant /* Serialize the contents of the encoder and return the buffer. ENCODED_SIZE
236 1.1.1.1.4.2 perseant is updated to the size of the buffer. Sets ERRP if failure. */
237 1.1.1.1.4.2 perseant extern char *
238 1.1.1.1.4.2 perseant sframe_encoder_write (sframe_encoder_ctx *encoder,
239 1.1.1.1.4.2 perseant size_t *encoded_size, int *errp);
240 1.1.1.1.4.2 perseant
241 1.1.1.1.4.2 perseant #ifdef __cplusplus
242 1.1.1.1.4.2 perseant }
243 1.1.1.1.4.2 perseant #endif
244 1.1.1.1.4.2 perseant
245 1.1.1.1.4.2 perseant #endif /* _SFRAME_API_H */
246