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

@@ -385,9 +385,12 @@ struct ctf_dict
ctf_dynhash_t *ctf_structs; /* Hash table of struct types. */
ctf_dynhash_t *ctf_unions; /* Hash table of union types. */
ctf_dynhash_t *ctf_enums; /* Hash table of enum types. */
ctf_dynhash_t *ctf_datasecs; /* Hash table of datasecs. */
ctf_dynhash_t *ctf_tags; /* Hash table of dynsets of type/decl tags. */
ctf_dynhash_t *ctf_names; /* Hash table of remaining types, plus
enumeration constants. */
ctf_lookup_t ctf_lookups[5]; /* Pointers to nametabs for name lookup. */
ctf_lookup_t ctf_lookups[6]; /* Pointers to nametabs for name lookup. */
ctf_dynhash_t *ctf_var_datasecs; /* Variable -> datasec mappings. */
ctf_strs_t ctf_str[2]; /* Array of string table base and bounds. */
ctf_strs_writable_t *ctf_dynstrtab; /* Dynamically allocated string table, if any. */
ctf_dynhash_t *ctf_str_atoms; /* Hash table of ctf_str_atoms_t. */
@@ -412,6 +415,8 @@ struct ctf_dict
uint32_t *ctf_pptrtab; /* Parent types pointed to by child dicts. */
size_t ctf_pptrtab_len; /* Num types storable in pptrtab currently. */
uint32_t ctf_pptrtab_typemax; /* Max child type when pptrtab last updated. */
ctf_type_t *ctf_void_type; /* void type, if dynamically constructed. (More
space allocated, due to vlen.) */
ctf_dynset_t *ctf_conflicting_enums; /* Tracks enum constants that conflict. */
uint32_t *ctf_funcidx_names; /* Name of each function symbol in symtypetab
(if indexed). */
@@ -707,6 +712,7 @@ extern int ctf_dynset_insert (ctf_dynset_t *, void *);
extern void ctf_dynset_remove (ctf_dynset_t *, const void *);
extern size_t ctf_dynset_elements (const ctf_dynset_t *);
extern void ctf_dynset_destroy (ctf_dynset_t *);
extern void ctf_dynset_destroy_arg (ctf_dynset_t *, void *unused);
extern void *ctf_dynset_lookup (ctf_dynset_t *, const void *);
extern int ctf_dynset_exists (ctf_dynset_t *, const void *key,
const void **orig_key);