btf: generate and output DECL_TAG and TYPE_TAG records

Support the btf_decl_tag and btf_type_tag attributes in BTF by creating
and emitting BTF_KIND_DECL_TAG and BTF_KIND_TYPE_TAG records,
respectively, for them.

Some care is required when -gprune-btf is in effect to avoid emitting
decl or type tags for declarations or types which have been pruned and
will not be emitted in BTF.

gcc/
	* btfout.cc (get_btf_kind): Handle DECL_TAG and TYPE_TAG kinds.
	(btf_calc_num_vbytes): Likewise.
	(btf_asm_type): Likewise.
	(output_asm_btf_vlen_bytes): Likewise.
	(output_btf_tags): New.
	(btf_output): Call it here.
	(btf_add_used_type): Replace with simple wrapper around...
	(btf_add_used_type_1): ...the implementation.  Handle
	BTF_KIND_DECL_TAG and BTF_KIND_TYPE_TAG.
	(btf_add_vars): Update btf_add_used_type call.
	(btf_assign_tag_ids): New.
	(btf_mark_type_used): Update btf_add_used_type call.
	(btf_collect_pruned_types): Likewise.  Handle type and decl tags.
	(btf_finish): Call btf_assign_tag_ids.

gcc/testsuite/
	* gcc.dg/debug/btf/btf-decl-tag-1.c: New test.
	* gcc.dg/debug/btf/btf-decl-tag-2.c: New test.
	* gcc.dg/debug/btf/btf-decl-tag-3.c: New test.
	* gcc.dg/debug/btf/btf-decl-tag-4.c: New test.
	* gcc.dg/debug/btf/btf-type-tag-1.c: New test.
	* gcc.dg/debug/btf/btf-type-tag-2.c: New test.
	* gcc.dg/debug/btf/btf-type-tag-3.c: New test.
	* gcc.dg/debug/btf/btf-type-tag-4.c: New test.
	* gcc.dg/debug/btf/btf-type-tag-c2x-1.c: New test.

include/
	* btf.h (BTF_KIND_DECL_TAG, BTF_KIND_TYPE_TAG) New defines.
	(struct btf_decl_tag): New.
This commit is contained in:
David Faust
2025-02-05 10:41:23 -08:00
parent 9c862a593b
commit 43dcea48b8
11 changed files with 366 additions and 24 deletions

View File

@@ -114,6 +114,8 @@ struct btf_type
#define BTF_KIND_VAR 14 /* Variable. */
#define BTF_KIND_DATASEC 15 /* Section such as .bss or .data. */
#define BTF_KIND_FLOAT 16 /* Floating point. */
#define BTF_KIND_DECL_TAG 17 /* Declaration tag. */
#define BTF_KIND_TYPE_TAG 18 /* Type tag. */
#define BTF_KIND_ENUM64 19 /* Enumeration up to 64 bits. */
#define BTF_KIND_MAX BTF_KIND_ENUM64
#define NR_BTF_KINDS (BTF_KIND_MAX + 1)
@@ -227,6 +229,18 @@ struct btf_enum64
uint32_t val_hi32; /* high 32-bit value for a 64-bit value Enumerator */
};
/* BTF_KIND_DECL_TAG is followed by a single struct btf_decl_tag, which
describes the item to which the tag applies:
- If component_idx == (uint32_t) -1, then the tag applies to item referred
to by the type_id.
- Otherwise, the tag applies to the struct or union member, or function
argument of the type referred to by type_id with the 0-based index
given by component_idx. */
struct btf_decl_tag
{
uint32_t component_idx;
};
#ifdef __cplusplus
}
#endif