Move /var/svc/log to /var/log/svc
[unleashed/lotheac.git] / share / man / man4 / ctf.4
blob1319bb6ac6adc5be707bceca322e816336c293a1
1 .\"
2 .\" This file and its contents are supplied under the terms of the
3 .\" Common Development and Distribution License ("CDDL"), version 1.0.
4 .\" You may only use this file in accordance with the terms of version
5 .\" 1.0 of the CDDL.
6 .\"
7 .\" A full copy of the text of the CDDL should have accompanied this
8 .\" source.  A copy of the CDDL is also available via the Internet at
9 .\" http://www.illumos.org/license/CDDL.
10 .\"
11 .\"
12 .\" Copyright (c) 2014 Joyent, Inc.
13 .\"
14 .Dd Sep 26, 2014
15 .Dt CTF 4
16 .Os
17 .Sh NAME
18 .Nm ctf
19 .Nd Compact C Type Format
20 .Sh SYNOPSIS
21 .In sys/ctf.h
22 .Sh DESCRIPTION
23 .Nm
24 is designed to be a compact representation of the C programming
25 language's type information focused on serving the needs of dynamic
26 tracing, debuggers, and other in-situ and post-mortem introspection
27 tools.
28 .Nm
29 data is generally included in
30 .Sy ELF
31 objects and is tagged as
32 .Sy SHT_PROGBITS
33 to ensure that the data is accessible in a running process and in subsequent
34 core dumps, if generated.
35 .Lp
36 The
37 .Nm
38 data contained in each file has information about the layout and
39 sizes of C types, including intrinsic types, enumerations, structures,
40 typedefs, and unions, that are used by the corresponding
41 .Sy ELF
42 object.
43 The
44 .Nm
45 data may also include information about the types of global objects and
46 the return type and arguments of functions in the symbol table.
47 .Lp
48 Because a
49 .Nm
50 file is often embedded inside a file, rather than being a standalone
51 file itself, it may also be referred to as a
52 .Nm
53 .Sy container .
54 .Lp
55 On illumos systems,
56 .Nm
57 data is consumed by multiple programs.
58 It can be used by the modular debugger,
59 .Xr mdb 1 ,
60 as well as by
61 .Xr dtrace 8 .
62 Programmatic access to
63 .Nm
64 data can be obtained through
65 .Xr libctf 3LIB .
66 .Lp
67 The
68 .Nm
69 file format is broken down into seven different sections.
70 The first section is the
71 .Sy preamble
72 and
73 .Sy header ,
74 which describes the version of the
75 .Nm
76 file, links it has to other
77 .Nm
78 files, and the sizes of the other sections.
79 The next section is the
80 .Sy label
81 section,
82 which provides a way of identifying similar groups of
83 .Nm
84 data across multiple files.
85 This is followed by the
86 .Sy object
87 information section, which describes the type of global
88 symbols.
89 The subsequent section is the
90 .Sy function
91 information section, which describes the return
92 types and arguments of functions.
93 The next section is the
94 .Sy type
95 information section, which describes
96 the format and layout of the C types themselves, and finally the last
97 section is the
98 .Sy string
99 section, which contains the names of types, enumerations, members, and
100 labels.
102 While strictly speaking, only the
103 .Sy preamble
105 .Sy header
106 are required, to be actually useful, both the type and string
107 sections are necessary.
111 file may contain all of the type information that it requires, or it
112 may optionally refer to another
114 file which holds the remaining types.
115 When a
117 file refers to another file, it is called the
118 .Sy child
119 and the file it refers to is called the
120 .Sy parent .
121 A given file may only refer to one parent.
122 This process is called
123 .Em uniquification
124 because it ensures each child only has type information that is
125 unique to it.
126 A common example of this is that most kernel modules in illumos are uniquified
127 against the kernel module
128 .Sy genunix
129 and the type information that comes from the
130 .Sy IP
131 module.
132 This means that a module only has types that are unique to itself and the most
133 common types in the kernel are not duplicated.
134 .Sh FILE FORMAT
135 This documents version
136 .Em two
137 of the
139 file format.
140 All applications and tools currently produce and operate on this version.
142 The file format can be summarized with the following image, the
143 following sections will cover this in more detail.
144 .Bd -literal
146          +-------------+  0t0
147 +--------| Preamble    |
148 |        +-------------+  0t4
149 |+-------| Header      |
150 ||       +-------------+  0t36 + cth_lbloff
151 ||+------| Labels      |
152 |||      +-------------+  0t36 + cth_objtoff
153 |||+-----| Objects     |
154 ||||     +-------------+  0t36 + cth_funcoff
155 ||||+----| Functions   |
156 |||||    +-------------+  0t36 + cth_typeoff
157 |||||+---| Types       |
158 ||||||   +-------------+  0t36 + cth_stroff
159 ||||||+--| Strings     |
160 |||||||  +-------------+  0t36 + cth_stroff + cth_strlen
161 |||||||
162 |||||||
163 |||||||
164 |||||||    +-- magic -   vers   flags
165 |||||||    |          |    |      |
166 |||||||   +------+------+------+------+
167 +---------| 0xcf | 0xf1 | 0x02 | 0x00 |
168  ||||||   +------+------+------+------+
169  ||||||   0      1      2      3      4
170  ||||||
171  ||||||    + parent label        + objects
172  ||||||    |       + parent name |     + functions    + strings
173  ||||||    |       |     + label |     |      + types |       + strlen
174  ||||||    |       |     |       |     |      |       |       |
175  ||||||   +------+------+------+------+------+-------+-------+-------+
176  +--------| 0x00 | 0x00 | 0x00 | 0x08 | 0x36 | 0x110 | 0x5f4 | 0x611 |
177   |||||   +------+------+------+------+------+-------+-------+-------+
178   |||||   0x04   0x08   0x0c   0x10   0x14    0x18    0x1c    0x20   0x24
179   |||||
180   |||||         + Label name
181   |||||         |       + Label type
182   |||||         |       |       + Next label
183   |||||         |       |       |
184   |||||       +-------+------+-----+
185   +-----------| 0x01  | 0x42 | ... |
186    ||||       +-------+------+-----+
187    ||||  cth_lbloff   +0x4   +0x8  cth_objtoff
188    ||||
189    ||||
190    |||| Symidx  0t15   0t43   0t44
191    ||||       +------+------+------+-----+
192    +----------| 0x00 | 0x42 | 0x36 | ... |
193     |||       +------+------+------+-----+
194     ||| cth_objtoff  +0x2   +0x4   +0x6   cth_funcoff
195     |||
196     |||        + CTF_TYPE_INFO         + CTF_TYPE_INFO
197     |||        |        + Return type  |
198     |||        |        |       + arg0 |
199     |||       +--------+------+------+-----+
200     +---------| 0x2c10 | 0x08 | 0x0c | ... |
201      ||       +--------+------+------+-----+
202      || cth_funcff     +0x2   +0x4   +0x6  cth_typeoff
203      ||
204      ||         + ctf_stype_t for type 1
205      ||         |  integer           + integer encoding
206      ||         |                    |          + ctf_stype_t for type 2
207      ||         |                    |          |
208      ||       +--------------------+-----------+-----+
209      +--------| 0x19 * 0xc01 * 0x0 | 0x1000000 | ... |
210       |       +--------------------+-----------+-----+
211       | cth_typeoff               +0x08      +0x0c  cth_stroff
212       |
213       |     +--- str 0
214       |     |    +--- str 1       + str 2
215       |     |    |                |
216       |     v    v                v
217       |   +----+---+---+---+----+---+---+---+---+---+----+
218       +---| \\0 | i | n | t | \\0 | f | o | o | _ | t | \\0 |
219           +----+---+---+---+----+---+---+---+---+---+----+
220           0    1   2   3   4    5   6   7   8   9   10   11
223 Every
225 file begins with a
226 .Sy preamble ,
227 followed by a
228 .Sy header .
230 .Sy preamble
231 is defined as follows:
232 .Bd -literal
233 typedef struct ctf_preamble {
234         ushort_t ctp_magic;     /* magic number (CTF_MAGIC) */
235         uchar_t ctp_version;    /* data format version number (CTF_VERSION) */
236         uchar_t ctp_flags;      /* flags (see below) */
237 } ctf_preamble_t;
241 .Sy preamble
242 is four bytes long and must be four byte aligned.
243 This
244 .Sy preamble
245 defines the version of the
247 file which defines the format of the rest of the header.
248 While the header may change in subsequent versions, the preamble will not change
249 across versions, though the interpretation of its flags may change from
250 version to version.
252 .Em ctp_magic
253 member defines the magic number for the
255 file format.
256 This must always be
257 .Li 0xcff1 .
258 If another value is encountered, then the file should not be treated as
261 file.
263 .Em ctp_version
264 member defines the version of the
266 file.
267 The current version is
268 .Li 2 .
269 It is possible to encounter an unsupported version.
270 In that case, software should not try to parse the format, as it may have
271 changed.
272 Finally, the
273 .Em ctp_flags
274 member describes aspects of the file which modify its interpretation.
275 The following flags are currently defined:
276 .Bd -literal
277 #define CTF_F_COMPRESS          0x01
280 The flag
281 .Sy CTF_F_COMPRESS
282 indicates that the body of the
284 file, all the data following the
285 .Sy header ,
286 has been compressed through the
287 .Sy zlib
288 library and its
289 .Sy deflate
290 algorithm.
291 If this flag is not present, then the body has not been compressed and no
292 special action is needed to interpret it.
293 All offsets into the data as described by
294 .Sy header ,
295 always refer to the
296 .Sy uncompressed
297 data.
299 In version two of the
301 file format, the
302 .Sy header
303 denotes whether whether or not this
305 file is the child of another
307 file and also indicates the size of the remaining sections.
308 The structure for the
309 .Sy header ,
310 logically contains a copy of the
311 .Sy preamble
312 and the two have a combined size of 36 bytes.
313 .Bd -literal
314 typedef struct ctf_header {
315         ctf_preamble_t cth_preamble;
316         uint_t cth_parlabel;    /* ref to name of parent lbl uniq'd against */
317         uint_t cth_parname;     /* ref to basename of parent */
318         uint_t cth_lbloff;      /* offset of label section */
319         uint_t cth_objtoff;     /* offset of object section */
320         uint_t cth_funcoff;     /* offset of function section */
321         uint_t cth_typeoff;     /* offset of type section */
322         uint_t cth_stroff;      /* offset of string section */
323         uint_t cth_strlen;      /* length of string section in bytes */
324 } ctf_header_t;
327 After the
328 .Sy preamble ,
329 the next two members
330 .Em cth_parlablel
332 .Em cth_parname ,
333 are used to identify the parent.
334 The value of both members are offsets into the
335 .Sy string
336 section which point to the start of a null-terminated string.
337 For more information on the encoding of strings, see the subsection on
338 .Sx String Identifiers .
339 If the value of either is zero, then there is no entry for that
340 member.
341 If the member
342 .Em cth_parlabel
343 is set, then the
344 .Em ctf_parname
345 member must be set, otherwise it will not be possible to find the
346 parent.
348 .Em ctf_parname
349 is set, it is not necessary to define
350 .Em cth_parlabel ,
351 as the parent may not have a label.
352 For more information on labels and their interpretation, see
353 .Sx The Label Section .
355 The remaining members (excepting
356 .Em cth_strlen )
357 describe the beginning of the corresponding sections.
358 These offsets are relative to the end of the
359 .Sy header .
360 Therefore, something with an offset of 0 is at an offset of thirty-six
361 bytes relative to the start of the
363 file.
364 The difference between members indicates the size of the section itself.
365 Different offsets have different alignment requirements.
366 The start of the
367 .Em cth_objotoff
369 .Em cth_funcoff
370 must be two byte aligned, while the sections
371 .Em cth_lbloff
373 .Em cth_typeoff
374 must be four-byte aligned.
375 The section
376 .Em cth_stroff
377 has no alignment requirements.
378 To calculate the size of a given section, excepting the
379 .Sy string
380 section, one should subtract the offset of the section from the following one.
381 For example, the size of the
382 .Sy types
383 section can be calculated by subtracting
384 .Em cth_stroff
385 from
386 .Em cth_typeoff .
388 Finally, the member
389 .Em cth_strlen
390 describes the length of the string section itself.
391 From it, you can also calculate the size of the entire
393 file by adding together the size of the
394 .Sy ctf_header_t ,
395 the offset of the string section in
396 .Em cth_stroff ,
397 and the size of the string section in
398 .Em cth_srlen .
399 .Ss Type Identifiers
400 Through the
401 .Nm ctf
402 data, types are referred to by identifiers.
403 A given
405 file supports up to 32767 (0x7fff) types.
406 The first valid type identifier is 0x1.
407 When a given
409 file is a child, indicated by a non-zero entry for the
410 .Sy header Ns 's
411 .Em cth_parname ,
412 then the first valid type identifier is 0x8000 and the last is 0xffff.
413 In this case, type identifiers 0x1 through 0x7fff are references to the
414 parent.
416 The type identifier zero is a sentinel value used to indicate that there
417 is no type information available or it is an unknown type.
419 Throughout the file format, the identifier is stored in different sized
420 values; however, the minimum size to represent a given identifier is a
421 .Sy uint16_t .
422 Other consumers of
424 information may use larger or opaque identifiers.
425 .Ss String Identifiers
426 String identifiers are always encoded as four byte unsigned integers
427 which are an offset into a string table.
430 format supports two different string tables which have an identifier of
431 zero or one.
432 This identifier is stored in the high-order bit of the unsigned four byte
433 offset.
434 Therefore, the maximum supported offset into one of these tables is 0x7ffffffff.
436 Table identifier zero, always refers to the
437 .Sy string
438 section in the CTF file itself.
439 String table identifier one refers to an external string table which is the ELF
440 string table for the ELF symbol table associated with the
442 container.
443 .Ss Type Encoding
444 Every
446 type begins with metadata encoded into a
447 .Sy uint16_t .
448 This encoded information tells us three different pieces of information:
449 .Bl -bullet -offset indent -compact
451 The kind of the type
453 Whether this type is a root type or not
455 The length of the variable data
458 The 16 bits that make up the encoding are broken down such that you have
459 five bits for the kind, one bit for indicating whether or not it is a
460 root type, and 10 bits for the variable length.
461 This is laid out as follows:
462 .Bd -literal -offset indent
463 +--------------------+
464 | kind | root | vlen |
465 +--------------------+
466 15   11   10   9    0
469 The current version of the file format defines 14 different kinds.
470 The interpretation of these different kinds will be discussed in the section
471 .Sx The Type Section .
472 If a kind is encountered that is not listed below, then it is not a valid
474 file.
475 The kinds are defined as follows:
476 .Bd -literal -offset indent
477 #define CTF_K_UNKNOWN   0
478 #define CTF_K_INTEGER   1
479 #define CTF_K_FLOAT     2
480 #define CTF_K_POINTER   3
481 #define CTF_K_ARRAY     4
482 #define CTF_K_FUNCTION  5
483 #define CTF_K_STRUCT    6
484 #define CTF_K_UNION     7
485 #define CTF_K_ENUM      8
486 #define CTF_K_FORWARD   9
487 #define CTF_K_TYPEDEF   10
488 #define CTF_K_VOLATILE  11
489 #define CTF_K_CONST     12
490 #define CTF_K_RESTRICT  13
493 Programs directly reference many types; however, other types are referenced
494 indirectly because they are part of some other structure.
495 These types that are referenced directly and used are called
496 .Sy root
497 types.
498 Other types may be used indirectly, for example, a program may reference
499 a structure directly, but not one of its members which has a type.
500 That type is not considered a
501 .Sy root
502 type.
503 If a type is a
504 .Sy root
505 type, then it will have bit 10 set.
507 The variable length section is specific to each kind and is discussed in the
508 section
509 .Sx The Type Section .
511 The following macros are useful for constructing and deconstructing the encoded
512 type information:
513 .Bd -literal -offset indent
515 #define CTF_MAX_VLEN    0x3ff
516 #define CTF_INFO_KIND(info)     (((info) & 0xf800) >> 11)
517 #define CTF_INFO_ISROOT(info)   (((info) & 0x0400) >> 10)
518 #define CTF_INFO_VLEN(info)     (((info) & CTF_MAX_VLEN))
520 #define CTF_TYPE_INFO(kind, isroot, vlen) \\
521         (((kind) << 11) | (((isroot) ? 1 : 0) << 10) | ((vlen) & CTF_MAX_VLEN))
523 .Ss The Label Section
524 When consuming
526 data, it is often useful to know whether two different
528 containers come from the same source base and version.
529 For example, when building illumos, there are many kernel modules that are built
530 against a single collection of source code.
531 A label is encoded into the
533 files that corresponds with the particular build.
534 This ensures that if files on the system were to become mixed up from multiple
535 releases, that they are not used together by tools, particularly when a child
536 needs to refer to a type in the parent.
537 Because they are linked used the type identifiers, if the wrong parent is used
538 then the wrong type will be encountered.
540 Each label is encoded in the file format using the following eight byte
541 structure:
542 .Bd -literal
543 typedef struct ctf_lblent {
544         uint_t ctl_label;       /* ref to name of label */
545         uint_t ctl_typeidx;     /* last type associated with this label */
546 } ctf_lblent_t;
549 Each label has two different components, a name and a type identifier.
550 The name is encoded in the
551 .Em ctl_label
552 member which is in the format defined in the section
553 .Sx String Identifiers .
554 Generally, the names of all labels are found in the internal string
555 section.
557 The type identifier encoded in the member
558 .Em ctl_typeidx
559 refers to the last type identifier that a label refers to in the current
560 file.
561 Labels only refer to types in the current file, if the
563 file is a child, then it will have the same label as its parent;
564 however, its label will only refer to its types, not its parents.
566 It is also possible, though rather uncommon, for a
568 file to have multiple labels.
569 Labels are placed one after another, every eight bytes.
570 When multiple labels are present, types may only belong to a single label.
571 .Ss The Object Section
572 The object section provides a mapping from ELF symbols of type
573 .Sy STT_OBJECT
574 in the symbol table to a type identifier.
575 Every entry in this section is a
576 .Sy uint16_t
577 which contains a type identifier as described in the section
578 .Sx Type Identifiers .
579 If there is no information for an object, then the type identifier 0x0
580 is stored for that entry.
582 To walk the object section, you need to have a corresponding
583 .Sy symbol table
584 in the ELF object that contains the
586 data.
587 Not every object is included in this section.
588 Specifically, when walking the symbol table.
589 An entry is skipped if it matches any of the following conditions:
591 .Bl -bullet -offset indent -compact
593 The symbol type is not
594 .Sy STT_OBJECT
596 The symbol's section index is
597 .Sy SHN_UNDEF
599 The symbol's name offset is zero
601 The symbol's section index is
602 .Sy SHN_ABS
603 and the value of the symbol is zero.
605 The symbol's name is
606 .Li _START_
608 .Li _END_ .
609 These are skipped because they are used for scoping local symbols in
610 ELF.
613 The following sample code shows an example of iterating the object
614 section and skipping the correct symbols:
615 .Bd -literal
616 #include <gelf.h>
617 #include <stdio.h>
620  * Given the start of the object section in the CTF file, the number of symbols,
621  * and the ELF Data sections for the symbol table and the string table, this
622  * prints the type identifiers that correspond to objects. Note, a more robust
623  * implementation should ensure that they don't walk beyond the end of the CTF
624  * object section.
625  */
626 static int
627 walk_symbols(uint16_t *objtoff, Elf_Data *symdata, Elf_Data *strdata,
628     long nsyms)
630         long i;
631         uintptr_t strbase = strdata->d_buf;
633         for (i = 1; i < nsyms; i++, objftoff++) {
634                 const char *name;
635                 GElf_Sym sym;
637                 if (gelf_getsym(symdata, i, &sym) == NULL)
638                         return (1);
640                 if (GELF_ST_TYPE(sym.st_info) != STT_OBJECT)
641                         continue;
642                 if (sym.st_shndx == SHN_UNDEF || sym.st_name == 0)
643                         continue;
644                 if (sym.st_shndx == SHN_ABS && sym.st_value == 0)
645                         continue;
646                 name = (const char *)(strbase + sym.st_name);
647                 if (strcmp(name, "_START_") == 0 || strcmp(name, "_END_") == 0)
648                         continue;
650                 (void) printf("Symbol %d has type %d\n", i, *objtoff);
651         }
653         return (0);
656 .Ss The Function Section
657 The function section of the
659 file encodes the types of both the function's arguments and the function's
660 return type.
661 Similar to
662 .Sx The Object Section ,
663 the function section encodes information for all symbols of type
664 .Sy STT_FUNCTION ,
665 excepting those that fit specific criteria.
666 Unlike with objects, because functions have a variable number of arguments, they
667 start with a type encoding as defined in
668 .Sx Type Encoding ,
669 which is the size of a
670 .Sy uint16_t .
671 For functions which have no type information available, they are encoded as
672 .Li CTF_TYPE_INFO(CTF_K_UNKNOWN, 0, 0) .
673 Functions with arguments are encoded differently.
674 Here, the variable length is turned into the number of arguments in the
675 function.
676 If a function is a
677 .Sy varargs
678 type function, then the number of arguments is increased by one.
679 Functions with type information are encoded as:
680 .Li CTF_TYPE_INFO(CTF_K_FUNCTION, 0, nargs) .
682 For functions that have no type information, nothing else is encoded, and the
683 next function is encoded.
684 For functions with type information, the next
685 .Sy uint16_t
686 is encoded with the type identifier of the return type of the function.
687 It is followed by each of the type identifiers of the arguments, if any exist,
688 in the order that they appear in the function.
689 Therefore, argument 0 is the first type identifier and so on.
690 When a function has a final varargs argument, that is encoded with the type
691 identifier of zero.
693 Like
694 .Sx The Object Section ,
695 the function section is encoded in the order of the symbol table.
696 It has similar, but slightly different considerations from objects.
697 While iterating the symbol table, if any of the following conditions are true,
698 then the entry is skipped and no corresponding entry is written:
700 .Bl -bullet -offset indent -compact
702 The symbol type is not
703 .Sy STT_FUNCTION
705 The symbol's section index is
706 .Sy SHN_UNDEF
708 The symbol's name offset is zero
710 The symbol's name is
711 .Li _START_
713 .Li _END_ .
714 These are skipped because they are used for scoping local symbols in
715 ELF.
717 .Ss The Type Section
718 The type section is the heart of the
720 data.
721 It encodes all of the information about the types themselves.
722 The base of the type information comes in two forms, a short form and a long
723 form, each of which may be followed by a variable number of arguments.
724 The following definitions describe the short and long forms:
725 .Bd -literal
726 #define CTF_MAX_SIZE    0xfffe  /* max size of a type in bytes */
727 #define CTF_LSIZE_SENT  0xffff  /* sentinel for ctt_size */
728 #define CTF_MAX_LSIZE   UINT64_MAX
730 typedef struct ctf_stype {
731         uint_t ctt_name;        /* reference to name in string table */
732         ushort_t ctt_info;      /* encoded kind, variant length */
733         union {
734                 ushort_t _size; /* size of entire type in bytes */
735                 ushort_t _type; /* reference to another type */
736         } _u;
737 } ctf_stype_t;
739 typedef struct ctf_type {
740         uint_t ctt_name;        /* reference to name in string table */
741         ushort_t ctt_info;      /* encoded kind, variant length */
742         union {
743                 ushort_t _size; /* always CTF_LSIZE_SENT */
744                 ushort_t _type; /* do not use */
745         } _u;
746         uint_t ctt_lsizehi;     /* high 32 bits of type size in bytes */
747         uint_t ctt_lsizelo;     /* low 32 bits of type size in bytes */
748 } ctf_type_t;
750 #define ctt_size _u._size       /* for fundamental types that have a size */
751 #define ctt_type _u._type       /* for types that reference another type */
754 Type sizes are stored in
755 .Sy bytes .
756 The basic small form uses a
757 .Sy ushort_t
758 to store the number of bytes.
759 If the number of bytes in a structure would exceed 0xfffe, then the alternate
760 form, the
761 .Sy ctf_type_t ,
762 is used instead.
763 To indicate that the larger form is being used, the member
764 .Em ctt_size
765 is set to value of
766 .Sy CTF_LSIZE_SENT
767 (0xffff).
768 In general, when going through the type section, consumers use the
769 .Sy ctf_type_t
770 structure, but pay attention to the value of the member
771 .Em ctt_size
772 to determine whether they should increment their scan by the size of the
773 .Sy ctf_stype_t
775 .Sy ctf_type_t .
776 Not all kinds of types use
777 .Sy ctt_size .
778 Those which do not, will always use the
779 .Sy ctf_stype_t
780 structure.
781 The individual sections for each kind have more information.
783 Types are written out in order.
784 Therefore the first entry encountered has a type id of 0x1, or 0x8000 if a
785 child.
786 The member
787 .Em ctt_name
788 is encoded as described in the section
789 .Sx String Identifiers .
790 The string that it points to is the name of the type.
791 If the identifier points to an empty string (one that consists solely of a null
792 terminator) then the type does not have a name, this is common with anonymous
793 structures and unions that only have a typedef to name them, as well as,
794 pointers and qualifiers.
796 The next member, the
797 .Em ctt_info ,
798 is encoded as described in the section
799 .Sx Type Encoding .
800 The types kind tells us how to interpret the remaining data in the
801 .Sy ctf_type_t
802 and any variable length data that may exist.
803 The rest of this section will be broken down into the interpretation of the
804 various kinds.
805 .Ss Encoding of Integers
806 Integers, which are of type
807 .Sy CTF_K_INTEGER ,
808 have no variable length arguments.
809 Instead, they are followed by a four byte
810 .Sy uint_t
811 which describes their encoding.
812 All integers must be encoded with a variable length of zero.
814 .Em ctt_size
815 member describes the length of the integer in bytes.
816 In general, integer sizes will be rounded up to the closest power of two.
818 The integer encoding contains three different pieces of information:
819 .Bl -bullet -offset indent -compact
821 The encoding of the integer
823 The offset in
824 .Sy bits
825 of the type
827 The size in
828 .Sy bits
829 of the type
832 This encoding can be expressed through the following macros:
833 .Bd -literal -offset indent
834 #define CTF_INT_ENCODING(data)  (((data) & 0xff000000) >> 24)
835 #define CTF_INT_OFFSET(data)    (((data) & 0x00ff0000) >> 16)
836 #define CTF_INT_BITS(data)      (((data) & 0x0000ffff))
838 #define CTF_INT_DATA(encoding, offset, bits) \\
839         (((encoding) << 24) | ((offset) << 16) | (bits))
842 The following flags are defined for the encoding at this time:
843 .Bd -literal -offset indent
844 #define CTF_INT_SIGNED          0x01
845 #define CTF_INT_CHAR            0x02
846 #define CTF_INT_BOOL            0x04
847 #define CTF_INT_VARARGS         0x08
850 By default, an integer is considered to be unsigned, unless it has the
851 .Sy CTF_INT_SIGNED
852 flag set.
853 If the flag
854 .Sy CTF_INT_CHAR
855 is set, that indicates that the integer is of a type that stores character
856 data, for example the intrinsic C type
857 .Sy char
858 would have the
859 .Sy CTF_INT_CHAR
860 flag set.
861 If the flag
862 .Sy CTF_INT_BOOL
863 is set, that indicates that the integer represents a boolean type.
864 For example, the intrinsic C type
865 .Sy _Bool
866 would have the
867 .Sy CTF_INT_BOOL
868 flag set.
869 Finally, the flag
870 .Sy CTF_INT_VARARGS
871 indicates that the integer is used as part of a variable number of arguments.
872 This encoding is rather uncommon.
873 .Ss Encoding of Floats
874 Floats, which are of type
875 .Sy CTF_K_FLOAT ,
876 are similar to their integer counterparts.
877 They have no variable length arguments and are followed by a four byte encoding
878 which describes the kind of float that exists.
880 .Em ctt_size
881 member is the size, in bytes, of the float.
882 The float encoding has three different pieces of information inside of it:
884 .Bl -bullet -offset indent -compact
886 The specific kind of float that exists
888 The offset in
889 .Sy bits
890 of the float
892 The size in
893 .Sy bits
894 of the float
897 This encoding can be expressed through the following macros:
898 .Bd -literal -offset indent
899 #define CTF_FP_ENCODING(data)   (((data) & 0xff000000) >> 24)
900 #define CTF_FP_OFFSET(data)     (((data) & 0x00ff0000) >> 16)
901 #define CTF_FP_BITS(data)       (((data) & 0x0000ffff))
903 #define CTF_FP_DATA(encoding, offset, bits) \\
904         (((encoding) << 24) | ((offset) << 16) | (bits))
907 Where as the encoding for integers was a series of flags, the encoding for
908 floats maps to a specific kind of float.
909 It is not a flag-based value.
910 The kinds of floats correspond to both their size, and the encoding.
911 This covers all of the basic C intrinsic floating point types.
912 The following are the different kinds of floats represented in the encoding:
913 .Bd -literal -offset indent
914 #define CTF_FP_SINGLE   1       /* IEEE 32-bit float encoding */
915 #define CTF_FP_DOUBLE   2       /* IEEE 64-bit float encoding */
916 #define CTF_FP_CPLX     3       /* Complex encoding */
917 #define CTF_FP_DCPLX    4       /* Double complex encoding */
918 #define CTF_FP_LDCPLX   5       /* Long double complex encoding */
919 #define CTF_FP_LDOUBLE  6       /* Long double encoding */
920 #define CTF_FP_INTRVL   7       /* Interval (2x32-bit) encoding */
921 #define CTF_FP_DINTRVL  8       /* Double interval (2x64-bit) encoding */
922 #define CTF_FP_LDINTRVL 9       /* Long double interval (2x128-bit) encoding */
923 #define CTF_FP_IMAGRY   10      /* Imaginary (32-bit) encoding */
924 #define CTF_FP_DIMAGRY  11      /* Long imaginary (64-bit) encoding */
925 #define CTF_FP_LDIMAGRY 12      /* Long double imaginary (128-bit) encoding */
927 .Ss Encoding of Arrays
928 Arrays, which are of type
929 .Sy CTF_K_ARRAY ,
930 have no variable length arguments.
931 They are followed by a structure which describes the number of elements in the
932 array, the type identifier of the elements in the array, and the type identifier
933 of the index of the array.
934 With arrays, the
935 .Em ctt_size
936 member is set to zero.
937 The structure that follows an array is defined as:
938 .Bd -literal
939 typedef struct ctf_array {
940         ushort_t cta_contents;  /* reference to type of array contents */
941         ushort_t cta_index;     /* reference to type of array index */
942         uint_t cta_nelems;      /* number of elements */
943 } ctf_array_t;
947 .Em cta_contents
949 .Em cta_index
950 members of the
951 .Sy ctf_array_t
952 are type identifiers which are encoded as per the section
953 .Sx Type Identifiers .
954 The member
955 .Em cta_nelems
956 is a simple four byte unsigned count of the number of elements.
957 This count may be zero when encountering C99's flexible array members.
958 .Ss Encoding of Functions
959 Function types, which are of type
960 .Sy CTF_K_FUNCTION ,
961 use the variable length list to be the number of arguments in the function.
962 When the function has a final member which is a varargs, then the argument count
963 is incremented by one to account for the variable argument.
964 Here, the
965 .Em ctt_type
966 member is encoded with the type identifier of the return type of the function.
967 Note that the
968 .Em ctt_size
969 member is not used here.
971 The variable argument list contains the type identifiers for the arguments of
972 the function, if any.
973 Each one is represented by a
974 .Sy uint16_t
975 and encoded according to the
976 .Sx Type Identifiers
977 section.
978 If the function's last argument is of type varargs, then it is also written out,
979 but the type identifier is zero.
980 This is included in the count of the function's arguments.
981 .Ss Encoding of Structures and Unions
982 Structures and Unions, which are encoded with
983 .Sy CTF_K_STRUCT
985 .Sy CTF_K_UNION
986 respectively,  are very similar constructs in C.
987 The main difference between them is the fact that every member of a structure
988 follows one another, where as in a union, all members share the same memory.
989 They are also very similar in terms of their encoding in
990 .Nm .
991 The variable length argument for structures and unions represents the number of
992 members that they have.
993 The value of the member
994 .Em ctt_size
995 is the size of the structure and union.
996 There are two different structures which are used to encode members in the
997 variable list.
998 When the size of a structure or union is greater than or equal to the large
999 member threshold, 8192, then a different structure is used to encode the member,
1000 all members are encoded using the same structure.
1001 The structure for members is as follows:
1002 .Bd -literal
1003 typedef struct ctf_member {
1004         uint_t ctm_name;        /* reference to name in string table */
1005         ushort_t ctm_type;      /* reference to type of member */
1006         ushort_t ctm_offset;    /* offset of this member in bits */
1007 } ctf_member_t;
1009 typedef struct ctf_lmember {
1010         uint_t ctlm_name;       /* reference to name in string table */
1011         ushort_t ctlm_type;     /* reference to type of member */
1012         ushort_t ctlm_pad;      /* padding */
1013         uint_t ctlm_offsethi;   /* high 32 bits of member offset in bits */
1014         uint_t ctlm_offsetlo;   /* low 32 bits of member offset in bits */
1015 } ctf_lmember_t;
1018 Both the
1019 .Em ctm_name
1021 .Em ctlm_name
1022 refer to the name of the member.
1023 The name is encoded as an offset into the string table as described by the
1024 section
1025 .Sx String Identifiers .
1026 The members
1027 .Sy ctm_type
1029 .Sy ctlm_type
1030 both refer to the type of the member.
1031 They are encoded as per the section
1032 .Sx Type Identifiers .
1034 The last piece of information that is present is the offset which describes the
1035 offset in memory that the member begins at.
1036 For unions, this value will always be zero because the start of unions in memory
1037 is always zero.
1038 For structures, this is the offset in
1039 .Sy bits
1040 that the member begins at.
1041 Note that a compiler may lay out a type with padding.
1042 This means that the difference in offset between two consecutive members may be
1043 larger than the size of the member.
1044 When the size of the overall structure is strictly less than 8192 bytes, the
1045 normal structure,
1046 .Sy ctf_member_t ,
1047 is used and the offset in bits is stored in the member
1048 .Em ctm_offset .
1049 However, when the size of the structure is greater than or equal to 8192 bytes,
1050 then the number of bits is split into two 32-bit quantities.
1051 One member,
1052 .Em ctlm_offsethi ,
1053 represents the upper 32 bits of the offset, while the other member,
1054 .Em ctlm_offsetlo ,
1055 represents the lower 32 bits of the offset.
1056 These can be joined together to get a 64-bit sized offset in bits by shifting
1057 the member
1058 .Em ctlm_offsethi
1059 to the left by thirty two and then doing a binary or of
1060 .Em ctlm_offsetlo .
1061 .Ss Encoding of Enumerations
1062 Enumerations, noted by the type
1063 .Sy CTF_K_ENUM ,
1064 are similar to structures.
1065 Enumerations use the variable list to note the number of values that the
1066 enumeration contains, which we'll term enumerators.
1067 In C, an enumeration is always equivalent to the intrinsic type
1068 .Sy int ,
1069 thus the value of the member
1070 .Em ctt_size
1071 is always the size of an integer which is determined based on the current model.
1072 For illumos systems, this will always be 4, as an integer is always defined to
1073 be 4 bytes large in both
1074 .Sy ILP32
1076 .Sy LP64 ,
1077 regardless of the architecture.
1079 The enumerators encoded in an enumeration have the following structure in the
1080 variable list:
1081 .Bd -literal
1082 typedef struct ctf_enum {
1083         uint_t cte_name;        /* reference to name in string table */
1084         int cte_value;          /* value associated with this name */
1085 } ctf_enum_t;
1088 The member
1089 .Em cte_name
1090 refers to the name of the enumerator's value, it is encoded according to the
1091 rules in the section
1092 .Sx String Identifiers .
1093 The member
1094 .Em cte_value
1095 contains the integer value of this enumerator.
1096 .Ss Encoding of Forward References
1097 Forward references, types of kind
1098 .Sy CTF_K_FORWARD ,
1099 in a
1101 file refer to types which may not have a definition at all, only a name.
1102 If the
1104 file is a child, then it may be that the forward is resolved to an
1105 actual type in the parent, otherwise the definition may be in another
1107 container or may not be known at all.
1108 The only member of the
1109 .Sy ctf_type_t
1110 that matters for a forward declaration is the
1111 .Em ctt_name
1112 which points to the name of the forward reference in the string table as
1113 described earlier.
1114 There is no other information recorded for forward references.
1115 .Ss Encoding of Pointers, Typedefs, Volatile, Const, and Restrict
1116 Pointers, typedefs, volatile, const, and restrict are all similar in
1117 .Nm .
1118 They all refer to another type.
1119 In the case of typedefs, they provide an alternate name, while volatile, const,
1120 and restrict change how the type is interpreted in the C programming language.
1121 This covers the
1123 kinds
1124 .Sy CTF_K_POINTER ,
1125 .Sy CTF_K_TYPEDEF ,
1126 .Sy CTF_K_VOLATILE ,
1127 .Sy CTF_K_RESTRICT ,
1129 .Sy CTF_K_CONST .
1131 These types have no variable list entries and use the member
1132 .Em ctt_type
1133 to refer to the base type that they modify.
1134 .Ss Encoding of Unknown Types
1135 Types with the kind
1136 .Sy CTF_K_UNKNOWN
1137 are used to indicate gaps in the type identifier space.
1138 These entries consume an identifier, but do not define anything.
1139 Nothing should refer to these gap identifiers.
1140 .Ss Dependencies Between Types
1141 C types can be imagined as a directed, cyclic, graph.
1142 Structures and unions may refer to each other in a way that creates a cyclic
1143 dependency.
1144 In cases such as these, the entire type section must be read in and processed.
1145 Consumers must not assume that every type can be laid out in dependency order;
1146 they cannot.
1147 .Ss The String Section
1148 The last section of the
1150 file is the
1151 .Sy string
1152 section.
1153 This section encodes all of the strings that appear throughout the other
1154 sections.
1155 It is laid out as a series of characters followed by a null terminator.
1156 Generally, all names are written out in ASCII, as most C compilers do not allow
1157 and characters to appear in identifiers outside of a subset of ASCII.
1158 However, any extended characters sets should be written out as a series of UTF-8
1159 bytes.
1161 The first entry in the section, at offset zero, is a single null
1162 terminator to reference the empty string.
1163 Following that, each C string should be written out, including the null
1164 terminator.
1165 Offsets that refer to something in this section should refer to the first byte
1166 which begins a string.
1167 Beyond the first byte in the section being the null terminator, the order of
1168 strings is unimportant.
1169 .Sh Data Encoding and ELF Considerations
1171 data is generally included in ELF objects which specify information to
1172 identify the architecture and endianness of the file.
1175 container inside such an object must match the endianness of the ELF object.
1176 Aside from the question of the endian encoding of data, there should be no other
1177 differences between architectures.
1178 While many of the types in this document refer to non-fixed size C integral
1179 types, they are equivalent in the models
1180 .Sy ILP32
1182 .Sy LP64 .
1183 If any other model is being used with
1185 data that has different sizes, then it must not use the model's sizes for
1186 those integral types and instead use the fixed size equivalents based on an
1187 .Sy ILP32
1188 environment.
1190 When placing a
1192 container inside of an ELF object, there are certain conventions that are
1193 expected for the purposes of tooling being able to find the
1195 data.
1196 In particular, a given ELF object should only contain a single
1198 section.
1199 Multiple containers should be merged together into a single one.
1203 file should be included in its own ELF section.
1204 The section's name must be
1205 .Ql .SUNW_ctf .
1206 The type of the section must be
1207 .Sy SHT_PROGBITS .
1208 The section should have a link set to the symbol table and its address
1209 alignment must be 4.
1210 .Sh SEE ALSO
1211 .Xr mdb 1 ,
1212 .Xr dtrace 8 ,
1213 .Xr gelf 3ELF ,
1214 .Xr libelf 3LIB ,
1215 .Xr a.out 4