dwarf: do not clone a typedef for a tagged use of it [PR128441]
Checks
| Context |
Check |
Description |
| linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 |
success
|
Build passed
|
| linaro-tcwg-bot/tcwg_simplebootstrap_build--master-aarch64-bootstrap |
success
|
Build passed
|
| linaro-tcwg-bot/tcwg_gcc_build--master-arm |
success
|
Build passed
|
| linaro-tcwg-bot/tcwg_simplebootstrap_build--master-arm-bootstrap |
success
|
Build passed
|
Commit Message
A btf_type_tag on a use of a named typedef makes modified_type_die clone
the underlying typedef's DIE, so that the annotation can hang off the
clone without disturbing other users of the typedef. The clone keeps
DW_AT_name, so two DW_TAG_typedef DIEs end up sharing one name:
typedef unsigned int uint_t;
uint_t __attribute__((btf_type_tag("tag1"))) uint_A;
uint_t __attribute__((btf_type_tag("tag2"))) uint_B;
uint_t plain;
emits three DW_TAG_typedef DIEs named uint_t, and with -gbtf three
BTF_KIND_TYPEDEF records named uint_t.
This is not new. Before r17-1810-g3e2885588fe0 (PR debug/125421) every
variant of a typedef was cloned, tagged or not. That commit made an
untagged variant reuse the typedef's DIE instead, but deliberately kept
the clone for a tagged one, since the clone was the only place the tag
could be attached. Trunk and releases/gcc-16 therefore still clone for a
tagged use, and 16.1 and 16.2 clone for every use; hence this bug.
So the fix is to stop cloning and reuse the typedef's DIE instead.
There is one exception, where dropping the clone outright would lose
the tag:
typedef u64 __attribute__((btf_type_tag("tag"))) u64_t;
Here the tagged variant is itself named by a typedef, so it has no DIE of
its own to carry the annotation. Record it on the typedef being declared
instead, as is already done for a pointer to a tagged type. A variant of
a base type or of an aggregate has a DIE of its own and is tagged there,
so this applies only to a typedef of a tagged typedef.
A tag written on a direct use of a named type has nowhere left to live,
and is now dropped:
uint_t __attribute__((btf_type_tag("tag1"))) uint_A;
The reuse path returns the existing typedef DIE and records no
annotation, where the clone was previously what carried it. clang drops
such a tag too, and emits a single uint_t record.
The BPF selftests in the Linux kernel tree tag uses of typedefs in arena
code, as in arena_spinlock_t __arena lock, and do emit the clone: built
with bpf-gcc, arena_kfunc.bpf.o carries two TYPEDEF u64 records, and one
with this patch. Across a full test_progs-bpf_gcc build, 1085 objects,
three carry a duplicate typedef before and none after.
BTF consumers cannot tell such records apart. bpftool renames them when
generating a skeleton and does not define the renamed types, so the
header does not compile.
Bootstrapped and regression tested on x86_64-linux-gnu and aarch64-linux-gnu.
PR debug/128441
gcc/ChangeLog:
* dwarf2out.cc (modified_type_die): Let a variant carrying a
btf_type_tag reuse the DIE of the typedef that is its TYPE_NAME.
(gen_typedef_die): Record a btf_type_tag on the typedef being
emitted when the tagged variant's TYPE_NAME is itself a typedef.
gcc/testsuite/ChangeLog:
* gcc.dg/debug/dwarf2/pr128441.c: New test.
* gcc.dg/debug/dwarf2/dwarf-btf-type-tag-3.c: Update for the tag
on a direct use being dropped.
* gcc.dg/debug/dwarf2/dwarf-btf-type-tag-7.c: Likewise, and for
the single shared typedef DIE.
* gcc.dg/debug/dwarf2/pr125421.c: Likewise. It counts u16 typedef
DIEs as a proxy for const u16 chaining through the u16 typedef
rather than skipping to __u16, which still holds with one shared
typedef instead of two.
Signed-off-by: Vineet Gupta <vineet.gupta@linux.dev>
---
gcc/dwarf2out.cc | 22 ++++++++++++++-----
.../debug/dwarf2/dwarf-btf-type-tag-3.c | 6 +++--
.../debug/dwarf2/dwarf-btf-type-tag-7.c | 18 ++++++---------
gcc/testsuite/gcc.dg/debug/dwarf2/pr125421.c | 8 +++----
gcc/testsuite/gcc.dg/debug/dwarf2/pr128441.c | 21 ++++++++++++++++++
5 files changed, 53 insertions(+), 22 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/debug/dwarf2/pr128441.c
base-commit: be07ead43d9d980584b745ee6e06c5d4309ced1d
@@ -14001,12 +14001,9 @@ modified_type_die (tree type, int cv_quals, tree type_attrs, bool reverse,
&& (qualified_type == dtype
/* Pointer identity check above might fail when qualified_type
is a different variant node of the same typedef yet requires
- the same handling as if they matched (see PR/125421).
- Skip this when btf_type_tag attributes are present, as those
- need to be handled in the else branch below. */
+ the same handling as if they matched (see PR/125421). */
|| (TYPE_NAME (qualified_type) == name
- && TYPE_QUALS (qualified_type) == TYPE_QUALS (dtype)
- && !lookup_attribute ("btf_type_tag", type_attrs))))
+ && TYPE_QUALS (qualified_type) == TYPE_QUALS (dtype))))
{
tree origin = decl_ultimate_origin (name);
@@ -26721,6 +26718,21 @@ gen_typedef_die (tree decl, dw_die_ref context_die)
add_type_attribute (type_die, type, decl_quals (decl), false,
context_die);
+ /* TYPE here is a variant whose own TYPE_NAME is a typedef. Such a variant
+ reuses that typedef's DIE rather than cloning one, so it has nowhere of
+ its own to carry a btf_type_tag. Record the tag on the typedef being
+ emitted. A variant of a base type or of an aggregate has a DIE of its
+ own and is tagged there already. */
+ if (DECL_ORIGINAL_TYPE (decl)
+ && TYPE_NAME (type)
+ && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
+ && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
+ {
+ tree tags = lookup_attribute ("btf_type_tag", TYPE_ATTRIBUTES (type));
+ if (tags)
+ gen_btf_tag_dies (tags, type_die);
+ }
+
if (is_naming_typedef_decl (decl))
/* We want that all subsequent calls to lookup_type_die with
TYPE in argument yield the DW_TAG_typedef we have just
@@ -11,5 +11,7 @@ typedef int __tag1 bar;
foo __tag2 x;
const bar y;
-/* { dg-final { scan-assembler-times "DIE \\(\[^\n\]*\\) DW_TAG_GNU_annotation" 2 } } */
-/* { dg-final { scan-assembler-times " DW_AT_name: \"btf_type_tag\"" 2 } } */
+/* "tag2" is on a direct use of 'foo' and is dropped. Only "tag1",
+ declared on 'bar', is recorded. */
+/* { dg-final { scan-assembler-times "DIE \\(\[^\n\]*\\) DW_TAG_GNU_annotation" 1 } } */
+/* { dg-final { scan-assembler-times " DW_AT_name: \"btf_type_tag\"" 1 } } */
@@ -11,15 +11,11 @@ typedef int __tag1 foo;
foo a;
foo __tag2 b;
-/* { dg-final { scan-assembler-times "DIE \\(\[^\n\]*\\) DW_TAG_GNU_annotation" 2 } } */
-/* { dg-final { scan-assembler-times " DW_AT_name: \"btf_type_tag\"" 2 } } */
+/* "tag2" is on a direct use of 'foo' and is dropped. Only "tag1",
+ declared on the typedef, is recorded. */
+/* { dg-final { scan-assembler-times "DIE \\(\[^\n\]*\\) DW_TAG_GNU_annotation" 1 } } */
+/* { dg-final { scan-assembler-times " DW_AT_name: \"btf_type_tag\"" 1 } } */
+/* { dg-final { scan-assembler-times " DW_AT_GNU_annotation" 1 } } */
-/* Due to an ambiguity in the tree attribute list, it is not currently possible
- to distinguish with certaianty whether "tag1" appears to the left or right
- of "foo" in the declaration of "b" above. This means that the DIE for
- "tag1" is also referred in the AT_GNU_annotation chain of the duplicate
- typedef DIE annotated wtih "tag2", for a total of 3 AT_GNU_annotations. */
-/* { dg-final { scan-assembler-times " DW_AT_GNU_annotation" 3 } } */
-
-/* A duplicate typedef die must be created for the tagged use in 'b'. */
-/* { dg-final { scan-assembler-times "DIE \\(\[^\n\]*\\) DW_TAG_typedef" 2 } } */
+/* One typedef DIE, shared by 'a' and 'b'. */
+/* { dg-final { scan-assembler-times "DIE \\(\[^\n\]*\\) DW_TAG_typedef" 1 } } */
@@ -12,7 +12,7 @@ const u16 perm;
/* The exact failing pattern is hard to encode with TCL regex machinery,
so resort to an indirect way: in the buggy output, const_type points
- directly to __u16, and only one u16 typedef DIE is emitted (for the
- btf_type_tag use). In the fixed output, const_type points to a u16
- typedef, creating a second u16 typedef DIE.
-/* { dg-final { scan-assembler-times "(DW_AT_name: \"u16\"|\"u16..\"\[^\\r\\n\]*DW_AT_name)" 2 } } */
+ directly to __u16. In the fixed output it points to a u16 typedef.
+ The tagged use of u16 shares that same typedef DIE rather than
+ cloning one, so a single u16 typedef is emitted. */
+/* { dg-final { scan-assembler-times "(DW_AT_name: \"u16\"|\"u16..\"\[^\\r\\n\]*DW_AT_name)" 1 } } */
new file mode 100644
@@ -0,0 +1,21 @@
+/* Test dwarf generation for btf_type_tag on a typedef whose underlying
+ type is itself a typedef. The tagged variant's TYPE_NAME is that inner
+ typedef, so the variant has no DIE of its own and the tag is recorded on
+ the outer typedef rather than on a clone of the inner one. */
+/* { dg-do compile } */
+/* { dg-options "-gdwarf -dA" } */
+
+#define __tag __attribute__((btf_type_tag ("tag1")))
+
+typedef unsigned long long u64;
+typedef u64 __tag u64_t;
+
+u64 plain;
+u64_t tagged;
+
+/* One u64 typedef, shared by both, and no clone of it. */
+/* { dg-final { scan-assembler-times "DIE \\(\[^\n\]*\\) DW_TAG_typedef" 2 } } */
+
+/* The tag is on the u64_t typedef. */
+/* { dg-final { scan-assembler-times "DIE \\(\[^\n\]*\\) DW_TAG_GNU_annotation" 1 } } */
+/* { dg-final { scan-assembler-times " DW_AT_GNU_annotation" 1 } } */