public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Cupertino Miranda <cupertino.miranda@oracle.com>
To: gcc-patches@gcc.gnu.org
Cc: jose.marchesi@oracle.com, david.faust@oracle.com,
	elena.zannoni@oracle.com,
	Cupertino Miranda <cupertino.miranda@oracle.com>
Subject: [PATCH 2/3] bpf: Fix access string default for CO-RE type based relocations
Date: Wed, 13 Mar 2024 14:24:40 +0000	[thread overview]
Message-ID: <20240313142441.180242-2-cupertino.miranda@oracle.com> (raw)
In-Reply-To: <20240313142441.180242-1-cupertino.miranda@oracle.com>

Although part of all CO-RE relocation data, type based relocations do
not require an access string.
Initial implementation defined it as an empty string.
On the other hand, libbpf when parsing the CO-RE relocations verifies
that those strings would contain "0", otherwise reports an error.
This patch makes GCC compliant with libbpf expectations.

gcc/Changelog:
	* config/bpf/btfext-out.cc (cpf_core_reloc_add): Correct for new code.
	Add assert to validate the string is set.
	* config/bpf/core-builtins.cc (cr_final): Make string struct
	field as const.
	(process_enum_value): Correct for field type change.
	(process_type): Set access string to "0".

gcc/testsuite/ChangeLog:
	* gcc.target/bpf/core-builtin-type-based.c: Correct.
	* gcc.target/bpf/core-builtin-type-id.c: Correct.
---
 gcc/config/bpf/btfext-out.cc                           |  5 +++--
 gcc/config/bpf/core-builtins.cc                        | 10 ++++++----
 gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c |  1 +
 gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c    |  1 +
 4 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/gcc/config/bpf/btfext-out.cc b/gcc/config/bpf/btfext-out.cc
index 57c0dc323812..ff1fd0739f1e 100644
--- a/gcc/config/bpf/btfext-out.cc
+++ b/gcc/config/bpf/btfext-out.cc
@@ -299,8 +299,9 @@ bpf_core_reloc_add (const tree type, const char * section_name,
 
   /* Buffer the access string in the auxiliary strtab.  */
   bpfcr->bpfcr_astr_off = 0;
-  if (accessor != NULL)
-    bpfcr->bpfcr_astr_off = btf_ext_add_string (accessor);
+  gcc_assert (accessor != NULL);
+  bpfcr->bpfcr_astr_off = btf_ext_add_string (accessor);
+
   bpfcr->bpfcr_type = get_btf_id (ctf_lookup_tree_type (ctfc, type));
   bpfcr->bpfcr_insn_label = label;
   bpfcr->bpfcr_kind = kind;
diff --git a/gcc/config/bpf/core-builtins.cc b/gcc/config/bpf/core-builtins.cc
index 4256fea15e49..70b14e48e6e5 100644
--- a/gcc/config/bpf/core-builtins.cc
+++ b/gcc/config/bpf/core-builtins.cc
@@ -205,7 +205,7 @@ struct cr_local
 /* Core Relocation Final data */
 struct cr_final
 {
-  char *str;
+  const char *str;
   tree type;
   enum btf_core_reloc_kind kind;
 };
@@ -868,8 +868,10 @@ process_enum_value (struct cr_builtins *data)
 	{
 	  if (TREE_VALUE (l) == expr)
 	    {
-	      ret.str = (char *) ggc_alloc_atomic ((index / 10) + 1);
-	      sprintf (ret.str, "%d", index);
+	      char *tmp = (char *) ggc_alloc_atomic ((index / 10) + 1);
+	      sprintf (tmp, "%d", index);
+	      ret.str = (const char *) tmp;
+
 	      break;
 	    }
 	  index++;
@@ -987,7 +989,7 @@ process_type (struct cr_builtins *data)
 	      || data->kind == BPF_RELO_TYPE_MATCHES);
 
   struct cr_final ret;
-  ret.str = NULL;
+  ret.str = ggc_strdup ("0");
   ret.type = data->type;
   ret.kind = data->kind;
 
diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c b/gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c
index 74a8d5a14d9d..9d818133c084 100644
--- a/gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c
+++ b/gcc/testsuite/gcc.target/bpf/core-builtin-type-based.c
@@ -56,3 +56,4 @@ int foo(void *data)
 /* { dg-final { scan-assembler-times "0x8\[\t \]+\[^\n\]*bpfcr_kind" 13 } } BPF_TYPE_EXISTS */
 /* { dg-final { scan-assembler-times "0x9\[\t \]+\[^\n\]*bpfcr_kind" 11 } } BPF_TYPE_SIZE */
 /* { dg-final { scan-assembler-times "0xc\[\t \]+\[^\n\]*bpfcr_kind" 13 } } BPF_TYPE_MATCHES */
+/* { dg-final { scan-assembler-times "bpfcr_astr_off \[(\"\]+0\[(\"\]+" 37 } } */
diff --git a/gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c b/gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c
index 4b23288eac08..9576b91bc940 100644
--- a/gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c
+++ b/gcc/testsuite/gcc.target/bpf/core-builtin-type-id.c
@@ -38,3 +38,4 @@ int foo(void *data)
 /* { dg-final { scan-assembler-times "0\[\t \]+\[^\n\]*bpfcr_type" 0  { xfail *-*-* } } } */
 /* { dg-final { scan-assembler-times "0x6\[\t \]+\[^\n\]*bpfcr_kind" 13 } } BPF_TYPE_ID_LOCAL */
 /* { dg-final { scan-assembler-times "0x7\[\t \]+\[^\n\]*bpfcr_kind" 7 } } BPF_TYPE_ID_TARGET */
+/* { dg-final { scan-assembler-times "bpfcr_astr_off \[(\"\]+0\[(\"\]+" 20 } } */
-- 
2.30.2


  reply	other threads:[~2024-03-13 14:25 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-13 14:24 [PATCH 1/3] bpf: Fix CO-RE field expression builtins Cupertino Miranda
2024-03-13 14:24 ` Cupertino Miranda [this message]
2024-03-19 16:57   ` [PATCH 2/3] bpf: Fix access string default for CO-RE type based relocations David Faust
2024-03-20 20:31     ` Cupertino Miranda
2024-03-13 14:24 ` [PATCH 3/3] bpf: Corrected index computation when present with unnamed struct fields Cupertino Miranda
2024-03-19 17:07   ` David Faust
2024-03-20 20:32     ` Cupertino Miranda
2024-03-14  8:54 ` [PATCH 1/3] bpf: Fix CO-RE field expression builtins Jose E. Marchesi
2024-03-14 11:07   ` Cupertino Miranda
2024-03-14 13:23     ` Jose E. Marchesi
2024-03-14 13:44       ` Jose E. Marchesi
2024-03-20 20:31         ` Cupertino Miranda

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240313142441.180242-2-cupertino.miranda@oracle.com \
    --to=cupertino.miranda@oracle.com \
    --cc=david.faust@oracle.com \
    --cc=elena.zannoni@oracle.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jose.marchesi@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).