xref: /dflybsd-src/contrib/binutils-2.34/include/ctf.h (revision b52ef7118d1621abed722c5bbbd542210290ecef)
1*fae548d3Szrj /* CTF format description.
2*fae548d3Szrj    Copyright (C) 2019-2020 Free Software Foundation, Inc.
3*fae548d3Szrj 
4*fae548d3Szrj    This file is part of libctf.
5*fae548d3Szrj 
6*fae548d3Szrj    libctf is free software; you can redistribute it and/or modify it under
7*fae548d3Szrj    the terms of the GNU General Public License as published by the Free
8*fae548d3Szrj    Software Foundation; either version 3, or (at your option) any later
9*fae548d3Szrj    version.
10*fae548d3Szrj 
11*fae548d3Szrj    This program is distributed in the hope that it will be useful, but
12*fae548d3Szrj    WITHOUT ANY WARRANTY; without even the implied warranty of
13*fae548d3Szrj    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14*fae548d3Szrj    See the GNU General Public License for more details.
15*fae548d3Szrj 
16*fae548d3Szrj    You should have received a copy of the GNU General Public License
17*fae548d3Szrj    along with this program; see the file COPYING.  If not see
18*fae548d3Szrj    <http://www.gnu.org/licenses/>.  */
19*fae548d3Szrj 
20*fae548d3Szrj #ifndef	_CTF_H
21*fae548d3Szrj #define	_CTF_H
22*fae548d3Szrj 
23*fae548d3Szrj #include <sys/types.h>
24*fae548d3Szrj #include <limits.h>
25*fae548d3Szrj #include <stdint.h>
26*fae548d3Szrj 
27*fae548d3Szrj 
28*fae548d3Szrj #ifdef	__cplusplus
29*fae548d3Szrj extern "C"
30*fae548d3Szrj {
31*fae548d3Szrj #endif
32*fae548d3Szrj 
33*fae548d3Szrj /* CTF - Compact ANSI-C Type Format
34*fae548d3Szrj 
35*fae548d3Szrj    This file format can be used to compactly represent the information needed
36*fae548d3Szrj    by a debugger to interpret the ANSI-C types used by a given program.
37*fae548d3Szrj    Traditionally, this kind of information is generated by the compiler when
38*fae548d3Szrj    invoked with the -g flag and is stored in "stabs" strings or in the more
39*fae548d3Szrj    modern DWARF format.  CTF provides a representation of only the information
40*fae548d3Szrj    that is relevant to debugging a complex, optimized C program such as the
41*fae548d3Szrj    operating system kernel in a form that is significantly more compact than
42*fae548d3Szrj    the equivalent stabs or DWARF representation.  The format is data-model
43*fae548d3Szrj    independent, so consumers do not need different code depending on whether
44*fae548d3Szrj    they are 32-bit or 64-bit programs; libctf automatically compensates for
45*fae548d3Szrj    endianness variations.  CTF assumes that a standard ELF symbol table is
46*fae548d3Szrj    available for use in the debugger, and uses the structure and data of the
47*fae548d3Szrj    symbol table to avoid storing redundant information.  The CTF data may be
48*fae548d3Szrj    compressed on disk or in memory, indicated by a bit in the header.  CTF may
49*fae548d3Szrj    be interpreted in a raw disk file, or it may be stored in an ELF section,
50*fae548d3Szrj    typically named .ctf.  Data structures are aligned so that a raw CTF file or
51*fae548d3Szrj    CTF ELF section may be manipulated using mmap(2).
52*fae548d3Szrj 
53*fae548d3Szrj    The CTF file or section itself has the following structure:
54*fae548d3Szrj 
55*fae548d3Szrj    +--------+--------+---------+----------+--------+----------+...
56*fae548d3Szrj    |  file  |  type  |  data   | function | object | function |...
57*fae548d3Szrj    | header | labels | objects |   info   | index  |  index   |...
58*fae548d3Szrj    +--------+--------+---------+----------+--------+----------+...
59*fae548d3Szrj 
60*fae548d3Szrj    ...+----------+-------+--------+
61*fae548d3Szrj    ...| variable | data  | string |
62*fae548d3Szrj    ...|   info   | types | table  |
63*fae548d3Szrj       +----------+-------+--------+
64*fae548d3Szrj 
65*fae548d3Szrj    The file header stores a magic number and version information, encoding
66*fae548d3Szrj    flags, and the byte offset of each of the sections relative to the end of the
67*fae548d3Szrj    header itself.  If the CTF data has been uniquified against another set of
68*fae548d3Szrj    CTF data, a reference to that data also appears in the the header.  This
69*fae548d3Szrj    reference is the name of the label corresponding to the types uniquified
70*fae548d3Szrj    against.
71*fae548d3Szrj 
72*fae548d3Szrj    Following the header is a list of labels, used to group the types included in
73*fae548d3Szrj    the data types section.  Each label is accompanied by a type ID i.  A given
74*fae548d3Szrj    label refers to the group of types whose IDs are in the range [0, i].
75*fae548d3Szrj 
76*fae548d3Szrj    Data object and function records are stored in the same order as they appear
77*fae548d3Szrj    in the corresponding symbol table, except that symbols marked SHN_UNDEF are
78*fae548d3Szrj    not stored and symbols that have no type data are padded out with zeroes.
79*fae548d3Szrj    For each data object, the type ID (a small integer) is recorded.  For each
80*fae548d3Szrj    function, the type ID of the return type and argument types is recorded.
81*fae548d3Szrj 
82*fae548d3Szrj    For situations in which the order of the symbols in the symtab is not known,
83*fae548d3Szrj    a pair of optional indexes follow the data object and function info sections:
84*fae548d3Szrj    each of these is an array of strtab indexes, mapped 1:1 to the corresponding
85*fae548d3Szrj    data object / function info section, giving each entry in those sections a
86*fae548d3Szrj    name so that the linker can correlate them with final symtab entries and
87*fae548d3Szrj    reorder them accordingly (dropping the indexes in the process).
88*fae548d3Szrj 
89*fae548d3Szrj    Variable records (as distinct from data objects) provide a modicum of support
90*fae548d3Szrj    for non-ELF systems, mapping a variable name to a CTF type ID.  The variable
91*fae548d3Szrj    names are sorted into ASCIIbetical order, permitting binary searching.  We do
92*fae548d3Szrj    not define how the consumer maps these variable names to addresses or
93*fae548d3Szrj    anything else, or indeed what these names represent: they might be names
94*fae548d3Szrj    looked up at runtime via dlsym() or names extracted at runtime by a debugger
95*fae548d3Szrj    or anything else the consumer likes.
96*fae548d3Szrj 
97*fae548d3Szrj    The data types section is a list of variable size records that represent each
98*fae548d3Szrj    type, in order by their ID.  The types themselves form a directed graph,
99*fae548d3Szrj    where each node may contain one or more outgoing edges to other type nodes,
100*fae548d3Szrj    denoted by their ID.  Most type nodes are standalone or point backwards to
101*fae548d3Szrj    earlier nodes, but this is not required: nodes can point to later nodes,
102*fae548d3Szrj    particularly structure and union members.
103*fae548d3Szrj 
104*fae548d3Szrj    Strings are recorded as a string table ID (0 or 1) and a byte offset into the
105*fae548d3Szrj    string table.  String table 0 is the internal CTF string table.  String table
106*fae548d3Szrj    1 is the external string table, which is the string table associated with the
107*fae548d3Szrj    ELF symbol table for this object.  CTF does not record any strings that are
108*fae548d3Szrj    already in the symbol table, and the CTF string table does not contain any
109*fae548d3Szrj    duplicated strings.
110*fae548d3Szrj 
111*fae548d3Szrj    If the CTF data has been merged with another parent CTF object, some outgoing
112*fae548d3Szrj    edges may refer to type nodes that exist in another CTF object.  The debugger
113*fae548d3Szrj    and libctf library are responsible for connecting the appropriate objects
114*fae548d3Szrj    together so that the full set of types can be explored and manipulated.
115*fae548d3Szrj 
116*fae548d3Szrj    This connection is done purely using the ctf_import() function.  There is no
117*fae548d3Szrj    notation anywhere in the child CTF file indicating which parent it is
118*fae548d3Szrj    connected to: it is the debugger's responsibility to track this.  */
119*fae548d3Szrj 
120*fae548d3Szrj #define CTF_MAX_TYPE	0xfffffffe	/* Max type identifier value.  */
121*fae548d3Szrj #define CTF_MAX_PTYPE	0x7fffffff	/* Max parent type identifier value.  */
122*fae548d3Szrj #define CTF_MAX_NAME 0x7fffffff		/* Max offset into a string table.  */
123*fae548d3Szrj #define CTF_MAX_VLEN	0xffffff /* Max struct, union, enum members or args.  */
124*fae548d3Szrj 
125*fae548d3Szrj /* See ctf_type_t */
126*fae548d3Szrj #define CTF_MAX_SIZE	0xfffffffe	/* Max size of a v2 type in bytes. */
127*fae548d3Szrj #define CTF_LSIZE_SENT	0xffffffff	/* Sentinel for v2 ctt_size.  */
128*fae548d3Szrj 
129*fae548d3Szrj # define CTF_MAX_TYPE_V1	0xffff	/* Max type identifier value.  */
130*fae548d3Szrj # define CTF_MAX_PTYPE_V1	0x7fff	/* Max parent type identifier value.  */
131*fae548d3Szrj # define CTF_MAX_VLEN_V1	0x3ff	/* Max struct, union, enums or args.  */
132*fae548d3Szrj # define CTF_MAX_SIZE_V1	0xfffe	/* Max size of a type in bytes. */
133*fae548d3Szrj # define CTF_LSIZE_SENT_V1	0xffff	/* Sentinel for v1 ctt_size.  */
134*fae548d3Szrj 
135*fae548d3Szrj   /* Start of actual data structure definitions.
136*fae548d3Szrj 
137*fae548d3Szrj      Every field in these structures must have corresponding code in the
138*fae548d3Szrj      endianness-swapping machinery in libctf/ctf-open.c.  */
139*fae548d3Szrj 
140*fae548d3Szrj typedef struct ctf_preamble
141*fae548d3Szrj {
142*fae548d3Szrj   unsigned short ctp_magic;	/* Magic number (CTF_MAGIC).  */
143*fae548d3Szrj   unsigned char ctp_version;	/* Data format version number (CTF_VERSION).  */
144*fae548d3Szrj   unsigned char ctp_flags;	/* Flags (see below).  */
145*fae548d3Szrj } ctf_preamble_t;
146*fae548d3Szrj 
147*fae548d3Szrj typedef struct ctf_header_v2
148*fae548d3Szrj {
149*fae548d3Szrj   ctf_preamble_t cth_preamble;
150*fae548d3Szrj   uint32_t cth_parlabel;	/* Ref to name of parent lbl uniq'd against.  */
151*fae548d3Szrj   uint32_t cth_parname;		/* Ref to basename of parent.  */
152*fae548d3Szrj   uint32_t cth_lbloff;		/* Offset of label section.  */
153*fae548d3Szrj   uint32_t cth_objtoff;		/* Offset of object section.  */
154*fae548d3Szrj   uint32_t cth_funcoff;		/* Offset of function section.  */
155*fae548d3Szrj   uint32_t cth_varoff;		/* Offset of variable section.  */
156*fae548d3Szrj   uint32_t cth_typeoff;		/* Offset of type section.  */
157*fae548d3Szrj   uint32_t cth_stroff;		/* Offset of string section.  */
158*fae548d3Szrj   uint32_t cth_strlen;		/* Length of string section in bytes.  */
159*fae548d3Szrj } ctf_header_v2_t;
160*fae548d3Szrj 
161*fae548d3Szrj typedef struct ctf_header
162*fae548d3Szrj {
163*fae548d3Szrj   ctf_preamble_t cth_preamble;
164*fae548d3Szrj   uint32_t cth_parlabel;	/* Ref to name of parent lbl uniq'd against.  */
165*fae548d3Szrj   uint32_t cth_parname;		/* Ref to basename of parent.  */
166*fae548d3Szrj   uint32_t cth_cuname;		/* Ref to CU name (may be 0).  */
167*fae548d3Szrj   uint32_t cth_lbloff;		/* Offset of label section.  */
168*fae548d3Szrj   uint32_t cth_objtoff;		/* Offset of object section.  */
169*fae548d3Szrj   uint32_t cth_funcoff;		/* Offset of function section.  */
170*fae548d3Szrj   uint32_t cth_objtidxoff;	/* Offset of object index section.  */
171*fae548d3Szrj   uint32_t cth_funcidxoff;	/* Offset of function index section.  */
172*fae548d3Szrj   uint32_t cth_varoff;		/* Offset of variable section.  */
173*fae548d3Szrj   uint32_t cth_typeoff;		/* Offset of type section.  */
174*fae548d3Szrj   uint32_t cth_stroff;		/* Offset of string section.  */
175*fae548d3Szrj   uint32_t cth_strlen;		/* Length of string section in bytes.  */
176*fae548d3Szrj } ctf_header_t;
177*fae548d3Szrj 
178*fae548d3Szrj #define cth_magic   cth_preamble.ctp_magic
179*fae548d3Szrj #define cth_version cth_preamble.ctp_version
180*fae548d3Szrj #define cth_flags   cth_preamble.ctp_flags
181*fae548d3Szrj 
182*fae548d3Szrj #define CTF_MAGIC	0xdff2	/* Magic number identifying header.  */
183*fae548d3Szrj 
184*fae548d3Szrj /* Data format version number.  */
185*fae548d3Szrj 
186*fae548d3Szrj /* v1 upgraded to a later version is not quite the same as the native form,
187*fae548d3Szrj    because the boundary between parent and child types is different but not
188*fae548d3Szrj    recorded anywhere, and you can write it out again via ctf_compress_write(),
189*fae548d3Szrj    so we must track whether the thing was originally v1 or not.  If we were
190*fae548d3Szrj    writing the header from scratch, we would add a *pair* of version number
191*fae548d3Szrj    fields to allow for this, but this will do for now.  (A flag will not do,
192*fae548d3Szrj    because we need to encode both the version we came from and the version we
193*fae548d3Szrj    went to, not just "we were upgraded".) */
194*fae548d3Szrj 
195*fae548d3Szrj # define CTF_VERSION_1 1
196*fae548d3Szrj # define CTF_VERSION_1_UPGRADED_3 2
197*fae548d3Szrj # define CTF_VERSION_2 3
198*fae548d3Szrj 
199*fae548d3Szrj #define CTF_VERSION_3 4
200*fae548d3Szrj #define CTF_VERSION CTF_VERSION_3 /* Current version.  */
201*fae548d3Szrj 
202*fae548d3Szrj #define CTF_F_COMPRESS	0x1	/* Data buffer is compressed by libctf.  */
203*fae548d3Szrj 
204*fae548d3Szrj typedef struct ctf_lblent
205*fae548d3Szrj {
206*fae548d3Szrj   uint32_t ctl_label;		/* Ref to name of label.  */
207*fae548d3Szrj   uint32_t ctl_type;		/* Last type associated with this label.  */
208*fae548d3Szrj } ctf_lblent_t;
209*fae548d3Szrj 
210*fae548d3Szrj typedef struct ctf_varent
211*fae548d3Szrj {
212*fae548d3Szrj   uint32_t ctv_name;		/* Reference to name in string table.  */
213*fae548d3Szrj   uint32_t ctv_type;		/* Index of type of this variable.  */
214*fae548d3Szrj } ctf_varent_t;
215*fae548d3Szrj 
216*fae548d3Szrj /* In format v2, type sizes, measured in bytes, come in two flavours.  Nearly
217*fae548d3Szrj    all of them fit into a (UINT_MAX - 1), and thus can be stored in the ctt_size
218*fae548d3Szrj    member of a ctf_stype_t.  The maximum value for these sizes is CTF_MAX_SIZE.
219*fae548d3Szrj    Types larger than this must be stored in the ctf_lsize member of a
220*fae548d3Szrj    ctf_type_t.  Use of this member is indicated by the presence of
221*fae548d3Szrj    CTF_LSIZE_SENT in ctt_size.  */
222*fae548d3Szrj 
223*fae548d3Szrj /* In v1, the same applies, only the limit is (USHRT_MAX - 1) and
224*fae548d3Szrj    CTF_MAX_SIZE_V1, and CTF_LSIZE_SENT_V1 is the sentinel.  */
225*fae548d3Szrj 
226*fae548d3Szrj typedef struct ctf_stype_v1
227*fae548d3Szrj {
228*fae548d3Szrj   uint32_t ctt_name;		/* Reference to name in string table.  */
229*fae548d3Szrj   unsigned short ctt_info;	/* Encoded kind, variant length (see below).  */
230*fae548d3Szrj #ifndef __GNUC__
231*fae548d3Szrj   union
232*fae548d3Szrj   {
233*fae548d3Szrj     unsigned short _size;	/* Size of entire type in bytes.  */
234*fae548d3Szrj     unsigned short _type;	/* Reference to another type.  */
235*fae548d3Szrj   } _u;
236*fae548d3Szrj #else
237*fae548d3Szrj   __extension__
238*fae548d3Szrj   union
239*fae548d3Szrj   {
240*fae548d3Szrj     unsigned short ctt_size;	/* Size of entire type in bytes.  */
241*fae548d3Szrj     unsigned short ctt_type;	/* Reference to another type.  */
242*fae548d3Szrj   };
243*fae548d3Szrj #endif
244*fae548d3Szrj } ctf_stype_v1_t;
245*fae548d3Szrj 
246*fae548d3Szrj typedef struct ctf_type_v1
247*fae548d3Szrj {
248*fae548d3Szrj   uint32_t ctt_name;		/* Reference to name in string table.  */
249*fae548d3Szrj   unsigned short ctt_info;	/* Encoded kind, variant length (see below).  */
250*fae548d3Szrj #ifndef __GNUC__
251*fae548d3Szrj   union
252*fae548d3Szrj   {
253*fae548d3Szrj     unsigned short _size;	/* Always CTF_LSIZE_SENT_V1.  */
254*fae548d3Szrj     unsigned short _type;	/* Do not use.  */
255*fae548d3Szrj   } _u;
256*fae548d3Szrj #else
257*fae548d3Szrj   __extension__
258*fae548d3Szrj   union
259*fae548d3Szrj   {
260*fae548d3Szrj     unsigned short ctt_size;	/* Always CTF_LSIZE_SENT_V1.  */
261*fae548d3Szrj     unsigned short ctt_type;	/* Do not use.  */
262*fae548d3Szrj   };
263*fae548d3Szrj #endif
264*fae548d3Szrj   uint32_t ctt_lsizehi;		/* High 32 bits of type size in bytes.  */
265*fae548d3Szrj   uint32_t ctt_lsizelo;		/* Low 32 bits of type size in bytes.  */
266*fae548d3Szrj } ctf_type_v1_t;
267*fae548d3Szrj 
268*fae548d3Szrj 
269*fae548d3Szrj typedef struct ctf_stype
270*fae548d3Szrj {
271*fae548d3Szrj   uint32_t ctt_name;		/* Reference to name in string table.  */
272*fae548d3Szrj   uint32_t ctt_info;		/* Encoded kind, variant length (see below).  */
273*fae548d3Szrj #ifndef __GNUC__
274*fae548d3Szrj   union
275*fae548d3Szrj   {
276*fae548d3Szrj     uint32_t _size;		/* Size of entire type in bytes.  */
277*fae548d3Szrj     uint32_t _type;		/* Reference to another type.  */
278*fae548d3Szrj   } _u;
279*fae548d3Szrj #else
280*fae548d3Szrj   __extension__
281*fae548d3Szrj   union
282*fae548d3Szrj   {
283*fae548d3Szrj     uint32_t ctt_size;		/* Size of entire type in bytes.  */
284*fae548d3Szrj     uint32_t ctt_type;		/* Reference to another type.  */
285*fae548d3Szrj   };
286*fae548d3Szrj #endif
287*fae548d3Szrj } ctf_stype_t;
288*fae548d3Szrj 
289*fae548d3Szrj typedef struct ctf_type
290*fae548d3Szrj {
291*fae548d3Szrj   uint32_t ctt_name;		/* Reference to name in string table.  */
292*fae548d3Szrj   uint32_t ctt_info;		/* Encoded kind, variant length (see below).  */
293*fae548d3Szrj #ifndef __GNUC__
294*fae548d3Szrj union
295*fae548d3Szrj   {
296*fae548d3Szrj     uint32_t _size;		/* Always CTF_LSIZE_SENT.  */
297*fae548d3Szrj     uint32_t _type;		/* Do not use.  */
298*fae548d3Szrj   } _u;
299*fae548d3Szrj #else
300*fae548d3Szrj   __extension__
301*fae548d3Szrj   union
302*fae548d3Szrj   {
303*fae548d3Szrj     uint32_t ctt_size;		/* Always CTF_LSIZE_SENT.  */
304*fae548d3Szrj     uint32_t ctt_type;		/* Do not use.  */
305*fae548d3Szrj   };
306*fae548d3Szrj #endif
307*fae548d3Szrj   uint32_t ctt_lsizehi;		/* High 32 bits of type size in bytes.  */
308*fae548d3Szrj   uint32_t ctt_lsizelo;		/* Low 32 bits of type size in bytes.  */
309*fae548d3Szrj } ctf_type_t;
310*fae548d3Szrj 
311*fae548d3Szrj #ifndef __GNUC__
312*fae548d3Szrj #define ctt_size _u._size	/* For fundamental types that have a size.  */
313*fae548d3Szrj #define ctt_type _u._type	/* For types that reference another type.  */
314*fae548d3Szrj #endif
315*fae548d3Szrj 
316*fae548d3Szrj /* The following macros and inline functions compose and decompose values for
317*fae548d3Szrj    ctt_info and ctt_name, as well as other structures that contain name
318*fae548d3Szrj    references.  Use outside libdtrace-ctf itself is explicitly for access to CTF
319*fae548d3Szrj    files directly: types returned from the library will always appear to be
320*fae548d3Szrj    CTF_V2.
321*fae548d3Szrj 
322*fae548d3Szrj    v1: (transparently upgraded to v2 at open time: may be compiled out of the
323*fae548d3Szrj    library)
324*fae548d3Szrj                ------------------------
325*fae548d3Szrj    ctt_info:   | kind | isroot | vlen |
326*fae548d3Szrj                ------------------------
327*fae548d3Szrj                15   11    10    9     0
328*fae548d3Szrj 
329*fae548d3Szrj    v2:
330*fae548d3Szrj                ------------------------
331*fae548d3Szrj    ctt_info:   | kind | isroot | vlen |
332*fae548d3Szrj                ------------------------
333*fae548d3Szrj                31    26    25  24     0
334*fae548d3Szrj 
335*fae548d3Szrj    CTF_V1 and V2 _INFO_VLEN have the same interface:
336*fae548d3Szrj 
337*fae548d3Szrj    kind = CTF_*_INFO_KIND(c.ctt_info);     <-- CTF_K_* value (see below)
338*fae548d3Szrj    vlen = CTF_*_INFO_VLEN(fp, c.ctt_info); <-- length of variable data list
339*fae548d3Szrj 
340*fae548d3Szrj    stid = CTF_NAME_STID(c.ctt_name);     <-- string table id number (0 or 1)
341*fae548d3Szrj    offset = CTF_NAME_OFFSET(c.ctt_name); <-- string table byte offset
342*fae548d3Szrj 
343*fae548d3Szrj    c.ctt_info = CTF_TYPE_INFO(kind, vlen);
344*fae548d3Szrj    c.ctt_name = CTF_TYPE_NAME(stid, offset);  */
345*fae548d3Szrj 
346*fae548d3Szrj # define CTF_V1_INFO_KIND(info)		(((info) & 0xf800) >> 11)
347*fae548d3Szrj # define CTF_V1_INFO_ISROOT(info)	(((info) & 0x0400) >> 10)
348*fae548d3Szrj # define CTF_V1_INFO_VLEN(info)		(((info) & CTF_MAX_VLEN_V1))
349*fae548d3Szrj 
350*fae548d3Szrj #define CTF_V2_INFO_KIND(info)		(((info) & 0xfc000000) >> 26)
351*fae548d3Szrj #define CTF_V2_INFO_ISROOT(info)	(((info) & 0x2000000) >> 25)
352*fae548d3Szrj #define CTF_V2_INFO_VLEN(info)		(((info) & CTF_MAX_VLEN))
353*fae548d3Szrj 
354*fae548d3Szrj #define CTF_NAME_STID(name)		((name) >> 31)
355*fae548d3Szrj #define CTF_NAME_OFFSET(name)		((name) & CTF_MAX_NAME)
356*fae548d3Szrj #define CTF_SET_STID(name, stid)	((name) | (stid) << 31)
357*fae548d3Szrj 
358*fae548d3Szrj /* V2 only. */
359*fae548d3Szrj #define CTF_TYPE_INFO(kind, isroot, vlen) \
360*fae548d3Szrj 	(((kind) << 26) | (((isroot) ? 1 : 0) << 25) | ((vlen) & CTF_MAX_VLEN))
361*fae548d3Szrj 
362*fae548d3Szrj #define CTF_TYPE_NAME(stid, offset) \
363*fae548d3Szrj 	(((stid) << 31) | ((offset) & CTF_MAX_NAME))
364*fae548d3Szrj 
365*fae548d3Szrj /* The next set of macros are for public consumption only.  Not used internally,
366*fae548d3Szrj    since the relevant type boundary is dependent upon the version of the file at
367*fae548d3Szrj    *opening* time, not the version after transparent upgrade.  Use
368*fae548d3Szrj    ctf_type_isparent() / ctf_type_ischild() for that.  */
369*fae548d3Szrj 
370*fae548d3Szrj #define CTF_V2_TYPE_ISPARENT(fp, id)	((id) <= CTF_MAX_PTYPE)
371*fae548d3Szrj #define CTF_V2_TYPE_ISCHILD(fp, id)	((id) > CTF_MAX_PTYPE)
372*fae548d3Szrj #define CTF_V2_TYPE_TO_INDEX(id)	((id) & CTF_MAX_PTYPE)
373*fae548d3Szrj #define CTF_V2_INDEX_TO_TYPE(id, child) ((child) ? ((id) | (CTF_MAX_PTYPE+1)) : (id))
374*fae548d3Szrj 
375*fae548d3Szrj # define CTF_V1_TYPE_ISPARENT(fp, id)	((id) <= CTF_MAX_PTYPE_V1)
376*fae548d3Szrj # define CTF_V1_TYPE_ISCHILD(fp, id)	((id) > CTF_MAX_PTYPE_V1)
377*fae548d3Szrj # define CTF_V1_TYPE_TO_INDEX(id)	((id) & CTF_MAX_PTYPE_V1)
378*fae548d3Szrj # define CTF_V1_INDEX_TO_TYPE(id, child) ((child) ? ((id) | (CTF_MAX_PTYPE_V1+1)) : (id))
379*fae548d3Szrj 
380*fae548d3Szrj /* Valid for both V1 and V2. */
381*fae548d3Szrj #define CTF_TYPE_LSIZE(cttp) \
382*fae548d3Szrj 	(((uint64_t)(cttp)->ctt_lsizehi) << 32 | (cttp)->ctt_lsizelo)
383*fae548d3Szrj #define CTF_SIZE_TO_LSIZE_HI(size)	((uint32_t)((uint64_t)(size) >> 32))
384*fae548d3Szrj #define CTF_SIZE_TO_LSIZE_LO(size)	((uint32_t)(size))
385*fae548d3Szrj 
386*fae548d3Szrj #define CTF_STRTAB_0	0	/* String table id 0 (in-CTF).  */
387*fae548d3Szrj #define CTF_STRTAB_1	1	/* String table id 1 (ELF strtab).  */
388*fae548d3Szrj 
389*fae548d3Szrj /* Values for CTF_TYPE_KIND().  If the kind has an associated data list,
390*fae548d3Szrj    CTF_INFO_VLEN() will extract the number of elements in the list, and
391*fae548d3Szrj    the type of each element is shown in the comments below. */
392*fae548d3Szrj 
393*fae548d3Szrj #define CTF_K_UNKNOWN	0	/* Unknown type (used for padding).  */
394*fae548d3Szrj #define CTF_K_INTEGER	1	/* Variant data is CTF_INT_DATA (see below).  */
395*fae548d3Szrj #define CTF_K_FLOAT	2	/* Variant data is CTF_FP_DATA (see below).  */
396*fae548d3Szrj #define CTF_K_POINTER	3	/* ctt_type is referenced type.  */
397*fae548d3Szrj #define CTF_K_ARRAY	4	/* Variant data is single ctf_array_t.  */
398*fae548d3Szrj #define CTF_K_FUNCTION	5	/* ctt_type is return type, variant data is
399*fae548d3Szrj 				   list of argument types (unsigned short's for v1,
400*fae548d3Szrj 				   uint32_t's for v2).  */
401*fae548d3Szrj #define CTF_K_STRUCT	6	/* Variant data is list of ctf_member_t's.  */
402*fae548d3Szrj #define CTF_K_UNION	7	/* Variant data is list of ctf_member_t's.  */
403*fae548d3Szrj #define CTF_K_ENUM	8	/* Variant data is list of ctf_enum_t's.  */
404*fae548d3Szrj #define CTF_K_FORWARD	9	/* No additional data; ctt_name is tag.  */
405*fae548d3Szrj #define CTF_K_TYPEDEF	10	/* ctt_type is referenced type.  */
406*fae548d3Szrj #define CTF_K_VOLATILE	11	/* ctt_type is base type.  */
407*fae548d3Szrj #define CTF_K_CONST	12	/* ctt_type is base type.  */
408*fae548d3Szrj #define CTF_K_RESTRICT	13	/* ctt_type is base type.  */
409*fae548d3Szrj #define CTF_K_SLICE	14	/* Variant data is a ctf_slice_t.  */
410*fae548d3Szrj 
411*fae548d3Szrj #define CTF_K_MAX	63	/* Maximum possible (V2) CTF_K_* value.  */
412*fae548d3Szrj 
413*fae548d3Szrj /* Values for ctt_type when kind is CTF_K_INTEGER.  The flags, offset in bits,
414*fae548d3Szrj    and size in bits are encoded as a single word using the following macros.
415*fae548d3Szrj    (However, you can also encode the offset and bitness in a slice.)  */
416*fae548d3Szrj 
417*fae548d3Szrj #define CTF_INT_ENCODING(data) (((data) & 0xff000000) >> 24)
418*fae548d3Szrj #define CTF_INT_OFFSET(data)   (((data) & 0x00ff0000) >> 16)
419*fae548d3Szrj #define CTF_INT_BITS(data)     (((data) & 0x0000ffff))
420*fae548d3Szrj 
421*fae548d3Szrj #define CTF_INT_DATA(encoding, offset, bits) \
422*fae548d3Szrj        (((encoding) << 24) | ((offset) << 16) | (bits))
423*fae548d3Szrj 
424*fae548d3Szrj #define CTF_INT_SIGNED	0x01	/* Integer is signed (otherwise unsigned).  */
425*fae548d3Szrj #define CTF_INT_CHAR	0x02	/* Character display format.  */
426*fae548d3Szrj #define CTF_INT_BOOL	0x04	/* Boolean display format.  */
427*fae548d3Szrj #define CTF_INT_VARARGS	0x08	/* Varargs display format.  */
428*fae548d3Szrj 
429*fae548d3Szrj /* Use CTF_CHAR to produce a char that agrees with the system's native
430*fae548d3Szrj    char signedness.  */
431*fae548d3Szrj #if CHAR_MIN == 0
432*fae548d3Szrj # define CTF_CHAR (CTF_INT_CHAR)
433*fae548d3Szrj #else
434*fae548d3Szrj # define CTF_CHAR (CTF_INT_CHAR | CTF_INT_SIGNED)
435*fae548d3Szrj #endif
436*fae548d3Szrj 
437*fae548d3Szrj /* Values for ctt_type when kind is CTF_K_FLOAT.  The encoding, offset in bits,
438*fae548d3Szrj    and size in bits are encoded as a single word using the following macros.
439*fae548d3Szrj    (However, you can also encode the offset and bitness in a slice.)  */
440*fae548d3Szrj 
441*fae548d3Szrj #define CTF_FP_ENCODING(data)  (((data) & 0xff000000) >> 24)
442*fae548d3Szrj #define CTF_FP_OFFSET(data)    (((data) & 0x00ff0000) >> 16)
443*fae548d3Szrj #define CTF_FP_BITS(data)      (((data) & 0x0000ffff))
444*fae548d3Szrj 
445*fae548d3Szrj #define CTF_FP_DATA(encoding, offset, bits) \
446*fae548d3Szrj        (((encoding) << 24) | ((offset) << 16) | (bits))
447*fae548d3Szrj 
448*fae548d3Szrj /* Variant data when kind is CTF_K_FLOAT is an encoding in the top eight bits.  */
449*fae548d3Szrj #define CTF_FP_ENCODING(data)	(((data) & 0xff000000) >> 24)
450*fae548d3Szrj 
451*fae548d3Szrj #define CTF_FP_SINGLE	1	/* IEEE 32-bit float encoding.  */
452*fae548d3Szrj #define CTF_FP_DOUBLE	2	/* IEEE 64-bit float encoding.  */
453*fae548d3Szrj #define CTF_FP_CPLX	3	/* Complex encoding.  */
454*fae548d3Szrj #define CTF_FP_DCPLX	4	/* Double complex encoding.  */
455*fae548d3Szrj #define CTF_FP_LDCPLX	5	/* Long double complex encoding.  */
456*fae548d3Szrj #define CTF_FP_LDOUBLE	6	/* Long double encoding.  */
457*fae548d3Szrj #define CTF_FP_INTRVL	7	/* Interval (2x32-bit) encoding.  */
458*fae548d3Szrj #define CTF_FP_DINTRVL	8	/* Double interval (2x64-bit) encoding.  */
459*fae548d3Szrj #define CTF_FP_LDINTRVL	9	/* Long double interval (2x128-bit) encoding.  */
460*fae548d3Szrj #define CTF_FP_IMAGRY	10	/* Imaginary (32-bit) encoding.  */
461*fae548d3Szrj #define CTF_FP_DIMAGRY	11	/* Long imaginary (64-bit) encoding.  */
462*fae548d3Szrj #define CTF_FP_LDIMAGRY	12	/* Long double imaginary (128-bit) encoding.  */
463*fae548d3Szrj 
464*fae548d3Szrj #define CTF_FP_MAX	12	/* Maximum possible CTF_FP_* value */
465*fae548d3Szrj 
466*fae548d3Szrj /* A slice increases the offset and reduces the bitness of the referenced
467*fae548d3Szrj    ctt_type, which must be a type which has an encoding (fp, int, or enum).  We
468*fae548d3Szrj    also store the referenced type in here, because it is easier to keep the
469*fae548d3Szrj    ctt_size correct for the slice than to shuffle the size into here and keep
470*fae548d3Szrj    the ctt_type where it is for other types.
471*fae548d3Szrj 
472*fae548d3Szrj    In a future version, where we loosen requirements on alignment in the CTF
473*fae548d3Szrj    file, the cts_offset and cts_bits will be chars: but for now they must be
474*fae548d3Szrj    shorts or everything after a slice will become unaligned.  */
475*fae548d3Szrj 
476*fae548d3Szrj typedef struct ctf_slice
477*fae548d3Szrj {
478*fae548d3Szrj   uint32_t cts_type;
479*fae548d3Szrj   unsigned short cts_offset;
480*fae548d3Szrj   unsigned short cts_bits;
481*fae548d3Szrj } ctf_slice_t;
482*fae548d3Szrj 
483*fae548d3Szrj typedef struct ctf_array_v1
484*fae548d3Szrj {
485*fae548d3Szrj   unsigned short cta_contents;	/* Reference to type of array contents.  */
486*fae548d3Szrj   unsigned short cta_index;	/* Reference to type of array index.  */
487*fae548d3Szrj   uint32_t cta_nelems;		/* Number of elements.  */
488*fae548d3Szrj } ctf_array_v1_t;
489*fae548d3Szrj 
490*fae548d3Szrj typedef struct ctf_array
491*fae548d3Szrj {
492*fae548d3Szrj   uint32_t cta_contents;	/* Reference to type of array contents.  */
493*fae548d3Szrj   uint32_t cta_index;		/* Reference to type of array index.  */
494*fae548d3Szrj   uint32_t cta_nelems;		/* Number of elements.  */
495*fae548d3Szrj } ctf_array_t;
496*fae548d3Szrj 
497*fae548d3Szrj /* Most structure members have bit offsets that can be expressed using a short.
498*fae548d3Szrj    Some don't.  ctf_member_t is used for structs which cannot contain any of
499*fae548d3Szrj    these large offsets, whereas ctf_lmember_t is used in the latter case.  If
500*fae548d3Szrj    any member of a given struct has an offset that cannot be expressed using a
501*fae548d3Szrj    uint32_t, all members will be stored as type ctf_lmember_t.  This is expected
502*fae548d3Szrj    to be very rare (but nonetheless possible).  */
503*fae548d3Szrj 
504*fae548d3Szrj #define CTF_LSTRUCT_THRESH	536870912
505*fae548d3Szrj 
506*fae548d3Szrj /* In v1, the same is true, except that lmembers are used for structs >= 8192
507*fae548d3Szrj    bytes in size.  (The ordering of members in the ctf_member_* structures is
508*fae548d3Szrj    different to improve padding.)  */
509*fae548d3Szrj 
510*fae548d3Szrj #define CTF_LSTRUCT_THRESH_V1	8192
511*fae548d3Szrj 
512*fae548d3Szrj typedef struct ctf_member_v1
513*fae548d3Szrj {
514*fae548d3Szrj   uint32_t ctm_name;		/* Reference to name in string table.  */
515*fae548d3Szrj   unsigned short ctm_type;	/* Reference to type of member.  */
516*fae548d3Szrj   unsigned short ctm_offset;	/* Offset of this member in bits.  */
517*fae548d3Szrj } ctf_member_v1_t;
518*fae548d3Szrj 
519*fae548d3Szrj typedef struct ctf_lmember_v1
520*fae548d3Szrj {
521*fae548d3Szrj   uint32_t ctlm_name;		/* Reference to name in string table.  */
522*fae548d3Szrj   unsigned short ctlm_type;	/* Reference to type of member.  */
523*fae548d3Szrj   unsigned short ctlm_pad;	/* Padding.  */
524*fae548d3Szrj   uint32_t ctlm_offsethi;	/* High 32 bits of member offset in bits.  */
525*fae548d3Szrj   uint32_t ctlm_offsetlo;	/* Low 32 bits of member offset in bits.  */
526*fae548d3Szrj } ctf_lmember_v1_t;
527*fae548d3Szrj 
528*fae548d3Szrj typedef struct ctf_member_v2
529*fae548d3Szrj {
530*fae548d3Szrj   uint32_t ctm_name;		/* Reference to name in string table.  */
531*fae548d3Szrj   uint32_t ctm_offset;		/* Offset of this member in bits.  */
532*fae548d3Szrj   uint32_t ctm_type;		/* Reference to type of member.  */
533*fae548d3Szrj } ctf_member_t;
534*fae548d3Szrj 
535*fae548d3Szrj typedef struct ctf_lmember_v2
536*fae548d3Szrj {
537*fae548d3Szrj   uint32_t ctlm_name;		/* Reference to name in string table.  */
538*fae548d3Szrj   uint32_t ctlm_offsethi;	/* High 32 bits of member offset in bits.  */
539*fae548d3Szrj   uint32_t ctlm_type;		/* Reference to type of member.  */
540*fae548d3Szrj   uint32_t ctlm_offsetlo;	/* Low 32 bits of member offset in bits.  */
541*fae548d3Szrj } ctf_lmember_t;
542*fae548d3Szrj 
543*fae548d3Szrj #define	CTF_LMEM_OFFSET(ctlmp) \
544*fae548d3Szrj 	(((uint64_t)(ctlmp)->ctlm_offsethi) << 32 | (ctlmp)->ctlm_offsetlo)
545*fae548d3Szrj #define	CTF_OFFSET_TO_LMEMHI(offset)	((uint32_t)((uint64_t)(offset) >> 32))
546*fae548d3Szrj #define	CTF_OFFSET_TO_LMEMLO(offset)	((uint32_t)(offset))
547*fae548d3Szrj 
548*fae548d3Szrj typedef struct ctf_enum
549*fae548d3Szrj {
550*fae548d3Szrj   uint32_t cte_name;		/* Reference to name in string table.  */
551*fae548d3Szrj   int32_t cte_value;		/* Value associated with this name.  */
552*fae548d3Szrj } ctf_enum_t;
553*fae548d3Szrj 
554*fae548d3Szrj /* The ctf_archive is a collection of ctf_file_t's stored together. The format
555*fae548d3Szrj    is suitable for mmap()ing: this control structure merely describes the
556*fae548d3Szrj    mmap()ed archive (and overlaps the first few bytes of it), hence the
557*fae548d3Szrj    greater care taken with integral types.  All CTF files in an archive
558*fae548d3Szrj    must have the same data model.  (This is not validated.)
559*fae548d3Szrj 
560*fae548d3Szrj    All integers in this structure are stored in little-endian byte order.
561*fae548d3Szrj 
562*fae548d3Szrj    The code relies on the fact that everything in this header is a uint64_t
563*fae548d3Szrj    and thus the header needs no padding (in particular, that no padding is
564*fae548d3Szrj    needed between ctfa_ctfs and the unnamed ctfa_archive_modent array
565*fae548d3Szrj    that follows it).
566*fae548d3Szrj 
567*fae548d3Szrj    This is *not* the same as the data structure returned by the ctf_arc_*()
568*fae548d3Szrj    functions:  this is the low-level on-disk representation.  */
569*fae548d3Szrj 
570*fae548d3Szrj #define CTFA_MAGIC 0x8b47f2a4d7623eeb	/* Random.  */
571*fae548d3Szrj struct ctf_archive
572*fae548d3Szrj {
573*fae548d3Szrj   /* Magic number.  (In loaded files, overwritten with the file size
574*fae548d3Szrj      so ctf_arc_close() knows how much to munmap()).  */
575*fae548d3Szrj   uint64_t ctfa_magic;
576*fae548d3Szrj 
577*fae548d3Szrj   /* CTF data model.  */
578*fae548d3Szrj   uint64_t ctfa_model;
579*fae548d3Szrj 
580*fae548d3Szrj   /* Number of CTF files in the archive.  */
581*fae548d3Szrj   uint64_t ctfa_nfiles;
582*fae548d3Szrj 
583*fae548d3Szrj   /* Offset of the name table.  */
584*fae548d3Szrj   uint64_t ctfa_names;
585*fae548d3Szrj 
586*fae548d3Szrj   /* Offset of the CTF table.  Each element starts with a size (a uint64_t
587*fae548d3Szrj      in network byte order) then a ctf_file_t of that size.  */
588*fae548d3Szrj   uint64_t ctfa_ctfs;
589*fae548d3Szrj };
590*fae548d3Szrj 
591*fae548d3Szrj /* An array of ctfa_nnamed of this structure lies at
592*fae548d3Szrj    ctf_archive[ctf_archive->ctfa_modents] and gives the ctfa_ctfs or
593*fae548d3Szrj    ctfa_names-relative offsets of each name or ctf_file_t.  */
594*fae548d3Szrj 
595*fae548d3Szrj typedef struct ctf_archive_modent
596*fae548d3Szrj {
597*fae548d3Szrj   uint64_t name_offset;
598*fae548d3Szrj   uint64_t ctf_offset;
599*fae548d3Szrj } ctf_archive_modent_t;
600*fae548d3Szrj 
601*fae548d3Szrj #ifdef	__cplusplus
602*fae548d3Szrj }
603*fae548d3Szrj #endif
604*fae548d3Szrj 
605*fae548d3Szrj #endif				/* _CTF_H */
606