include, libctf: start work on libctf v4

This format is a superset of BTF, but for now we just do the minimum to
declare a new file format version, without actually introducing any format
changes.

From now on, we refuse to reserialize CTFv1 dicts: these have a distinct
parent/child boundary which obviously cannot change upon reserialization
(that would change the type IDs): instead, we encoded this by stuffing in
a unique CTF version for such dicts.  We can't do that now we have one
version for all CTFv4 dicts, and testing such old dicts is very hard these
days anyway, and is not automated: so just drop support for writing them out
entirely. (You still *can* write them out, but you have to do a full-blown
ctf_link, which generates an all-new fresh dict and recomputes type IDs as
part of deduplication.)

To prevent this extremely-not-ready format escaping into the wild, add a
new mechanism whereby any format version higher than the new #define
CTF_STABLE_VERSION cannot be serialized unless I_KNOW_LIBCTF_IS_UNSTABLE is
set in the environment.

include/
	* ctf-api.h (_CTF_ERRORS) [ECTF_CTFVERS_NO_SERIALIZE]: New.
        [ECTF_UNSTABLE]: New.
         (ECTF_NERR): Update.
	* ctf.h: Small comment improvements..
        (ctf_header_v3): New, copy of ctf_header.
	(CTF_VERSION_4): New.
	(CTF_VERSION): Now CTF_VERSION_4.
	(CTF_STABLE_VERSION): Still 4, CTF_VERSION_3.

ld/
	* testsuite/ld-ctf/*.d: Update to CTF_VERSION_4.

libctf/
	* ctf-impl.h (LCTF_NO_SERIALIZE): New.
	* ctf-dump.c (ctf_dump_header): Add CTF_VERSION_4.
	* ctf-open.c (ctf_dictops): Likewise.
        (upgrade_header): Rename to...
	(upgrade_header_v2): ... this.
	(upgrade_header_v3): New.
	(upgrade_types): Support upgrading from CTF_VERSION_3.
        Turn on LCTF_NO_SERIALIZE for CTFv1.
	(init_static_types_internal): Upgrade all types tables older than
	* CTF_VERSION_4.
	(ctf_bufopen): Support CTF_VERSION_4: error out if we forget to
	update this switch in future.  Add header upgrading from v3 and
	below.  Improve comments slightly.
	* ctf-serialize.c (ctf_serialize): Block serialization of unstable
	file formats, and of file formats for which LCTF_NO_SERIALIZE is
	turned on (v1).
This commit is contained in:
Nick Alcock
2024-07-15 20:21:36 +01:00
parent 8cdf110a11
commit 9a74ab12c8
43 changed files with 168 additions and 65 deletions

View File

@@ -961,9 +961,28 @@ ctf_serialize (ctf_dict_t *fp, size_t *bufsiz)
emit_symtypetab_state_t symstate;
memset (&symstate, 0, sizeof (emit_symtypetab_state_t));
/* Fill in an initial CTF header. We will leave the label, object,
and function sections empty and only output a header, type section,
and string table. The type section begins at a 4-byte aligned
/* Stop unstable file formats (subject to change) getting out into the
wild. */
#if CTF_VERSION != CTF_STABLE_VERSION
if (!getenv ("I_KNOW_LIBCTF_IS_UNSTABLE"))
{
ctf_set_errno (fp, ECTF_UNSTABLE);
return NULL;
}
#endif
/* Prohibit reserialization of dicts for which we have dynamic state inherited
from the upgrade process which we cannot record in the dict. Right now,
this applies only to CTFv1 dicts, which have a different parent/child type
offset to v2 and higher, and nowhere to record this in CTFv4. */
if (fp->ctf_flags & LCTF_NO_SERIALIZE)
{
ctf_set_errno (fp, ECTF_CTFVERS_NO_SERIALIZE);
return NULL;
}
/* Fill in an initial CTF header. The type section begins at a 4-byte aligned
boundary past the CTF header itself (at relative offset zero). The flag
indicating a new-style function info section (an array of CTF_K_FUNCTION
type IDs in the types section) is flipped on. */
@@ -974,6 +993,8 @@ ctf_serialize (ctf_dict_t *fp, size_t *bufsiz)
/* This is a new-format func info section, and the symtab and strtab come out
of the dynsym and dynstr these days. */
/* UPTODO: remove. */
hdr.cth_flags = (CTF_F_NEWFUNCINFO | CTF_F_DYNSTR);
/* Propagate all symbols in the symtypetabs into the dynamic state, so that