Next: , Previous: , Up: The type section   [Contents][Index]


2.3.11 Structs and unions

Structures and unions are represnted as types of kind CTF_K_STRUCT and CTF_K_UNION: their representation is otherwise identical, and it is perfectly allowed for “structs” to contain overlapping fields etc, so we will treat them together for the rest of this section.

They fill out ctt_size, and use ctf_type_t in preference to ctf_stype_t if the structure size is greater than CTF_MAX_SIZE (0xfffffffe).

The vlen for structures and unions is a count of structure fields, but the type used to represent a structure field (and thus the size of the variable-length array element representing the type) depends on the size of the structure: truly huge structures, greater than CTF_LSTRUCT_THRESH bytes in length, use a different type. (CTF_LSTRUCT_THRESH is 536870912, so such structures are vanishingly rare: in v4, this representation will change somewhat for greater compactness. It’s inherited from v1, where the limits were much lower.)

Most structures can get away with using ctf_member_t:

typedef struct ctf_member_v2
{
  uint32_t ctm_name;
  uint32_t ctm_offset;
  uint32_t ctm_type;
} ctf_member_t;

Huge structures that are represented by ctf_type_t rather than ctf_stype_t have to use ctf_lmember_t, which splits the offset as ctf_type_t splits the size:

typedef struct ctf_lmember_v2
{
  uint32_t ctlm_name;
  uint32_t ctlm_offsethi;
  uint32_t ctlm_type;
  uint32_t ctlm_offsetlo;
} ctf_lmember_t;

Here’s what the fields of ctf_member mean:

OffsetNameDescription
0x00uint32_t ctm_name Strtab offset of the field name.
0x04uint32_t ctm_offset The offset of this field in bits. (Usually, for bitfields, this is machine-word-aligned and the individual field has an offset in bits, but the format allows for the offset to be encoded in bits here.)
0x08uint32_t ctm_type The type ID of the type of the field.

Here’s what the fields of the very similar ctf_lmember mean:

OffsetNameDescription
0x00uint32_t ctlm_name Strtab offset of the field name.
0x04uint32_t ctlm_offsethi The high 32 bits of the offset of this field in bits.
0x08uint32_t ctlm_type The type ID of the type of the field.
0x0cuint32_t ctlm_offsetlo The low 32 bits of the offset of this field in bits.

Macros CTF_LMEM_OFFSET, CTF_OFFSET_TO_LMEMHI and CTF_OFFSET_TO_LMEMLO serve to extract and install the values of the ctlm_offset fields, much as with the split size fields in ctf_type_t.

Unnamed structure and union fields are simply implemented by collapsing the unnamed field’s members into the containing structure or union: this does mean that a structure containing an unnamed union can end up being a “structure” with multiple members at the same offset. (A future format revision may collapse CTF_K_STRUCT and CTF_K_UNION into the same kind and decide among them based on whether their members do in fact overlap.)

Structure and union type names, as usual in C, go into their own namespace, just as enum type names do.

Forward declarations of structures and unions are not implemented with this kind: see section Forward declarations.


Next: , Previous: , Up: The type section   [Contents][Index]