]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
bpf: make sure CO-RE relocs are typed with struct BTF_KIND_STRUCT
authorCupertino Miranda <cupertino.miranda@oracle.com>
Wed, 15 Jan 2025 14:25:44 +0000 (14:25 +0000)
committerCupertino Miranda <cupertino.miranda@oracle.com>
Wed, 15 Jan 2025 16:15:36 +0000 (16:15 +0000)
Based on observation within bpf-next selftests and comparisson of GCC
and clang compiled code, the BPF loader expects all CO-RE relocations to
point to BTF non const and non volatile type nodes.

gcc/ChangeLog:

* btfout.cc (get_btf_kind): Remove static from function definition.
* config/bpf/btfext-out.cc (bpf_code_reloc_add): Check if CO-RE type
is not a const or volatile.
* ctfc.h (btf_dtd_kind): Add prototype for function.

gcc/testsuite/ChangeLog:

* gcc.target/bpf/core-attr-const.c: New test.

gcc/btfout.cc
gcc/config/bpf/btfext-out.cc
gcc/ctfc.h
gcc/testsuite/gcc.target/bpf/core-attr-const.c [new file with mode: 0644]

index 16d5b7f97cc43e2d0f2e8c112543dfeae55bda2b..ff7ea42a9614d8b825cc537223306d857ecafb09 100644 (file)
@@ -148,7 +148,7 @@ get_btf_kind (uint32_t ctf_kind)
 
 /* Convenience wrapper around get_btf_kind for the common case.  */
 
-static uint32_t
+uint32_t
 btf_dtd_kind (ctf_dtdef_ref dtd)
 {
   if (!dtd)
index ad5ce9655a7c1c2a0748b400c2af7cff1be9285d..7cab59bb540001498009eb69c612b395ec6b0025 100644 (file)
@@ -298,6 +298,13 @@ bpf_core_reloc_add (const tree type, const char * section_name,
   ctf_container_ref ctfc = ctf_get_tu_ctfc ();
   ctf_dtdef_ref dtd = ctf_lookup_tree_type (ctfc, type);
 
+  /* Make sure CO-RE type is never the const or volatile version.  */
+  if ((btf_dtd_kind (dtd) == BTF_KIND_CONST
+       || btf_dtd_kind (dtd) == BTF_KIND_VOLATILE)
+      && kind >= BPF_RELO_FIELD_BYTE_OFFSET
+      && kind <= BPF_RELO_FIELD_RSHIFT_U64)
+    dtd = dtd->ref_type;
+
   /* Buffer the access string in the auxiliary strtab.  */
   bpfcr->bpfcr_astr_off = 0;
   gcc_assert (accessor != NULL);
index 6a9baa56194cc8f515a5fd90c9a66a1cf7bc3a5d..32c73be6a4120234cbe7f40846e8582abed19e3c 100644 (file)
@@ -465,4 +465,6 @@ extern void btf_mark_type_used (tree);
 extern int ctfc_get_dtd_srcloc (ctf_dtdef_ref, ctf_srcloc_ref);
 extern int ctfc_get_dvd_srcloc (ctf_dvdef_ref, ctf_srcloc_ref);
 
+extern uint32_t btf_dtd_kind (ctf_dtdef_ref dtd);
+
 #endif /* GCC_CTFC_H */
diff --git a/gcc/testsuite/gcc.target/bpf/core-attr-const.c b/gcc/testsuite/gcc.target/bpf/core-attr-const.c
new file mode 100644 (file)
index 0000000..da6113a
--- /dev/null
@@ -0,0 +1,40 @@
+/* Test to make sure CO-RE access relocs point to non const versions of the
+   type.  */
+
+/* { dg-do compile } */
+/* { dg-options "-O0 -dA -gbtf -mco-re -masm=normal" } */
+
+struct S {
+  int a;
+  int b;
+  int c;
+} __attribute__((preserve_access_index));
+
+void
+func (struct S * s)
+{
+  int *x;
+  int *y;
+  int *z;
+  struct S tmp;
+  const struct S *cs = s;
+  volatile struct S *vs = s;
+
+  /* 0:2 */
+  x = &(s->c);
+
+  /* 0:1 */
+  y = (int *) &(cs->b);
+
+  /* 0:1 */
+  z = (int *) &(vs->b);
+
+  *x = 4;
+  *y = 4;
+  *z = 4;
+}
+
+/* Both const and non const struct type should have the same bpfcr_type. */
+/* { dg-final { scan-assembler-times "0x1\t# bpfcr_type \\(struct S\\)" 1 } } */
+/* { dg-final { scan-assembler-times "0x1\t# bpfcr_type \\(const struct S\\)" 1 } } */
+/* { dg-final { scan-assembler-times "0x1\t# bpfcr_type \\(volatile struct S\\)" 1 } } */