https://gcc.gnu.org/g:f10c18df9c02dc518360426c021971838e0012d2

commit r14-9585-gf10c18df9c02dc518360426c021971838e0012d2
Author: Cupertino Miranda <cupertino.mira...@oracle.com>
Date:   Fri Mar 8 13:33:42 2024 +0000

    bpf: Corrected index computation when present with unnamed struct fields
    
    Any unnamed non-struct-or-union field is not a member of the
    BTF_KIND_STRUCT.
    For that reason, CO-RE access strings indexes should take that in
    consideration. This patch adds a condition to the incrementer that
    computes the index for the field access.
    
    gcc/ChangeLog:
            * config/bpf/core-builtins.cc (bpf_core_get_index): Check if
            field contains a DECL_NAME.
    
    gcc/testsuite/ChangeLog:
            * gcc.target/bpf/core-builtin-fieldinfo-offset-1.c: Add
            testcase for unnamed fields.

Diff:
---
 gcc/config/bpf/core-builtins.cc                                |  6 +++++-
 gcc/testsuite/gcc.target/bpf/core-builtin-fieldinfo-offset-1.c | 10 ++++++++--
 2 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc
index 70b14e48e6e..8333ad81d0e 100644
--- a/gcc/config/bpf/core-builtins.cc
+++ b/gcc/config/bpf/core-builtins.cc
@@ -553,7 +553,11 @@ bpf_core_get_index (const tree node, bool *valid)
        {
          if (l == node)
            return i;
-         i++;
+         /* Skip unnamed padding, not represented by BTF.  */
+         if (DECL_NAME(l) != NULL_TREE
+             || TREE_CODE (TREE_TYPE (l)) == UNION_TYPE
+             || TREE_CODE (TREE_TYPE (l)) == RECORD_TYPE)
+           i++;
        }
     }
   else if (code == ARRAY_REF || code == ARRAY_RANGE_REF || code == MEM_REF)
diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-fieldinfo-offset-1.c 
b/gcc/testsuite/gcc.target/bpf/core-builtin-fieldinfo-offset-1.c
index 27654205287..8b1d8b012a2 100644
--- a/gcc/testsuite/gcc.target/bpf/core-builtin-fieldinfo-offset-1.c
+++ b/gcc/testsuite/gcc.target/bpf/core-builtin-fieldinfo-offset-1.c
@@ -14,6 +14,9 @@ struct T {
   struct S s[2];
   char c;
   char d;
+  int a: 1;
+  int:31;
+  int f;
 };
 
 enum {
@@ -38,7 +41,9 @@ unsigned int foo (struct T *t)
   unsigned e1 = __builtin_preserve_field_info (bar()->d, FIELD_BYTE_OFFSET);
   unsigned e2 = __builtin_preserve_field_info (bar()->s[1].a4, 
FIELD_BYTE_OFFSET);
 
-  return s0a1 + s0a4 + s0x + s1a1 + s1a4 + s1x + c + d + e1 + e2;
+  unsigned f1 = __builtin_preserve_field_info (t->f, FIELD_BYTE_OFFSET);
+
+  return s0a1 + s0a4 + s0x + s1a1 + s1a4 + s1x + c + d + e1 + e2 + f1;
 }
 
 /* { dg-final { scan-assembler-times "\[\t \]mov\[\t \]%r\[0-9\],4" 2 } } */
@@ -65,5 +70,6 @@ unsigned int foo (struct T *t)
 /* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"0:1:1:4\"\\)" 1 } } 
*/
 /* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"0:2\"\\)" 1 } } */
 /* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"0:3\"\\)" 2 } } */
+/* { dg-final { scan-assembler-times "bpfcr_astr_off \\(\"0:5\"\\)" 1 } } */
 
-/* { dg-final { scan-assembler-times "0\[\t \]+\[^\n\]*bpfcr_kind" 10 } } */
+/* { dg-final { scan-assembler-times "0\[\t \]+\[^\n\]*bpfcr_kind" 11 } } */

Reply via email to