libctf: CTFv4: type opening

The majority of this commit rejigs the core type table opening
code for CTFv4: there are a few ancillary bits it drags in,
indicated below.

The internal definition of a child dict (that may not have type or string
lookups performed in it until ctf_open time) used to be 'has a
cth_parent_name', but since BTF doesn't have one of those at all, we add
an additional check: a dict the first byte of whose strtab is not 0 must
be a child.  (If *either* is true, this is a child dict, which allows for
the possibility of CTF dicts with non-deduplicated strtabs -- thus with
leading \0's -- to exist in future.)

The initial sweep through the type table in init_static_types (to size
the name-table lookup hashes) also now checks for various types which
indicate that this must be a CTF dict, in addition to being adjusted
to cater for new CTFv4 representations of things like forwards.  (At
this early stage, we cannot rely on the functions in ctf-type.c to
abstract over this for us.)

We make some new hashtables for new namespace-like things: datasecs
and type and decl tags.

The main name-population loop in init_static_types_names_internal
takes prefixes into account, looking for the name on the suffix type
(where the name is always found).  LSTRUCT handling is removed (they
no longer exist); ENUM64s, enum forwards, VARs, datasecs, and type
and decl tags get their names suitably populated.  Some buggy code
which tried to populate the name tables for cvr-quals (which are
nameless) was dropped.

We add an extra pass which traverses all datasecs and keeps track of which
datasec each var is instantiated in (if any) in a new ctf_var_datasecs hash
table.  (This uses a number of type-querying functions which don't yet
exist: they'll be added in the upcoming commits.)

We handle the type 0 == void case by pointing the first element of
ctf_txlate at a type read in named "void" (making type 0 an alias to it),
or, if one doesn't exist, creating a new one (outside the type table and dtd
arrays), and pointing type 0 at that.  Since it is numbered 0 and not in the
type table or dtd arrays, it will never be written out at serialization
time, but since it is *present*, libctf consumers who expect the void type
to have an integral definition rather than being a magic number will get
what they expect.
This commit is contained in:
Nick Alcock
2025-04-24 14:58:50 +01:00
parent f7d05ab342
commit ad13b7d44f
5 changed files with 373 additions and 115 deletions

View File

@@ -106,14 +106,21 @@ ctf_grow_vlen (ctf_dict_t *fp, ctf_dtdef_t *dtd, size_t vlen)
ctf_dict_t *
ctf_create (int *errp)
{
static const ctf_header_t hdr = { .cth_preamble = { CTF_MAGIC, CTF_VERSION, 0 } };
static ctf_header_t hdr =
{
.btf.bth_preamble = { CTF_BTF_MAGIC, CTF_BTF_VERSION, 0 },
.btf.bth_hdr_len = sizeof (ctf_btf_header_t),
};
ctf_dynhash_t *structs = NULL, *unions = NULL, *enums = NULL, *names = NULL;
ctf_dynhash_t *datasecs = NULL, *tags = NULL;
ctf_sect_t cts;
ctf_dict_t *fp;
libctf_init_debug();
hdr.cth_preamble.ctp_magic_version = (CTFv4_MAGIC << 16) | CTF_VERSION;
structs = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
NULL, NULL);
unions = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
@@ -122,7 +129,11 @@ ctf_create (int *errp)
NULL, NULL);
names = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
NULL, NULL);
if (!structs || !unions || !enums || !names)
datasecs = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
NULL, NULL);
tags = ctf_dynhash_create (ctf_hash_string, ctf_hash_eq_string,
NULL, (ctf_hash_free_fun) ctf_dynset_destroy);
if (!structs || !unions || !enums || !names || !datasecs || !tags)
{
ctf_set_open_errno (errp, EAGAIN);
goto err;
@@ -142,10 +153,14 @@ ctf_create (int *errp)
ctf_dynhash_destroy (fp->ctf_unions);
ctf_dynhash_destroy (fp->ctf_enums);
ctf_dynhash_destroy (fp->ctf_names);
ctf_dynhash_destroy (fp->ctf_datasecs);
ctf_dynhash_destroy (fp->ctf_tags);
fp->ctf_structs = structs;
fp->ctf_unions = unions;
fp->ctf_enums = enums;
fp->ctf_names = names;
fp->ctf_datasecs = datasecs;
fp->ctf_tags = tags;
fp->ctf_dtoldid = 0;
fp->ctf_snapshot_lu = 0;
@@ -166,6 +181,8 @@ ctf_create (int *errp)
ctf_dynhash_destroy (unions);
ctf_dynhash_destroy (enums);
ctf_dynhash_destroy (names);
ctf_dynhash_destroy (datasecs);
ctf_dynhash_destroy (tags);
return NULL;
}