ctf.h 25 KB

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