1 /*        $OpenBSD: ctf.h,v 1.5 2017/08/13 14:56:05 nayden Exp $      */
2 
3 /*-
4  * SPDX-License-Identifier: ISC
5  *
6  * Copyright (c) 2016 Martin Pieuchot <mpi@openbsd.org>
7  * Copyright (c) 2022 The FreeBSD Foundation
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  */
21 
22 #ifndef _CTF_H_
23 #define _CTF_H_
24 
25 #include <sys/types.h>
26 
27 /*
28  * CTF ``Compact ANSI-C Type Format'' ABI header file.
29  *
30  * See the ctf(5) manual page for a detailed description of the format.
31  */
32 
33 typedef struct ctf_preamble {
34           __uint16_t                    ctp_magic;
35           __uint8_t           ctp_version;
36           __uint8_t           ctp_flags;
37 } ctf_preamble_t;
38 
39 typedef struct ctf_header {
40           struct ctf_preamble cth_preamble;
41 #define cth_magic   cth_preamble.ctp_magic
42 #define cth_version cth_preamble.ctp_version
43 #define cth_flags   cth_preamble.ctp_flags
44           __uint32_t                    cth_parlabel;
45           __uint32_t                    cth_parname;
46           __uint32_t                    cth_lbloff;
47           __uint32_t                    cth_objtoff;
48           __uint32_t                    cth_funcoff;
49           __uint32_t                    cth_typeoff;
50           __uint32_t                    cth_stroff;
51           __uint32_t                    cth_strlen;
52 } ctf_header_t;
53 
54 #define CTF_F_COMPRESS                  (1 << 0)  /* zlib compression */
55 
56 typedef struct ctf_lblent {
57           __uint32_t                    ctl_label;
58           __uint32_t                    ctl_typeidx;
59 } ctf_lblent_t;
60 
61 struct ctf_stype_v2 {
62           __uint32_t                    ctt_name;
63           __uint16_t                    ctt_info;
64           union {
65                     __uint16_t _size;
66                     __uint16_t _type;
67           } _u;
68 };
69 
70 struct ctf_stype_v3 {
71           __uint32_t                    ctt_name;
72           __uint32_t                    ctt_info;
73           union {
74                     __uint32_t _size;
75                     __uint32_t _type;
76           } _u;
77 };
78 
79 struct ctf_type_v2 {
80           __uint32_t                    ctt_name;
81           __uint16_t                    ctt_info;
82           union {
83                     __uint16_t _size;
84                     __uint16_t _type;
85           } _u;
86           __uint32_t                    ctt_lsizehi;
87           __uint32_t                    ctt_lsizelo;
88 };
89 
90 struct ctf_type_v3 {
91           __uint32_t                    ctt_name;
92           __uint32_t                    ctt_info;
93           union {
94                     __uint32_t _size;
95                     __uint32_t _type;
96           } _u;
97           __uint32_t                    ctt_lsizehi;
98           __uint32_t                    ctt_lsizelo;
99 };
100 
101 #define ctt_size _u._size
102 #define ctt_type _u._type
103 
104 struct ctf_array_v2 {
105           __uint16_t                    cta_contents;
106           __uint16_t                    cta_index;
107           __uint32_t                    cta_nelems;
108 };
109 
110 struct ctf_array_v3 {
111           __uint32_t                    cta_contents;
112           __uint32_t                    cta_index;
113           __uint32_t                    cta_nelems;
114 };
115 
116 struct ctf_member_v2 {
117           __uint32_t                    ctm_name;
118           __uint16_t                    ctm_type;
119           __uint16_t                    ctm_offset;
120 };
121 
122 struct ctf_member_v3 {
123           __uint32_t                    ctm_name;
124           __uint32_t                    ctm_type;
125           __uint32_t                    ctm_offset;
126 };
127 
128 struct ctf_lmember_v2 {
129           __uint32_t                    ctlm_name;
130           __uint16_t                    ctlm_type;
131           __uint16_t                    ctlm_pad;
132           __uint32_t                    ctlm_offsethi;
133           __uint32_t                    ctlm_offsetlo;
134 };
135 
136 struct ctf_lmember_v3 {
137           __uint32_t                    ctlm_name;
138           __uint32_t                    ctlm_type;
139           __uint32_t                    ctlm_offsethi;
140           __uint32_t                    ctlm_offsetlo;
141 };
142 
143 #define CTF_V2_LSTRUCT_THRESH (1 << 13)
144 #define CTF_V3_LSTRUCT_THRESH (1 << 29)
145 
146 typedef struct ctf_enum {
147           __uint32_t                    cte_name;
148           __int32_t           cte_value;
149 } ctf_enum_t;
150 
151 #define CTF_MAGIC             0xcff1
152 #define CTF_VERSION           CTF_VERSION_3
153 #define CTF_VERSION_3                   3
154 #define CTF_VERSION_2                   2
155 #define CTF_VERSION_1                   1
156 
157 #define CTF_MAX_NAME                    0x7fffffff
158 
159 #define CTF_V2_MAX_VLEN                 0x03ff
160 #define CTF_V2_MAX_SIZE                 0xfffe
161 #define CTF_V2_LSIZE_SENT     (CTF_V2_MAX_SIZE + 1) /* sentinel for cts vs ctt */
162 
163 #define CTF_V3_MAX_VLEN                 0x00ffffff
164 #define CTF_V3_MAX_SIZE                 0xfffffffeu
165 #define CTF_V3_LSIZE_SENT     (CTF_V3_MAX_SIZE + 1)
166 
167 #define CTF_V2_PARENT_SHIFT             15
168 #define CTF_V2_MAX_TYPE                           0xffff
169 #define CTF_V2_TYPE_ISPARENT(id)        ((id) < 0x8000)
170 #define CTF_V2_TYPE_ISCHILD(id)                   ((id) > 0x7fff)
171 #define CTF_V2_TYPE_TO_INDEX(type)      ((type) & 0x7fff)
172 #define CTF_V2_INDEX_TO_TYPE(type, ischild)                           \
173           (((type) & 0x7fff) | ((ischild) != 0 ? 0x8000 : 0))
174 #define CTF_V2_TYPE_INFO(kind, isroot, vlen)                          \
175           (((kind) << 11) | ((isroot) != 0 ? (1 << 10) : 0) |         \
176               ((vlen) & CTF_V2_MAX_VLEN))
177 
178 #define CTF_V3_PARENT_SHIFT             31
179 #define CTF_V3_MAX_TYPE                           0xfffffffeu
180 #define CTF_V3_TYPE_ISPARENT(id)        ((__uint32_t)(id) < 0x80000000u)
181 #define CTF_V3_TYPE_ISCHILD(id)                   ((__uint32_t)(id) > 0x7fffffffu)
182 #define CTF_V3_TYPE_TO_INDEX(type)      ((type) & 0x7fffffffu)
183 #define CTF_V3_INDEX_TO_TYPE(type, ischild)                           \
184           (((type) & 0x7fffffffu) | ((ischild) != 0 ? 0x80000000u : 0))
185 #define CTF_V3_TYPE_INFO(kind, isroot, vlen)                          \
186           (((kind) << 26) | ((isroot) != 0 ? (1 << 25) : 0) |         \
187               ((vlen) & CTF_V3_MAX_VLEN))
188 
189 #define CTF_STRTAB_0                    0
190 #define CTF_STRTAB_1                    1
191 
192 #define CTF_TYPE_NAME(t, o)   (((t) << 31) | ((o) & ((1u << 31) - 1)))
193 
194 /*
195  * Info macro.
196  */
197 #define CTF_V2_INFO_VLEN(i)   ((i) & CTF_V2_MAX_VLEN)
198 #define CTF_V2_INFO_ISROOT(i) (((i) & 0x0400) >> 10)
199 #define CTF_V2_INFO_KIND(i)   (((i) & 0xf800) >> 11)
200 
201 #define CTF_V3_INFO_VLEN(i)   ((i) & CTF_V3_MAX_VLEN)
202 #define CTF_V3_INFO_ISROOT(i) (((i) & 0x02000000) >> 25)
203 #define CTF_V3_INFO_KIND(i)   (((i) & 0xfc000000) >> 26)
204 
205 #define  CTF_K_UNKNOWN                  0
206 #define  CTF_K_INTEGER                  1
207 #define  CTF_K_FLOAT                    2
208 #define  CTF_K_POINTER                  3
209 #define  CTF_K_ARRAY                    4
210 #define  CTF_K_FUNCTION                 5
211 #define  CTF_K_STRUCT                   6
212 #define  CTF_K_UNION                    7
213 #define  CTF_K_ENUM           8
214 #define  CTF_K_FORWARD                  9
215 #define  CTF_K_TYPEDEF                  10
216 #define  CTF_K_VOLATILE                 11
217 #define  CTF_K_CONST                    12
218 #define  CTF_K_RESTRICT                 13
219 #define  CTF_K_MAX            63
220 
221 /*
222  * Integer/Float Encoding macro.
223  */
224 #define _CTF_ENCODING(e)      (((e) & 0xff000000) >> 24)
225 #define _CTF_OFFSET(e)                  (((e) & 0x00ff0000) >> 16)
226 #define _CTF_BITS(e)                    (((e) & 0x0000ffff))
227 #define _CTF_DATA(encoding, offset, bits) \
228           (((encoding) << 24) | ((offset) << 16) | (bits))
229 
230 #define CTF_INT_ENCODING(e)   _CTF_ENCODING(e)
231 #define  CTF_INT_SIGNED                 (1 << 0)
232 #define  CTF_INT_CHAR                   (1 << 1)
233 #define  CTF_INT_BOOL                   (1 << 2)
234 #define  CTF_INT_VARARGS      (1 << 3)
235 #define CTF_INT_OFFSET(e)     _CTF_OFFSET(e)
236 #define CTF_INT_BITS(e)                 _CTF_BITS(e)
237 #define CTF_INT_DATA(e, o, b) _CTF_DATA(e, o, b)
238 
239 #define CTF_FP_ENCODING(e)    _CTF_ENCODING(e)
240 #define CTF_FP_OFFSET(e)      _CTF_OFFSET(e)
241 #define CTF_FP_BITS(e)                  _CTF_BITS(e)
242 #define CTF_FP_DATA(e, o, b)  _CTF_DATA(e, o, b)
243 #define   CTF_FP_SINGLE       1         /* IEEE 32-bit float encoding */
244 #define   CTF_FP_DOUBLE       2         /* IEEE 64-bit float encoding */
245 #define   CTF_FP_CPLX         3         /* Complex encoding */
246 #define   CTF_FP_DCPLX        4         /* Double complex encoding */
247 #define   CTF_FP_LDCPLX       5         /* Long double complex encoding */
248 #define   CTF_FP_LDOUBLE      6         /* Long double encoding */
249 #define   CTF_FP_INTRVL       7         /* Interval (2x32-bit) encoding */
250 #define   CTF_FP_DINTRVL      8         /* Double interval (2x64-bit) encoding */
251 #define   CTF_FP_LDINTRVL     9         /* Long double interval (2x128-bit) encoding */
252 #define   CTF_FP_IMAGRY       10        /* Imaginary (32-bit) encoding */
253 #define   CTF_FP_DIMAGRY      11        /* Long imaginary (64-bit) encoding */
254 #define   CTF_FP_LDIMAGRY     12        /* Long double imaginary (128-bit) encoding */
255 #define   CTF_FP_MAX          12        /* Maximum possible CTF_FP_* value */
256 
257 /*
258  * Name reference macro.
259  */
260 #define CTF_NAME_STID(n)      ((n) >> 31)
261 #define CTF_NAME_OFFSET(n)    ((n) & CTF_MAX_NAME)
262 
263 /*
264  * Type macro.
265  */
266 #define CTF_SIZE_TO_LSIZE_HI(s)         ((uint32_t)((uint64_t)(s) >> 32))
267 #define CTF_SIZE_TO_LSIZE_LO(s)         ((uint32_t)(s))
268 #define CTF_TYPE_LSIZE(t)     \
269           (((uint64_t)(t)->ctt_lsizehi) << 32 | (t)->ctt_lsizelo)
270 
271 /*
272  * Member macro.
273  */
274 #define CTF_LMEM_OFFSET(m) \
275           (((__uint64_t)(m)->ctlm_offsethi) << 32 | (m)->ctlm_offsetlo)
276 #define CTF_OFFSET_TO_LMEMHI(off)       ((__uint32_t)((__uint64_t)(off) >> 32))
277 #define CTF_OFFSET_TO_LMEMLO(off)       ((__uint32_t)(off))
278 
279 /*
280  * Compatibility for pre-v3 code.
281  */
282 typedef struct ctf_array_v2 ctf_array_t;
283 typedef struct ctf_member_v2 ctf_member_t;
284 typedef struct ctf_lmember_v2 ctf_lmember_t;
285 typedef struct ctf_type_v2 ctf_type_t;
286 typedef struct ctf_stype_v2 ctf_stype_t;
287 
288 #define CTF_INFO_KIND                   CTF_V2_INFO_KIND
289 #define CTF_INFO_VLEN                   CTF_V2_INFO_VLEN
290 #define CTF_INFO_ISROOT                 CTF_V2_INFO_ISROOT
291 #define CTF_TYPE_INFO                   CTF_V2_TYPE_INFO
292 #define CTF_TYPE_ISPARENT     CTF_V2_TYPE_ISPARENT
293 #define CTF_TYPE_ISCHILD      CTF_V2_TYPE_ISCHILD
294 #define CTF_TYPE_TO_INDEX     CTF_V2_TYPE_TO_INDEX
295 #define CTF_INDEX_TO_TYPE     CTF_V2_INDEX_TO_TYPE
296 #define CTF_LSIZE_SENT                  CTF_V2_LSIZE_SENT
297 #define CTF_LSTRUCT_THRESH    CTF_V2_LSTRUCT_THRESH
298 #define CTF_MAX_SIZE                    CTF_V2_MAX_SIZE
299 #define CTF_MAX_TYPE                    CTF_V2_MAX_TYPE
300 #define CTF_MAX_VLEN                    CTF_V2_MAX_VLEN
301 
302 #endif /* _CTF_H_ */
303