A btf_type_tag that reaches a pointer through the function it points to is
rejected and lost. The Linux kernel spells an RCU-protected function
pointer that way, in include/linux/rethook.h:
void (__rcu *handler)(struct rethook_node *, unsigned long);
warning: 'btf_type_tag' attribute does not apply to functions
No annotation reaches the debug info, so BTF holds PTR -> FUNC_PROTO.
clang accepts the same declaration and records PTR -> TYPE_TAG 'rcu' ->
FUNC_PROTO.
Two things are in the way. The attribute handler rejects a FUNCTION_TYPE
outright. Here the function is a pointee, and the tag belongs on it as on
any other pointed-to type. And modified_type_die records only
the tags passed in type_attrs, which for a pointer are the pointer's own
attributes. A tag carried by the pointed-to type is never looked at.
So accept the attribute on a FUNCTION_TYPE, and record a btf_type_tag
found on the pointee when the pointer DIE is emitted. A tag written in
the pointer's own declarator still arrives through type_attrs, and is not
recorded twice. METHOD_TYPE keeps the old rejection, with the diagnostic
reworded to match.
The same gap covers a pointer formed from an object that is already
tagged, __typeof__ (&obj). GCC records that tag on the pointed-to type,
where it does not reach BTF, so the tag is lost there. clang records it
on the pointer, and so does this patch.
GCC 16.1 behaves the same.
Bootstrapped and regression tested on x86_64-linux-gnu and aarch64-linux-gnu.
PR debug/128479
gcc/c-family/ChangeLog:
* c-attribs.cc (handle_btf_type_tag_attribute): Accept the attribute
on a FUNCTION_TYPE so that a pointer to a tagged function keeps the
tag. Keep rejecting METHOD_TYPE.
gcc/ChangeLog:
* dwarf2out.cc (modified_type_die): When emitting a pointer DIE,
record a btf_type_tag carried by the pointed-to type.
gcc/testsuite/ChangeLog:
* gcc.dg/attr-btf-type-tag-3.c: Expect no warning for a tag on a
function, as with clang.
* gcc.dg/debug/dwarf2/btf-type-tag-fnptr-1.c: New test.
Signed-off-by: Vineet Gupta <[email protected]>
---
gcc/c-family/c-attribs.cc | 7 ++++--
gcc/dwarf2out.cc | 13 +++++++++++
gcc/testsuite/gcc.dg/attr-btf-type-tag-3.c | 9 ++++----
.../debug/dwarf2/btf-type-tag-fnptr-1.c | 23 +++++++++++++++++++
4 files changed, 46 insertions(+), 6 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/debug/dwarf2/btf-type-tag-fnptr-1.c
diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index aab9f2cb9e3b..f6815c506f1d 100644
--- a/gcc/c-family/c-attribs.cc
+++ b/gcc/c-family/c-attribs.cc
@@ -4654,10 +4654,13 @@ handle_btf_type_tag_attribute (tree *node, tree name,
tree args,
return NULL_TREE;
}
- if (TREE_CODE (*node) == FUNCTION_TYPE || TREE_CODE (*node) == METHOD_TYPE)
+ /* A btf_type_tag on a function type is how a pointer to a tagged function
+ is spelled, as in "void (__tag *fp) (int)". Let it attach like any other
+ type attribute; the pointer picks it up from the pointee. */
+ if (TREE_CODE (*node) == METHOD_TYPE)
{
warning (OPT_Wattributes,
- "%qE attribute does not apply to functions", name);
+ "%qE attribute does not apply to methods", name);
*no_add_attrs = true;
return NULL_TREE;
}
diff --git a/gcc/dwarf2out.cc b/gcc/dwarf2out.cc
index e8a6019f72e0..85977e85c89f 100644
--- a/gcc/dwarf2out.cc
+++ b/gcc/dwarf2out.cc
@@ -14185,6 +14185,19 @@ modified_type_die (tree type, int cv_quals, tree
type_attrs, bool reverse,
add_alignment_attribute (mod_type_die, type);
item_type = TREE_TYPE (type);
+ /* A btf_type_tag written in the declaration specifiers lands on the
+ pointer type and is picked up above from TYPE_ATTRIBUTES. One that is
+ already part of the pointed-to type is not, as in a pointer to a tagged
+ function, "void (__tag *fp) (int)", or a pointer formed from an object
+ that is already tagged. Record those here, on the pointer. */
+ if (!lookup_attribute ("btf_type_tag", type_attrs))
+ {
+ tree item_tags = lookup_attribute ("btf_type_tag",
+ TYPE_ATTRIBUTES (item_type));
+ if (item_tags)
+ gen_btf_tag_dies (item_tags, mod_type_die);
+ }
+
addr_space_t as = TYPE_ADDR_SPACE (item_type);
if (!ADDR_SPACE_GENERIC_P (as))
{
diff --git a/gcc/testsuite/gcc.dg/attr-btf-type-tag-3.c
b/gcc/testsuite/gcc.dg/attr-btf-type-tag-3.c
index afb14b12a22f..8700a181f44c 100644
--- a/gcc/testsuite/gcc.dg/attr-btf-type-tag-3.c
+++ b/gcc/testsuite/gcc.dg/attr-btf-type-tag-3.c
@@ -1,8 +1,9 @@
-/* Test btf_type_tag attribute warnings. */
+/* Test btf_type_tag attribute on functions. It is accepted without a
+ warning, as with clang. */
/* { dg-do compile } */
-int __attribute__((btf_type_tag ("A"))) a (int x); /* { dg-warning "does not
apply to functions" } */
+int __attribute__((btf_type_tag ("A"))) a (int x);
-__attribute__((btf_type_tag ("B"))) int *b (int y); /* { dg-warning "does not
apply to functions" } */
+__attribute__((btf_type_tag ("B"))) int *b (int y);
-int *c (int z) __attribute__((btf_type_tag ("C"))); /* { dg-warning "does not
apply to functions" } */
+int *c (int z) __attribute__((btf_type_tag ("C")));
diff --git a/gcc/testsuite/gcc.dg/debug/dwarf2/btf-type-tag-fnptr-1.c
b/gcc/testsuite/gcc.dg/debug/dwarf2/btf-type-tag-fnptr-1.c
new file mode 100644
index 000000000000..11fab508c00b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/dwarf2/btf-type-tag-fnptr-1.c
@@ -0,0 +1,23 @@
+/* A btf_type_tag on the function a pointer points to must be recorded, as
+ in "void (__tag *fp) (int)". There the tag reaches the pointer through
+ the pointee type rather than through the pointer's own declarator. */
+/* { dg-do compile } */
+/* { dg-options "-gdwarf -dA" } */
+
+#define __tag1 __attribute__((btf_type_tag ("tag1")))
+#define __tag2 __attribute__((btf_type_tag ("tag2")))
+
+struct S { int v; };
+
+/* Tag on the pointed-to function. */
+struct F { void (__tag1 *fp) (int); };
+struct F f;
+
+/* Tag written in the pointer's own declarator; this already worked. */
+struct S __tag2 *direct;
+
+/* Both tags are recorded, each on its pointer. */
+/* { dg-final { scan-assembler-times "DIE \\(\[^\n\]*\\)
DW_TAG_GNU_annotation" 2 } } */
+/* { dg-final { scan-assembler-times " DW_AT_GNU_annotation" 2 } } */
+/* { dg-final { scan-assembler-times "DW_AT_const_value: \"tag1\"" 1 } } */
+/* { dg-final { scan-assembler-times "DW_AT_const_value: \"tag2\"" 1 } } */
--
2.53.0-Meta