A btf_type_tag on a function that returns a pointer never reaches the
returned pointer:
int __attribute__((btf_type_tag ("user"))) * f (void);
GCC 16.1 rejects it with "btf_type_tag attribute does not apply to
functions" and drops it. The previous patch removes that warning, so the
tag is accepted and still dropped. Either way BTF holds
f: FUNC_PROTO -> ptr -> int
where clang records
f: FUNC_PROTO -> ptr -> type_tag("user") -> int
A pointer always has somewhere to carry a tag: its own DIE, one per
pointee. A pointer return type is no different from any other pointer,
so the tag belongs on it rather than on the function.
So when the attribute lands on a FUNCTION_TYPE whose return type is a
pointer, tag that return type. The function type is rebuilt around the
tagged variant. The BTF backend needs no change, as the existing
pointer handling emits the tagged return type.
A non-pointer return type has nowhere to carry the tag, so it is dropped
without a warning, as clang does. METHOD_TYPE keeps its rejection.
The Linux kernel returns tagged pointers from many functions, as in
arch/arm64/include/asm/uaccess.h:
void __user *__uaccess_mask_ptr(const void __user *ptr);
so the tag is missing from those return types in the kernel's BTF, where
a clang build has it.
Bootstrapped and regression tested on x86_64-linux-gnu and aarch64-linux-gnu.
PR debug/125991
gcc/c-family/ChangeLog:
* c-attribs.cc (handle_btf_type_tag_attribute): When the attribute
applies to a FUNCTION_TYPE whose return type is a pointer, apply it
to that pointer return type by rebuilding the function type.
gcc/ChangeLog:
* doc/extend.texi (btf_type_tag): Document that on a function the
attribute tags a pointer return type.
gcc/testsuite/ChangeLog:
* gcc.dg/debug/btf/btf-type-tag-9.c: New test.
* gcc.dg/debug/btf/btf-type-tag-10.c: New test.
* gcc.dg/debug/btf/btf-type-tag-11.c: New test.
* gcc.dg/debug/btf/btf-type-tag-12.c: New test.
* gcc.dg/debug/btf/btf-type-tag-13.c: New test.
* gcc.dg/debug/btf/btf-type-tag-14.c: New test.
* gcc.dg/debug/btf/btf-type-tag-c2x-2.c: New test.
Signed-off-by: Vineet Gupta <[email protected]>
---
gcc/c-family/c-attribs.cc | 27 ++++++++++++++--
gcc/doc/extend.texi | 4 +++
.../gcc.dg/debug/btf/btf-type-tag-10.c | 14 ++++++++
.../gcc.dg/debug/btf/btf-type-tag-11.c | 16 ++++++++++
.../gcc.dg/debug/btf/btf-type-tag-12.c | 19 +++++++++++
.../gcc.dg/debug/btf/btf-type-tag-13.c | 12 +++++++
.../gcc.dg/debug/btf/btf-type-tag-14.c | 32 +++++++++++++++++++
.../gcc.dg/debug/btf/btf-type-tag-9.c | 16 ++++++++++
.../gcc.dg/debug/btf/btf-type-tag-c2x-2.c | 26 +++++++++++++++
9 files changed, 163 insertions(+), 3 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-10.c
create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-11.c
create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-12.c
create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-13.c
create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-14.c
create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-9.c
create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-c2x-2.c
diff --git a/gcc/c-family/c-attribs.cc b/gcc/c-family/c-attribs.cc
index f6815c506f1d..5a54411c2c67 100644
--- a/gcc/c-family/c-attribs.cc
+++ b/gcc/c-family/c-attribs.cc
@@ -4654,9 +4654,30 @@ handle_btf_type_tag_attribute (tree *node, tree name,
tree args,
return NULL_TREE;
}
- /* 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. */
+ /* On a function whose return type is a pointer, the tag applies to that
+ pointer: rebuild the function type with a tagged variant of the return
+ type. This is the PR125991 shape, and matches clang. */
+ if (TREE_CODE (*node) == FUNCTION_TYPE
+ && TREE_CODE (TREE_TYPE (*node)) == POINTER_TYPE)
+ {
+ tree fntype = *node;
+ tree ret = TREE_TYPE (fntype);
+ tree newret
+ = build_type_attribute_variant (ret,
+ tree_cons (name, args,
+ TYPE_ATTRIBUTES (ret)));
+ tree newfn = build_function_type (newret, TYPE_ARG_TYPES (fntype));
+ /* Preserve any attributes already on the function type itself. */
+ if (TYPE_ATTRIBUTES (fntype))
+ newfn = build_type_attribute_variant (newfn, TYPE_ATTRIBUTES (fntype));
+ *node = newfn;
+ *no_add_attrs = true;
+ return NULL_TREE;
+ }
+
+ /* Otherwise 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,
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 4c740e83fea7..d5df15891d9a 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -2331,6 +2331,10 @@ information is generated, the attribute has no effect.
The argument is treated as a null-terminated sequence of zero or more
non-null bytes. Wide character strings are not supported.
+When applied to a function whose return type is a pointer, the attribute
+tags that pointer return type. On a function with a non-pointer return
+type the attribute has no effect.
+
The attribute may be supplied multiple times for a single type, in
which case each distinct argument string will be recorded in a
separate DIE or BTF record, each associated to the type. For a single
diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-10.c
b/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-10.c
new file mode 100644
index 000000000000..b3a65c6d21f3
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-10.c
@@ -0,0 +1,14 @@
+/* Test btf_type_tag on a function's pointer return type with the
+ __attribute__ placed immediately after the '*' (i.e. on the pointer
+ itself). The tag applies to the pointer return type. PR/125991.
+
+ f: FUNC_PROTO -> ptr -> type_tag("A") -> int */
+
+/* { dg-do compile } */
+/* { dg-options "-O0 -gbtf -dA" } */
+
+int * __attribute__((btf_type_tag ("A"))) f (int x) { return 0; }
+
+/* { dg-final { scan-assembler-times " BTF_KIND_FUNC_PROTO
''(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_PTR ''\\)" 1 } } */
+/* { dg-final { scan-assembler-times " BTF_KIND_PTR
''(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_TYPE_TAG 'A'\\)" 1 } } */
+/* { dg-final { scan-assembler-times " BTF_KIND_TYPE_TAG
'A'(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_INT 'int'\\)" 1 } } */
diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-11.c
b/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-11.c
new file mode 100644
index 000000000000..3f29abe61cbb
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-11.c
@@ -0,0 +1,16 @@
+/* Test btf_type_tag on a function return type, attribute in the prefix
+ position (before all declaration specifiers). PR/125991.
+
+ __attribute__((btf_type_tag("B"))) int *b (int y);
+
+ The tag applies to the (pointer) return type:
+ b: FUNC_PROTO -> ptr -> type_tag("B") -> int */
+
+/* { dg-do compile } */
+/* { dg-options "-O0 -gbtf -dA" } */
+
+__attribute__((btf_type_tag ("B"))) int *b (int y) { return 0; }
+
+/* { dg-final { scan-assembler-times " BTF_KIND_FUNC_PROTO
''(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_PTR ''\\)" 1 } } */
+/* { dg-final { scan-assembler-times " BTF_KIND_PTR
''(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_TYPE_TAG 'B'\\)" 1 } } */
+/* { dg-final { scan-assembler-times " BTF_KIND_TYPE_TAG
'B'(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_INT 'int'\\)" 1 } } */
diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-12.c
b/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-12.c
new file mode 100644
index 000000000000..cfe098f3f0b2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-12.c
@@ -0,0 +1,19 @@
+/* Test btf_type_tag on a function return type, attribute in the postfix
+ position (after the declarator). PR/125991.
+
+ int *c (int z) __attribute__((btf_type_tag("C")));
+
+ A postfix attribute is not permitted on a function definition, so 'c' is
+ declared and referenced via a caller to force BTF emission. The tag applies
+ to the (pointer) return type:
+ c: FUNC_PROTO -> ptr -> type_tag("C") -> int */
+
+/* { dg-do compile } */
+/* { dg-options "-O0 -gbtf -dA" } */
+
+int *c (int z) __attribute__((btf_type_tag ("C")));
+
+int *caller (int z) { return c (z); }
+
+/* { dg-final { scan-assembler-times " BTF_KIND_PTR
''(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_TYPE_TAG 'C'\\)" 1 } } */
+/* { dg-final { scan-assembler-times " BTF_KIND_TYPE_TAG
'C'(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_INT 'int'\\)" 1 } } */
diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-13.c
b/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-13.c
new file mode 100644
index 000000000000..3c0b90bc67f2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-13.c
@@ -0,0 +1,12 @@
+/* Test that btf_type_tag on a non-pointer function return type is dropped:
+ it is only meaningful on a pointer type, so no BTF_KIND_TYPE_TAG record is
+ emitted. No warning is issued, as with clang. PR/125991. */
+
+/* { dg-do compile } */
+/* { dg-options "-O0 -gbtf -dA" } */
+
+int __attribute__((btf_type_tag ("dropped"))) f (int x) { return x; }
+
+/* The function is emitted, but with no type tag on its return type. */
+/* { dg-final { scan-assembler "BTF_KIND_FUNC 'f'" } } */
+/* { dg-final { scan-assembler-not "BTF_KIND_TYPE_TAG" } } */
diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-14.c
b/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-14.c
new file mode 100644
index 000000000000..14f1f9b37d83
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-14.c
@@ -0,0 +1,32 @@
+/* Test generation of BTF type tags on a function's pointer return type.
+ PR/125991.
+
+ btf_type_tag is only meaningful on a pointer type, so the directly-tagged
+ return must be a pointer; here it carries two tags. Each tagged type
+ results in a BTF_KIND_TYPE_TAG record inserted into the return chain of the
+ BTF_KIND_FUNC_PROTO:
+
+ func: FUNC_PROTO -> ret ptr -> type_tag("type2") -> type_tag("type1") ->
int
+
+ The arguments are tagged via typedefs, which a tag does not yet reach in
+ BTF; they are here to show the return chain is built independently of
+ them. */
+
+/* { dg-do compile } */
+/* { dg-options "-O0 -gbtf -dA" } */
+
+#define __tag1 __attribute__((btf_type_tag ("type1")))
+#define __tag2 __attribute__((btf_type_tag ("type2")))
+
+typedef int __tag1 TYP1;
+typedef int __tag2 TYP2;
+
+int __tag1 __tag2 * func (TYP1 arg_a, TYP2 arg_b)
+{
+ return 0;
+}
+
+/* Return is a pointer carrying type2 -> type1. */
+/* { dg-final { scan-assembler-times " BTF_KIND_FUNC_PROTO
''(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_PTR ''\\)" 1 } } */
+/* { dg-final { scan-assembler-times " BTF_KIND_PTR
''(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_TYPE_TAG 'type2'\\)" 1 } } */
+/* { dg-final { scan-assembler-times " BTF_KIND_TYPE_TAG
'type2'(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_TYPE_TAG 'type1'\\)" 1 } } */
diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-9.c
b/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-9.c
new file mode 100644
index 000000000000..80f5e26c5315
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-9.c
@@ -0,0 +1,16 @@
+/* Test btf_type_tag on a function pointer return type, attribute in the
+ return-type declaration-specifier position (before the '*'). PR/125991.
+
+ int __attribute__((btf_type_tag("A"))) * a (int x);
+
+ The tag applies to the pointer return type:
+ a: FUNC_PROTO -> ptr -> type_tag("A") -> int */
+
+/* { dg-do compile } */
+/* { dg-options "-O0 -gbtf -dA" } */
+
+int __attribute__((btf_type_tag ("A"))) * a (int x) { return 0; }
+
+/* { dg-final { scan-assembler-times " BTF_KIND_FUNC_PROTO
''(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_PTR ''\\)" 1 } } */
+/* { dg-final { scan-assembler-times " BTF_KIND_PTR
''(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_TYPE_TAG 'A'\\)" 1 } } */
+/* { dg-final { scan-assembler-times " BTF_KIND_TYPE_TAG
'A'(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_INT 'int'\\)" 1 } } */
diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-c2x-2.c
b/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-c2x-2.c
new file mode 100644
index 000000000000..c7a7c960b63a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/btf/btf-type-tag-c2x-2.c
@@ -0,0 +1,26 @@
+/* Test btf_type_tag on a function's pointer return type using C23 standard
+ attribute syntax. C23 attributes do not "slide" (unlike __attribute__), so
+ the tag applies to the pointer return type only when it appears after the
+ '*'. Before the '*' it applies to the pointee, which is a non-pointer and
+ is dropped. PR/125991. */
+
+/* { dg-do compile } */
+/* { dg-options "-O0 -gbtf -dA -std=c23" } */
+
+#define __tag_after [[gnu::btf_type_tag ("after")]]
+#define __tag_before [[gnu::btf_type_tag ("before")]]
+
+/* After '*': tags the pointer return type.
+ f: FUNC_PROTO -> ptr -> type_tag("after") -> int */
+int * __tag_after f (int x) { return 0; }
+
+/* Before '*': tags the pointee, as the GNU spelling "int __tag *p" does.
+ That is the shape the kernel writes as "void __user *p", so it is
+ recorded:
+ g: FUNC_PROTO -> ptr -> type_tag("before") -> int */
+int __tag_before * g (int x) { return 0; }
+
+/* { dg-final { scan-assembler-times " BTF_KIND_PTR
''(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_TYPE_TAG 'after'\\)" 1 } } */
+/* { dg-final { scan-assembler-times " BTF_KIND_TYPE_TAG
'after'(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_INT 'int'\\)" 1 } } */
+/* { dg-final { scan-assembler-times " BTF_KIND_PTR
''(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_TYPE_TAG 'before'\\)" 1 } } */
+/* { dg-final { scan-assembler-times " BTF_KIND_TYPE_TAG
'before'(\[\\r\\n\]+\[^\\r\\n\]*){2}\\(BTF_KIND_INT 'int'\\)" 1 } } */
--
2.53.0-Meta