xref: /netbsd-src/external/gpl3/gdb/dist/include/ctf.h (revision e663ba6e3a60083e70de702e9d54bf486a57b6a7)
18dffb485Schristos /* CTF format description.
2*e663ba6eSchristos    Copyright (C) 2019-2024 Free Software Foundation, Inc.
38dffb485Schristos 
48dffb485Schristos    This file is part of libctf.
58dffb485Schristos 
68dffb485Schristos    libctf is free software; you can redistribute it and/or modify it under
78dffb485Schristos    the terms of the GNU General Public License as published by the Free
88dffb485Schristos    Software Foundation; either version 3, or (at your option) any later
98dffb485Schristos    version.
108dffb485Schristos 
118dffb485Schristos    This program is distributed in the hope that it will be useful, but
128dffb485Schristos    WITHOUT ANY WARRANTY; without even the implied warranty of
138dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
148dffb485Schristos    See the GNU General Public License for more details.
158dffb485Schristos 
168dffb485Schristos    You should have received a copy of the GNU General Public License
178dffb485Schristos    along with this program; see the file COPYING.  If not see
188dffb485Schristos    <http://www.gnu.org/licenses/>.  */
198dffb485Schristos 
208dffb485Schristos #ifndef	_CTF_H
218dffb485Schristos #define	_CTF_H
228dffb485Schristos 
238dffb485Schristos #include <sys/types.h>
248dffb485Schristos #include <limits.h>
258dffb485Schristos #include <stdint.h>
268dffb485Schristos 
278dffb485Schristos 
288dffb485Schristos #ifdef	__cplusplus
298dffb485Schristos extern "C"
308dffb485Schristos {
318dffb485Schristos #endif
328dffb485Schristos 
338dffb485Schristos /* CTF - Compact ANSI-C Type Format
348dffb485Schristos 
358dffb485Schristos    This file format can be used to compactly represent the information needed
368dffb485Schristos    by a debugger to interpret the ANSI-C types used by a given program.
378dffb485Schristos    Traditionally, this kind of information is generated by the compiler when
388dffb485Schristos    invoked with the -g flag and is stored in "stabs" strings or in the more
398dffb485Schristos    modern DWARF format.  CTF provides a representation of only the information
408dffb485Schristos    that is relevant to debugging a complex, optimized C program such as the
418dffb485Schristos    operating system kernel in a form that is significantly more compact than
428dffb485Schristos    the equivalent stabs or DWARF representation.  The format is data-model
438dffb485Schristos    independent, so consumers do not need different code depending on whether
448dffb485Schristos    they are 32-bit or 64-bit programs; libctf automatically compensates for
458dffb485Schristos    endianness variations.  CTF assumes that a standard ELF symbol table is
468dffb485Schristos    available for use in the debugger, and uses the structure and data of the
478dffb485Schristos    symbol table to avoid storing redundant information.  The CTF data may be
488dffb485Schristos    compressed on disk or in memory, indicated by a bit in the header.  CTF may
498dffb485Schristos    be interpreted in a raw disk file, or it may be stored in an ELF section,
508dffb485Schristos    typically named .ctf.  Data structures are aligned so that a raw CTF file or
518dffb485Schristos    CTF ELF section may be manipulated using mmap(2).
528dffb485Schristos 
538dffb485Schristos    The CTF file or section itself has the following structure:
548dffb485Schristos 
558dffb485Schristos    +--------+--------+---------+----------+--------+----------+...
568dffb485Schristos    |  file  |  type  |  data   | function | object | function |...
578dffb485Schristos    | header | labels | objects |   info   | index  |  index   |...
588dffb485Schristos    +--------+--------+---------+----------+--------+----------+...
598dffb485Schristos 
608dffb485Schristos    ...+----------+-------+--------+
618dffb485Schristos    ...| variable | data  | string |
628dffb485Schristos    ...|   info   | types | table  |
638dffb485Schristos       +----------+-------+--------+
648dffb485Schristos 
658dffb485Schristos    The file header stores a magic number and version information, encoding
668dffb485Schristos    flags, and the byte offset of each of the sections relative to the end of the
678dffb485Schristos    header itself.  If the CTF data has been uniquified against another set of
688dffb485Schristos    CTF data, a reference to that data also appears in the the header.  This
698dffb485Schristos    reference is the name of the label corresponding to the types uniquified
708dffb485Schristos    against.
718dffb485Schristos 
728dffb485Schristos    Following the header is a list of labels, used to group the types included in
738dffb485Schristos    the data types section.  Each label is accompanied by a type ID i.  A given
748dffb485Schristos    label refers to the group of types whose IDs are in the range [0, i].
758dffb485Schristos 
764b169a6bSchristos    Data object and function records (collectively, "symtypetabs") are stored in
774b169a6bSchristos    the same order as they appear in the corresponding symbol table, except that
784b169a6bSchristos    symbols marked SHN_UNDEF are not stored and symbols that have no type data
794b169a6bSchristos    are padded out with zeroes.  For each entry in these tables, the type ID (a
804b169a6bSchristos    small integer) is recorded.  (Functions get CTF_K_FUNCTION types, just like
814b169a6bSchristos    data objects that are function pointers.)
828dffb485Schristos 
838dffb485Schristos    For situations in which the order of the symbols in the symtab is not known,
844b169a6bSchristos    or most symbols have no type in this dict and most entries would be
854b169a6bSchristos    zero-pads, a pair of optional indexes follow the data object and function
864b169a6bSchristos    info sections: each of these is an array of strtab indexes, mapped 1:1 to the
874b169a6bSchristos    corresponding data object / function info section, giving each entry in those
884b169a6bSchristos    sections a name so that the linker can correlate them with final symtab
894b169a6bSchristos    entries and reorder them accordingly (dropping the indexes in the process).
908dffb485Schristos 
918dffb485Schristos    Variable records (as distinct from data objects) provide a modicum of support
924b169a6bSchristos    for non-ELF systems, mapping a variable or function name to a CTF type ID.
934b169a6bSchristos    The names are sorted into ASCIIbetical order, permitting binary searching.
944b169a6bSchristos    We do not define how the consumer maps these variable names to addresses or
958dffb485Schristos    anything else, or indeed what these names represent: they might be names
968dffb485Schristos    looked up at runtime via dlsym() or names extracted at runtime by a debugger
974b169a6bSchristos    or anything else the consumer likes.  Variable records with identically-
984b169a6bSchristos    named entries in the data object or function index section are removed.
998dffb485Schristos 
1008dffb485Schristos    The data types section is a list of variable size records that represent each
1018dffb485Schristos    type, in order by their ID.  The types themselves form a directed graph,
1028dffb485Schristos    where each node may contain one or more outgoing edges to other type nodes,
1038dffb485Schristos    denoted by their ID.  Most type nodes are standalone or point backwards to
1048dffb485Schristos    earlier nodes, but this is not required: nodes can point to later nodes,
1058dffb485Schristos    particularly structure and union members.
1068dffb485Schristos 
1078dffb485Schristos    Strings are recorded as a string table ID (0 or 1) and a byte offset into the
1088dffb485Schristos    string table.  String table 0 is the internal CTF string table.  String table
1098dffb485Schristos    1 is the external string table, which is the string table associated with the
1104b169a6bSchristos    ELF dynamic symbol table for this object.  CTF does not record any strings
1114b169a6bSchristos    that are already in the symbol table, and the CTF string table does not
1124b169a6bSchristos    contain any duplicated strings.
1138dffb485Schristos 
1148dffb485Schristos    If the CTF data has been merged with another parent CTF object, some outgoing
1158dffb485Schristos    edges may refer to type nodes that exist in another CTF object.  The debugger
1168dffb485Schristos    and libctf library are responsible for connecting the appropriate objects
1178dffb485Schristos    together so that the full set of types can be explored and manipulated.
1188dffb485Schristos 
1194b169a6bSchristos    This connection is done purely using the ctf_import() function.  The
1204b169a6bSchristos    ctf_archive machinery (and thus ctf_open et al) automatically imports archive
1214b169a6bSchristos    members named ".ctf" into child dicts if available in the same archive, to
1224b169a6bSchristos    match the relationship set up by the linker, but callers can call ctf_import
1234b169a6bSchristos    themselves as well if need be, if they know a different relationship is in
1244b169a6bSchristos    force.  */
1258dffb485Schristos 
1268dffb485Schristos #define CTF_MAX_TYPE	0xfffffffe	/* Max type identifier value.  */
1278dffb485Schristos #define CTF_MAX_PTYPE	0x7fffffff	/* Max parent type identifier value.  */
1288dffb485Schristos #define CTF_MAX_NAME 0x7fffffff		/* Max offset into a string table.  */
1298dffb485Schristos #define CTF_MAX_VLEN	0xffffff /* Max struct, union, enum members or args.  */
1308dffb485Schristos 
1318dffb485Schristos /* See ctf_type_t */
1328dffb485Schristos #define CTF_MAX_SIZE	0xfffffffe	/* Max size of a v2 type in bytes. */
1338dffb485Schristos #define CTF_LSIZE_SENT	0xffffffff	/* Sentinel for v2 ctt_size.  */
1348dffb485Schristos 
1358dffb485Schristos # define CTF_MAX_TYPE_V1	0xffff	/* Max type identifier value.  */
1368dffb485Schristos # define CTF_MAX_PTYPE_V1	0x7fff	/* Max parent type identifier value.  */
1378dffb485Schristos # define CTF_MAX_VLEN_V1	0x3ff	/* Max struct, union, enums or args.  */
1388dffb485Schristos # define CTF_MAX_SIZE_V1	0xfffe	/* Max size of a type in bytes. */
1398dffb485Schristos # define CTF_LSIZE_SENT_V1	0xffff	/* Sentinel for v1 ctt_size.  */
1408dffb485Schristos 
1418dffb485Schristos   /* Start of actual data structure definitions.
1428dffb485Schristos 
1438dffb485Schristos      Every field in these structures must have corresponding code in the
1448dffb485Schristos      endianness-swapping machinery in libctf/ctf-open.c.  */
1458dffb485Schristos 
1468dffb485Schristos typedef struct ctf_preamble
1478dffb485Schristos {
1488dffb485Schristos   unsigned short ctp_magic;	/* Magic number (CTF_MAGIC).  */
1498dffb485Schristos   unsigned char ctp_version;	/* Data format version number (CTF_VERSION).  */
1508dffb485Schristos   unsigned char ctp_flags;	/* Flags (see below).  */
1518dffb485Schristos } ctf_preamble_t;
1528dffb485Schristos 
1538dffb485Schristos typedef struct ctf_header_v2
1548dffb485Schristos {
1558dffb485Schristos   ctf_preamble_t cth_preamble;
1568dffb485Schristos   uint32_t cth_parlabel;	/* Ref to name of parent lbl uniq'd against.  */
1578dffb485Schristos   uint32_t cth_parname;		/* Ref to basename of parent.  */
1588dffb485Schristos   uint32_t cth_lbloff;		/* Offset of label section.  */
1598dffb485Schristos   uint32_t cth_objtoff;		/* Offset of object section.  */
1608dffb485Schristos   uint32_t cth_funcoff;		/* Offset of function section.  */
1618dffb485Schristos   uint32_t cth_varoff;		/* Offset of variable section.  */
1628dffb485Schristos   uint32_t cth_typeoff;		/* Offset of type section.  */
1638dffb485Schristos   uint32_t cth_stroff;		/* Offset of string section.  */
1648dffb485Schristos   uint32_t cth_strlen;		/* Length of string section in bytes.  */
1658dffb485Schristos } ctf_header_v2_t;
1668dffb485Schristos 
1678dffb485Schristos typedef struct ctf_header
1688dffb485Schristos {
1698dffb485Schristos   ctf_preamble_t cth_preamble;
1708dffb485Schristos   uint32_t cth_parlabel;	/* Ref to name of parent lbl uniq'd against.  */
1718dffb485Schristos   uint32_t cth_parname;		/* Ref to basename of parent.  */
1728dffb485Schristos   uint32_t cth_cuname;		/* Ref to CU name (may be 0).  */
1738dffb485Schristos   uint32_t cth_lbloff;		/* Offset of label section.  */
1748dffb485Schristos   uint32_t cth_objtoff;		/* Offset of object section.  */
1758dffb485Schristos   uint32_t cth_funcoff;		/* Offset of function section.  */
1768dffb485Schristos   uint32_t cth_objtidxoff;	/* Offset of object index section.  */
1778dffb485Schristos   uint32_t cth_funcidxoff;	/* Offset of function index section.  */
1788dffb485Schristos   uint32_t cth_varoff;		/* Offset of variable section.  */
1798dffb485Schristos   uint32_t cth_typeoff;		/* Offset of type section.  */
1808dffb485Schristos   uint32_t cth_stroff;		/* Offset of string section.  */
1818dffb485Schristos   uint32_t cth_strlen;		/* Length of string section in bytes.  */
1828dffb485Schristos } ctf_header_t;
1838dffb485Schristos 
1848dffb485Schristos #define cth_magic   cth_preamble.ctp_magic
1858dffb485Schristos #define cth_version cth_preamble.ctp_version
1868dffb485Schristos #define cth_flags   cth_preamble.ctp_flags
1878dffb485Schristos 
1888dffb485Schristos #define CTF_MAGIC	0xdff2	/* Magic number identifying header.  */
1898dffb485Schristos 
1908dffb485Schristos /* Data format version number.  */
1918dffb485Schristos 
1928dffb485Schristos /* v1 upgraded to a later version is not quite the same as the native form,
1938dffb485Schristos    because the boundary between parent and child types is different but not
1948dffb485Schristos    recorded anywhere, and you can write it out again via ctf_compress_write(),
1958dffb485Schristos    so we must track whether the thing was originally v1 or not.  If we were
1968dffb485Schristos    writing the header from scratch, we would add a *pair* of version number
1978dffb485Schristos    fields to allow for this, but this will do for now.  (A flag will not do,
1988dffb485Schristos    because we need to encode both the version we came from and the version we
1998dffb485Schristos    went to, not just "we were upgraded".) */
2008dffb485Schristos 
2018dffb485Schristos # define CTF_VERSION_1 1
2028dffb485Schristos # define CTF_VERSION_1_UPGRADED_3 2
2038dffb485Schristos # define CTF_VERSION_2 3
2048dffb485Schristos 
2058dffb485Schristos #define CTF_VERSION_3 4
2068dffb485Schristos #define CTF_VERSION CTF_VERSION_3 /* Current version.  */
2078dffb485Schristos 
2084b169a6bSchristos /* All of these flags bar CTF_F_COMPRESS and CTF_F_IDXSORTED are bug-workaround
2094b169a6bSchristos    flags and are valid only in format v3: in v2 and below they cannot occur and
2104b169a6bSchristos    in v4 and later, they will be recycled for other purposes.  */
2114b169a6bSchristos 
2128dffb485Schristos #define CTF_F_COMPRESS	0x1		/* Data buffer is compressed by libctf.  */
2134b169a6bSchristos #define CTF_F_NEWFUNCINFO 0x2		/* New v3 func info section format.  */
2144b169a6bSchristos #define CTF_F_IDXSORTED 0x4		/* Index sections already sorted.  */
2154b169a6bSchristos #define CTF_F_DYNSTR 0x8		/* Strings come from .dynstr.  */
2164b169a6bSchristos #define CTF_F_MAX (CTF_F_COMPRESS | CTF_F_NEWFUNCINFO | CTF_F_IDXSORTED	\
2174b169a6bSchristos 		   | CTF_F_DYNSTR)
2188dffb485Schristos 
2198dffb485Schristos typedef struct ctf_lblent
2208dffb485Schristos {
2218dffb485Schristos   uint32_t ctl_label;		/* Ref to name of label.  */
2228dffb485Schristos   uint32_t ctl_type;		/* Last type associated with this label.  */
2238dffb485Schristos } ctf_lblent_t;
2248dffb485Schristos 
2258dffb485Schristos typedef struct ctf_varent
2268dffb485Schristos {
2278dffb485Schristos   uint32_t ctv_name;		/* Reference to name in string table.  */
2288dffb485Schristos   uint32_t ctv_type;		/* Index of type of this variable.  */
2298dffb485Schristos } ctf_varent_t;
2308dffb485Schristos 
2318dffb485Schristos /* In format v2, type sizes, measured in bytes, come in two flavours.  Nearly
2328dffb485Schristos    all of them fit into a (UINT_MAX - 1), and thus can be stored in the ctt_size
2338dffb485Schristos    member of a ctf_stype_t.  The maximum value for these sizes is CTF_MAX_SIZE.
2348dffb485Schristos    Types larger than this must be stored in the ctf_lsize member of a
2358dffb485Schristos    ctf_type_t.  Use of this member is indicated by the presence of
2368dffb485Schristos    CTF_LSIZE_SENT in ctt_size.  */
2378dffb485Schristos 
2388dffb485Schristos /* In v1, the same applies, only the limit is (USHRT_MAX - 1) and
2398dffb485Schristos    CTF_MAX_SIZE_V1, and CTF_LSIZE_SENT_V1 is the sentinel.  */
2408dffb485Schristos 
2418dffb485Schristos typedef struct ctf_stype_v1
2428dffb485Schristos {
2438dffb485Schristos   uint32_t ctt_name;		/* Reference to name in string table.  */
2448dffb485Schristos   unsigned short ctt_info;	/* Encoded kind, variant length (see below).  */
2458dffb485Schristos #ifndef __GNUC__
2468dffb485Schristos   union
2478dffb485Schristos   {
2488dffb485Schristos     unsigned short _size;	/* Size of entire type in bytes.  */
2498dffb485Schristos     unsigned short _type;	/* Reference to another type.  */
2508dffb485Schristos   } _u;
2518dffb485Schristos #else
2528dffb485Schristos   __extension__
2538dffb485Schristos   union
2548dffb485Schristos   {
2558dffb485Schristos     unsigned short ctt_size;	/* Size of entire type in bytes.  */
2568dffb485Schristos     unsigned short ctt_type;	/* Reference to another type.  */
2578dffb485Schristos   };
2588dffb485Schristos #endif
2598dffb485Schristos } ctf_stype_v1_t;
2608dffb485Schristos 
2618dffb485Schristos typedef struct ctf_type_v1
2628dffb485Schristos {
2638dffb485Schristos   uint32_t ctt_name;		/* Reference to name in string table.  */
2648dffb485Schristos   unsigned short ctt_info;	/* Encoded kind, variant length (see below).  */
2658dffb485Schristos #ifndef __GNUC__
2668dffb485Schristos   union
2678dffb485Schristos   {
2688dffb485Schristos     unsigned short _size;	/* Always CTF_LSIZE_SENT_V1.  */
2698dffb485Schristos     unsigned short _type;	/* Do not use.  */
2708dffb485Schristos   } _u;
2718dffb485Schristos #else
2728dffb485Schristos   __extension__
2738dffb485Schristos   union
2748dffb485Schristos   {
2758dffb485Schristos     unsigned short ctt_size;	/* Always CTF_LSIZE_SENT_V1.  */
2768dffb485Schristos     unsigned short ctt_type;	/* Do not use.  */
2778dffb485Schristos   };
2788dffb485Schristos #endif
2798dffb485Schristos   uint32_t ctt_lsizehi;		/* High 32 bits of type size in bytes.  */
2808dffb485Schristos   uint32_t ctt_lsizelo;		/* Low 32 bits of type size in bytes.  */
2818dffb485Schristos } ctf_type_v1_t;
2828dffb485Schristos 
2838dffb485Schristos 
2848dffb485Schristos typedef struct ctf_stype
2858dffb485Schristos {
2868dffb485Schristos   uint32_t ctt_name;		/* Reference to name in string table.  */
2878dffb485Schristos   uint32_t ctt_info;		/* Encoded kind, variant length (see below).  */
2888dffb485Schristos #ifndef __GNUC__
2898dffb485Schristos   union
2908dffb485Schristos   {
2918dffb485Schristos     uint32_t _size;		/* Size of entire type in bytes.  */
2928dffb485Schristos     uint32_t _type;		/* Reference to another type.  */
2938dffb485Schristos   } _u;
2948dffb485Schristos #else
2958dffb485Schristos   __extension__
2968dffb485Schristos   union
2978dffb485Schristos   {
2988dffb485Schristos     uint32_t ctt_size;		/* Size of entire type in bytes.  */
2998dffb485Schristos     uint32_t ctt_type;		/* Reference to another type.  */
3008dffb485Schristos   };
3018dffb485Schristos #endif
3028dffb485Schristos } ctf_stype_t;
3038dffb485Schristos 
3048dffb485Schristos typedef struct ctf_type
3058dffb485Schristos {
3068dffb485Schristos   uint32_t ctt_name;		/* Reference to name in string table.  */
3078dffb485Schristos   uint32_t ctt_info;		/* Encoded kind, variant length (see below).  */
3088dffb485Schristos #ifndef __GNUC__
3098dffb485Schristos union
3108dffb485Schristos   {
3118dffb485Schristos     uint32_t _size;		/* Always CTF_LSIZE_SENT.  */
3128dffb485Schristos     uint32_t _type;		/* Do not use.  */
3138dffb485Schristos   } _u;
3148dffb485Schristos #else
3158dffb485Schristos   __extension__
3168dffb485Schristos   union
3178dffb485Schristos   {
3188dffb485Schristos     uint32_t ctt_size;		/* Always CTF_LSIZE_SENT.  */
3198dffb485Schristos     uint32_t ctt_type;		/* Do not use.  */
3208dffb485Schristos   };
3218dffb485Schristos #endif
3228dffb485Schristos   uint32_t ctt_lsizehi;		/* High 32 bits of type size in bytes.  */
3238dffb485Schristos   uint32_t ctt_lsizelo;		/* Low 32 bits of type size in bytes.  */
3248dffb485Schristos } ctf_type_t;
3258dffb485Schristos 
3268dffb485Schristos #ifndef __GNUC__
3278dffb485Schristos #define ctt_size _u._size	/* For fundamental types that have a size.  */
3288dffb485Schristos #define ctt_type _u._type	/* For types that reference another type.  */
3298dffb485Schristos #endif
3308dffb485Schristos 
3318dffb485Schristos /* The following macros and inline functions compose and decompose values for
3328dffb485Schristos    ctt_info and ctt_name, as well as other structures that contain name
3338dffb485Schristos    references.  Use outside libdtrace-ctf itself is explicitly for access to CTF
3348dffb485Schristos    files directly: types returned from the library will always appear to be
3358dffb485Schristos    CTF_V2.
3368dffb485Schristos 
3378dffb485Schristos    v1: (transparently upgraded to v2 at open time: may be compiled out of the
3388dffb485Schristos    library)
3398dffb485Schristos                ------------------------
3408dffb485Schristos    ctt_info:   | kind | isroot | vlen |
3418dffb485Schristos                ------------------------
3428dffb485Schristos                15   11    10    9     0
3438dffb485Schristos 
3448dffb485Schristos    v2:
3458dffb485Schristos                ------------------------
3468dffb485Schristos    ctt_info:   | kind | isroot | vlen |
3478dffb485Schristos                ------------------------
3488dffb485Schristos                31    26    25  24     0
3498dffb485Schristos 
3508dffb485Schristos    CTF_V1 and V2 _INFO_VLEN have the same interface:
3518dffb485Schristos 
3528dffb485Schristos    kind = CTF_*_INFO_KIND(c.ctt_info);     <-- CTF_K_* value (see below)
3538dffb485Schristos    vlen = CTF_*_INFO_VLEN(fp, c.ctt_info); <-- length of variable data list
3548dffb485Schristos 
3558dffb485Schristos    stid = CTF_NAME_STID(c.ctt_name);     <-- string table id number (0 or 1)
3568dffb485Schristos    offset = CTF_NAME_OFFSET(c.ctt_name); <-- string table byte offset
3578dffb485Schristos 
3588dffb485Schristos    c.ctt_info = CTF_TYPE_INFO(kind, vlen);
3598dffb485Schristos    c.ctt_name = CTF_TYPE_NAME(stid, offset);  */
3608dffb485Schristos 
3618dffb485Schristos #define CTF_V1_INFO_KIND(info)		(((info) & 0xf800) >> 11)
3628dffb485Schristos #define CTF_V1_INFO_ISROOT(info)	(((info) & 0x0400) >> 10)
3638dffb485Schristos #define CTF_V1_INFO_VLEN(info)		(((info) & CTF_MAX_VLEN_V1))
3648dffb485Schristos 
3658dffb485Schristos #define CTF_V2_INFO_KIND(info)		(((info) & 0xfc000000) >> 26)
3668dffb485Schristos #define CTF_V2_INFO_ISROOT(info)	(((info) & 0x2000000) >> 25)
3678dffb485Schristos #define CTF_V2_INFO_VLEN(info)		(((info) & CTF_MAX_VLEN))
3688dffb485Schristos 
3698dffb485Schristos #define CTF_NAME_STID(name)		((name) >> 31)
3708dffb485Schristos #define CTF_NAME_OFFSET(name)		((name) & CTF_MAX_NAME)
3714b169a6bSchristos #define CTF_SET_STID(name, stid)	((name) | ((unsigned int) stid) << 31)
3728dffb485Schristos 
3738dffb485Schristos /* V2 only. */
3748dffb485Schristos #define CTF_TYPE_INFO(kind, isroot, vlen) \
3758dffb485Schristos 	(((kind) << 26) | (((isroot) ? 1 : 0) << 25) | ((vlen) & CTF_MAX_VLEN))
3768dffb485Schristos 
3778dffb485Schristos #define CTF_TYPE_NAME(stid, offset) \
3788dffb485Schristos 	(((stid) << 31) | ((offset) & CTF_MAX_NAME))
3798dffb485Schristos 
3808dffb485Schristos /* The next set of macros are for public consumption only.  Not used internally,
3818dffb485Schristos    since the relevant type boundary is dependent upon the version of the file at
3828dffb485Schristos    *opening* time, not the version after transparent upgrade.  Use
3838dffb485Schristos    ctf_type_isparent() / ctf_type_ischild() for that.  */
3848dffb485Schristos 
3858dffb485Schristos #define CTF_V2_TYPE_ISPARENT(fp, id)	((id) <= CTF_MAX_PTYPE)
3868dffb485Schristos #define CTF_V2_TYPE_ISCHILD(fp, id)	((id) > CTF_MAX_PTYPE)
3878dffb485Schristos #define CTF_V2_TYPE_TO_INDEX(id)	((id) & CTF_MAX_PTYPE)
3888dffb485Schristos #define CTF_V2_INDEX_TO_TYPE(id, child) ((child) ? ((id) | (CTF_MAX_PTYPE+1)) : (id))
3898dffb485Schristos 
3908dffb485Schristos #define CTF_V1_TYPE_ISPARENT(fp, id)	((id) <= CTF_MAX_PTYPE_V1)
3918dffb485Schristos #define CTF_V1_TYPE_ISCHILD(fp, id)	((id) > CTF_MAX_PTYPE_V1)
3928dffb485Schristos #define CTF_V1_TYPE_TO_INDEX(id)	((id) & CTF_MAX_PTYPE_V1)
3938dffb485Schristos #define CTF_V1_INDEX_TO_TYPE(id, child) ((child) ? ((id) | (CTF_MAX_PTYPE_V1+1)) : (id))
3948dffb485Schristos 
3958dffb485Schristos /* Valid for both V1 and V2. */
3968dffb485Schristos #define CTF_TYPE_LSIZE(cttp) \
3978dffb485Schristos 	(((uint64_t)(cttp)->ctt_lsizehi) << 32 | (cttp)->ctt_lsizelo)
3988dffb485Schristos #define CTF_SIZE_TO_LSIZE_HI(size)	((uint32_t)((uint64_t)(size) >> 32))
3998dffb485Schristos #define CTF_SIZE_TO_LSIZE_LO(size)	((uint32_t)(size))
4008dffb485Schristos 
4018dffb485Schristos #define CTF_STRTAB_0	0	/* String table id 0 (in-CTF).  */
4028dffb485Schristos #define CTF_STRTAB_1	1	/* String table id 1 (ELF strtab).  */
4038dffb485Schristos 
4048dffb485Schristos /* Values for CTF_TYPE_KIND().  If the kind has an associated data list,
4058dffb485Schristos    CTF_INFO_VLEN() will extract the number of elements in the list, and
4068dffb485Schristos    the type of each element is shown in the comments below. */
4078dffb485Schristos 
4084b169a6bSchristos #define CTF_K_UNKNOWN	0	/* Unknown type (used for padding and
4094b169a6bSchristos 				   unrepresentable types).  */
4108dffb485Schristos #define CTF_K_INTEGER	1	/* Variant data is CTF_INT_DATA (see below).  */
4118dffb485Schristos #define CTF_K_FLOAT	2	/* Variant data is CTF_FP_DATA (see below).  */
4128dffb485Schristos #define CTF_K_POINTER	3	/* ctt_type is referenced type.  */
4138dffb485Schristos #define CTF_K_ARRAY	4	/* Variant data is single ctf_array_t.  */
4148dffb485Schristos #define CTF_K_FUNCTION	5	/* ctt_type is return type, variant data is
4158dffb485Schristos 				   list of argument types (unsigned short's for v1,
4168dffb485Schristos 				   uint32_t's for v2).  */
4178dffb485Schristos #define CTF_K_STRUCT	6	/* Variant data is list of ctf_member_t's.  */
4188dffb485Schristos #define CTF_K_UNION	7	/* Variant data is list of ctf_member_t's.  */
4198dffb485Schristos #define CTF_K_ENUM	8	/* Variant data is list of ctf_enum_t's.  */
4208dffb485Schristos #define CTF_K_FORWARD	9	/* No additional data; ctt_name is tag.  */
4218dffb485Schristos #define CTF_K_TYPEDEF	10	/* ctt_type is referenced type.  */
4228dffb485Schristos #define CTF_K_VOLATILE	11	/* ctt_type is base type.  */
4238dffb485Schristos #define CTF_K_CONST	12	/* ctt_type is base type.  */
4248dffb485Schristos #define CTF_K_RESTRICT	13	/* ctt_type is base type.  */
4258dffb485Schristos #define CTF_K_SLICE	14	/* Variant data is a ctf_slice_t.  */
4268dffb485Schristos 
4278dffb485Schristos #define CTF_K_MAX	63	/* Maximum possible (V2) CTF_K_* value.  */
4288dffb485Schristos 
4298dffb485Schristos /* Values for ctt_type when kind is CTF_K_INTEGER.  The flags, offset in bits,
4308dffb485Schristos    and size in bits are encoded as a single word using the following macros.
4318dffb485Schristos    (However, you can also encode the offset and bitness in a slice.)  */
4328dffb485Schristos 
4338dffb485Schristos #define CTF_INT_ENCODING(data) (((data) & 0xff000000) >> 24)
4348dffb485Schristos #define CTF_INT_OFFSET(data)   (((data) & 0x00ff0000) >> 16)
4358dffb485Schristos #define CTF_INT_BITS(data)     (((data) & 0x0000ffff))
4368dffb485Schristos 
4378dffb485Schristos #define CTF_INT_DATA(encoding, offset, bits) \
4388dffb485Schristos        (((encoding) << 24) | ((offset) << 16) | (bits))
4398dffb485Schristos 
4408dffb485Schristos #define CTF_INT_SIGNED	0x01	/* Integer is signed (otherwise unsigned).  */
4418dffb485Schristos #define CTF_INT_CHAR	0x02	/* Character display format.  */
4428dffb485Schristos #define CTF_INT_BOOL	0x04	/* Boolean display format.  */
4438dffb485Schristos #define CTF_INT_VARARGS	0x08	/* Varargs display format.  */
4448dffb485Schristos 
4458dffb485Schristos /* Use CTF_CHAR to produce a char that agrees with the system's native
4468dffb485Schristos    char signedness.  */
4478dffb485Schristos #if CHAR_MIN == 0
4488dffb485Schristos # define CTF_CHAR (CTF_INT_CHAR)
4498dffb485Schristos #else
4508dffb485Schristos # define CTF_CHAR (CTF_INT_CHAR | CTF_INT_SIGNED)
4518dffb485Schristos #endif
4528dffb485Schristos 
4538dffb485Schristos /* Values for ctt_type when kind is CTF_K_FLOAT.  The encoding, offset in bits,
4548dffb485Schristos    and size in bits are encoded as a single word using the following macros.
4558dffb485Schristos    (However, you can also encode the offset and bitness in a slice.)  */
4568dffb485Schristos 
4578dffb485Schristos #define CTF_FP_ENCODING(data)  (((data) & 0xff000000) >> 24)
4588dffb485Schristos #define CTF_FP_OFFSET(data)    (((data) & 0x00ff0000) >> 16)
4598dffb485Schristos #define CTF_FP_BITS(data)      (((data) & 0x0000ffff))
4608dffb485Schristos 
4618dffb485Schristos #define CTF_FP_DATA(encoding, offset, bits) \
4628dffb485Schristos        (((encoding) << 24) | ((offset) << 16) | (bits))
4638dffb485Schristos 
4648dffb485Schristos /* Variant data when kind is CTF_K_FLOAT is an encoding in the top eight bits.  */
4658dffb485Schristos #define CTF_FP_ENCODING(data)	(((data) & 0xff000000) >> 24)
4668dffb485Schristos 
4678dffb485Schristos #define CTF_FP_SINGLE	1	/* IEEE 32-bit float encoding.  */
4688dffb485Schristos #define CTF_FP_DOUBLE	2	/* IEEE 64-bit float encoding.  */
4698dffb485Schristos #define CTF_FP_CPLX	3	/* Complex encoding.  */
4708dffb485Schristos #define CTF_FP_DCPLX	4	/* Double complex encoding.  */
4718dffb485Schristos #define CTF_FP_LDCPLX	5	/* Long double complex encoding.  */
4728dffb485Schristos #define CTF_FP_LDOUBLE	6	/* Long double encoding.  */
4738dffb485Schristos #define CTF_FP_INTRVL	7	/* Interval (2x32-bit) encoding.  */
4748dffb485Schristos #define CTF_FP_DINTRVL	8	/* Double interval (2x64-bit) encoding.  */
4758dffb485Schristos #define CTF_FP_LDINTRVL	9	/* Long double interval (2x128-bit) encoding.  */
4768dffb485Schristos #define CTF_FP_IMAGRY	10	/* Imaginary (32-bit) encoding.  */
4778dffb485Schristos #define CTF_FP_DIMAGRY	11	/* Long imaginary (64-bit) encoding.  */
4788dffb485Schristos #define CTF_FP_LDIMAGRY	12	/* Long double imaginary (128-bit) encoding.  */
4798dffb485Schristos 
4808dffb485Schristos #define CTF_FP_MAX	12	/* Maximum possible CTF_FP_* value */
4818dffb485Schristos 
4828dffb485Schristos /* A slice increases the offset and reduces the bitness of the referenced
4838dffb485Schristos    ctt_type, which must be a type which has an encoding (fp, int, or enum).  We
4848dffb485Schristos    also store the referenced type in here, because it is easier to keep the
4858dffb485Schristos    ctt_size correct for the slice than to shuffle the size into here and keep
4868dffb485Schristos    the ctt_type where it is for other types.
4878dffb485Schristos 
4888dffb485Schristos    In a future version, where we loosen requirements on alignment in the CTF
4898dffb485Schristos    file, the cts_offset and cts_bits will be chars: but for now they must be
4908dffb485Schristos    shorts or everything after a slice will become unaligned.  */
4918dffb485Schristos 
4928dffb485Schristos typedef struct ctf_slice
4938dffb485Schristos {
4948dffb485Schristos   uint32_t cts_type;
4958dffb485Schristos   unsigned short cts_offset;
4968dffb485Schristos   unsigned short cts_bits;
4978dffb485Schristos } ctf_slice_t;
4988dffb485Schristos 
4998dffb485Schristos typedef struct ctf_array_v1
5008dffb485Schristos {
5018dffb485Schristos   unsigned short cta_contents;	/* Reference to type of array contents.  */
5028dffb485Schristos   unsigned short cta_index;	/* Reference to type of array index.  */
5038dffb485Schristos   uint32_t cta_nelems;		/* Number of elements.  */
5048dffb485Schristos } ctf_array_v1_t;
5058dffb485Schristos 
5068dffb485Schristos typedef struct ctf_array
5078dffb485Schristos {
5088dffb485Schristos   uint32_t cta_contents;	/* Reference to type of array contents.  */
5098dffb485Schristos   uint32_t cta_index;		/* Reference to type of array index.  */
5108dffb485Schristos   uint32_t cta_nelems;		/* Number of elements.  */
5118dffb485Schristos } ctf_array_t;
5128dffb485Schristos 
5138dffb485Schristos /* Most structure members have bit offsets that can be expressed using a short.
5148dffb485Schristos    Some don't.  ctf_member_t is used for structs which cannot contain any of
5158dffb485Schristos    these large offsets, whereas ctf_lmember_t is used in the latter case.  If
5168dffb485Schristos    any member of a given struct has an offset that cannot be expressed using a
5178dffb485Schristos    uint32_t, all members will be stored as type ctf_lmember_t.  This is expected
5188dffb485Schristos    to be very rare (but nonetheless possible).  */
5198dffb485Schristos 
5208dffb485Schristos #define CTF_LSTRUCT_THRESH	536870912
5218dffb485Schristos 
5228dffb485Schristos /* In v1, the same is true, except that lmembers are used for structs >= 8192
5238dffb485Schristos    bytes in size.  (The ordering of members in the ctf_member_* structures is
5248dffb485Schristos    different to improve padding.)  */
5258dffb485Schristos 
5268dffb485Schristos #define CTF_LSTRUCT_THRESH_V1	8192
5278dffb485Schristos 
5288dffb485Schristos typedef struct ctf_member_v1
5298dffb485Schristos {
5308dffb485Schristos   uint32_t ctm_name;		/* Reference to name in string table.  */
5318dffb485Schristos   unsigned short ctm_type;	/* Reference to type of member.  */
5328dffb485Schristos   unsigned short ctm_offset;	/* Offset of this member in bits.  */
5338dffb485Schristos } ctf_member_v1_t;
5348dffb485Schristos 
5358dffb485Schristos typedef struct ctf_lmember_v1
5368dffb485Schristos {
5378dffb485Schristos   uint32_t ctlm_name;		/* Reference to name in string table.  */
5388dffb485Schristos   unsigned short ctlm_type;	/* Reference to type of member.  */
5398dffb485Schristos   unsigned short ctlm_pad;	/* Padding.  */
5408dffb485Schristos   uint32_t ctlm_offsethi;	/* High 32 bits of member offset in bits.  */
5418dffb485Schristos   uint32_t ctlm_offsetlo;	/* Low 32 bits of member offset in bits.  */
5428dffb485Schristos } ctf_lmember_v1_t;
5438dffb485Schristos 
5448dffb485Schristos typedef struct ctf_member_v2
5458dffb485Schristos {
5468dffb485Schristos   uint32_t ctm_name;		/* Reference to name in string table.  */
5478dffb485Schristos   uint32_t ctm_offset;		/* Offset of this member in bits.  */
5488dffb485Schristos   uint32_t ctm_type;		/* Reference to type of member.  */
5498dffb485Schristos } ctf_member_t;
5508dffb485Schristos 
5518dffb485Schristos typedef struct ctf_lmember_v2
5528dffb485Schristos {
5538dffb485Schristos   uint32_t ctlm_name;		/* Reference to name in string table.  */
5548dffb485Schristos   uint32_t ctlm_offsethi;	/* High 32 bits of member offset in bits.  */
5558dffb485Schristos   uint32_t ctlm_type;		/* Reference to type of member.  */
5568dffb485Schristos   uint32_t ctlm_offsetlo;	/* Low 32 bits of member offset in bits.  */
5578dffb485Schristos } ctf_lmember_t;
5588dffb485Schristos 
5598dffb485Schristos #define	CTF_LMEM_OFFSET(ctlmp) \
5608dffb485Schristos 	(((uint64_t)(ctlmp)->ctlm_offsethi) << 32 | (ctlmp)->ctlm_offsetlo)
5618dffb485Schristos #define	CTF_OFFSET_TO_LMEMHI(offset)	((uint32_t)((uint64_t)(offset) >> 32))
5628dffb485Schristos #define	CTF_OFFSET_TO_LMEMLO(offset)	((uint32_t)(offset))
5638dffb485Schristos 
5648dffb485Schristos typedef struct ctf_enum
5658dffb485Schristos {
5668dffb485Schristos   uint32_t cte_name;		/* Reference to name in string table.  */
5678dffb485Schristos   int32_t cte_value;		/* Value associated with this name.  */
5688dffb485Schristos } ctf_enum_t;
5698dffb485Schristos 
5704b169a6bSchristos /* The ctf_archive is a collection of ctf_dict_t's stored together. The format
5718dffb485Schristos    is suitable for mmap()ing: this control structure merely describes the
5728dffb485Schristos    mmap()ed archive (and overlaps the first few bytes of it), hence the
5738dffb485Schristos    greater care taken with integral types.  All CTF files in an archive
5748dffb485Schristos    must have the same data model.  (This is not validated.)
5758dffb485Schristos 
5768dffb485Schristos    All integers in this structure are stored in little-endian byte order.
5778dffb485Schristos 
5788dffb485Schristos    The code relies on the fact that everything in this header is a uint64_t
5798dffb485Schristos    and thus the header needs no padding (in particular, that no padding is
5808dffb485Schristos    needed between ctfa_ctfs and the unnamed ctfa_archive_modent array
5818dffb485Schristos    that follows it).
5828dffb485Schristos 
5838dffb485Schristos    This is *not* the same as the data structure returned by the ctf_arc_*()
5848dffb485Schristos    functions:  this is the low-level on-disk representation.  */
5858dffb485Schristos 
5868dffb485Schristos #define CTFA_MAGIC 0x8b47f2a4d7623eeb	/* Random.  */
5878dffb485Schristos struct ctf_archive
5888dffb485Schristos {
5898dffb485Schristos   /* Magic number.  (In loaded files, overwritten with the file size
5908dffb485Schristos      so ctf_arc_close() knows how much to munmap()).  */
5918dffb485Schristos   uint64_t ctfa_magic;
5928dffb485Schristos 
5938dffb485Schristos   /* CTF data model.  */
5948dffb485Schristos   uint64_t ctfa_model;
5958dffb485Schristos 
5964b169a6bSchristos   /* Number of CTF dicts in the archive.  */
5974b169a6bSchristos   uint64_t ctfa_ndicts;
5988dffb485Schristos 
5998dffb485Schristos   /* Offset of the name table.  */
6008dffb485Schristos   uint64_t ctfa_names;
6018dffb485Schristos 
602*e663ba6eSchristos   /* Offset of the CTF table.  Each element starts with a size (a little-
603*e663ba6eSchristos      endian uint64_t) then a ctf_dict_t of that size.  */
6048dffb485Schristos   uint64_t ctfa_ctfs;
6058dffb485Schristos };
6068dffb485Schristos 
607*e663ba6eSchristos /* An array of ctfa_ndicts of this structure lies at
608*e663ba6eSchristos    ctf_archive[sizeof(struct ctf_archive)] and gives the ctfa_ctfs or
6094b169a6bSchristos    ctfa_names-relative offsets of each name or ctf_dict_t.  */
6108dffb485Schristos 
6118dffb485Schristos typedef struct ctf_archive_modent
6128dffb485Schristos {
6138dffb485Schristos   uint64_t name_offset;
6148dffb485Schristos   uint64_t ctf_offset;
6158dffb485Schristos } ctf_archive_modent_t;
6168dffb485Schristos 
6178dffb485Schristos #ifdef	__cplusplus
6188dffb485Schristos }
6198dffb485Schristos #endif
6208dffb485Schristos 
6218dffb485Schristos #endif				/* _CTF_H */
622