public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Faust <david.faust@oracle.com>
To: gcc-patches@gcc.gnu.org
Cc: indu.bhagat@oracle.com
Subject: [PATCH v2 1/3] btf: add 'extern' linkage for variables [PR106773]
Date: Tue, 13 Dec 2022 10:44:24 -0800	[thread overview]
Message-ID: <20221213184426.8861-2-david.faust@oracle.com> (raw)
In-Reply-To: <20221213184426.8861-1-david.faust@oracle.com>

[Changes from v1:
 - Add enum btf_var_linkage in include/btf.h and use that instead of
   local #defines.
 - Fix BTF generation for extern variable with both non-defining and
   defining decls in the same CU. Add a test for this. ]

Add support for the 'extern' linkage value for BTF_KIND_VAR records,
which is used for variables declared as extern in the source file.

This also fixes a bug with BTF generation for extern variables which
have both a non-defining declaration and a defining declaration in the
same CU.

	PR target/106773

gcc/

	* btfout.cc (btf_collect_datasec): Mark extern variables as such.
	(btf_dvd_emit_preprocess_cb): Skip non-defining extern variable decl
	if there is a defining decl for the same variable.
	(btf_asm_varent): Accomodate 'extern' linkage.

gcc/testsuite/

	* gcc.dg/debug/btf/btf-variables-4.c: New test.
	* gcc.dg/debug/btf/btf-variables-5.c: New test.

include/

	* btf.h (enum btf_var_linkage): New.
	(struct btf_var): Update comment to note 'extern' linkage.
---
 gcc/btfout.cc                                 | 11 ++++++++-
 .../gcc.dg/debug/btf/btf-variables-4.c        | 24 +++++++++++++++++++
 .../gcc.dg/debug/btf/btf-variables-5.c        | 19 +++++++++++++++
 include/btf.h                                 | 11 ++++++++-
 4 files changed, 63 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-variables-4.c
 create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-variables-5.c

diff --git a/gcc/btfout.cc b/gcc/btfout.cc
index aef9fd70a28..677e8324424 100644
--- a/gcc/btfout.cc
+++ b/gcc/btfout.cc
@@ -314,6 +314,9 @@ btf_collect_datasec (ctf_container_ref ctfc)
 	continue;
 
       const char *section_name = node->get_section ();
+      /* Mark extern variables.  */
+      if (DECL_EXTERNAL (node->decl))
+	dvd->dvd_visibility = BTF_VAR_GLOBAL_EXTERN;
 
       if (section_name == NULL)
 	{
@@ -431,6 +434,12 @@ btf_dvd_emit_preprocess_cb (ctf_dvdef_ref *slot, ctf_container_ref arg_ctfc)
 {
   ctf_dvdef_ref var = (ctf_dvdef_ref) * slot;
 
+  /* If this is an extern variable declaration with a defining declaration
+     later, skip it so that only the defining declaration is emitted.
+     This is the same case, fix and reasoning as in CTF; see PR105089.  */
+  if (ctf_dvd_ignore_lookup (arg_ctfc, var->dvd_key))
+    return 1;
+
   /* Do not add variables which refer to unsupported types.  */
   if (btf_removed_type_p (var->dvd_type))
     return 1;
@@ -676,7 +685,7 @@ btf_asm_varent (ctf_dvdef_ref var)
   dw2_asm_output_data (4, var->dvd_name_offset, "btv_name");
   dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_VAR, 0, 0), "btv_info");
   dw2_asm_output_data (4, get_btf_id (var->dvd_type), "btv_type");
-  dw2_asm_output_data (4, (var->dvd_visibility ? 1 : 0), "btv_linkage");
+  dw2_asm_output_data (4, var->dvd_visibility, "btv_linkage");
 }
 
 /* Asm'out a member description following a BTF_KIND_STRUCT or
diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-variables-4.c b/gcc/testsuite/gcc.dg/debug/btf/btf-variables-4.c
new file mode 100644
index 00000000000..d77600bae1c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/btf/btf-variables-4.c
@@ -0,0 +1,24 @@
+/* Test BTF generation for extern variables.  */
+
+/* { dg-do compile } */
+/* { dg-options "-O0 -gbtf -dA" } */
+
+/* Expect 4 variables.  */
+/* { dg-final { scan-assembler-times "\[\t \]0xe000000\[\t \]+\[^\n\]*btv_info" 4 } } */
+
+/* 2 extern, 1 global, 1 static.  */
+/* { dg-final { scan-assembler-times "\[\t \]0\[\t \]+\[^\n\]*btv_linkage" 1 } } */
+/* { dg-final { scan-assembler-times "\[\t \]0x1\[\t \]+\[^\n\]*btv_linkage" 1 } } */
+/* { dg-final { scan-assembler-times "\[\t \]0x2\[\t \]+\[^\n\]*btv_linkage" 2 } } */
+
+extern int a;
+extern const int b;
+int c;
+static const int d = 5;
+
+int foo (int x)
+{
+  c = a + b + x;
+
+  return c + d;
+}
diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-variables-5.c b/gcc/testsuite/gcc.dg/debug/btf/btf-variables-5.c
new file mode 100644
index 00000000000..8aae76cacab
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/btf/btf-variables-5.c
@@ -0,0 +1,19 @@
+/* Test BTF generation for extern variable with both non-defining and
+   defining declarations.
+
+   In this case, only a single variable record should be emitted,
+   with 'global' linkage. However two array types will be generated.  */
+
+/* { dg-do compile } */
+/* { dg-options "-O0 -gbtf -dA" } */
+
+/* Expect 1 variable with global (1) linkage.  */
+/* { dg-final { scan-assembler-times "\[\t \]0xe000000\[\t \]+\[^\n\]*btv_info" 1 } } */
+/* { dg-final { scan-assembler-times "\[\t \]0x1\[\t \]+\[^\n\]*btv_linkage" 1 } } */
+
+/* Expect 2 array types, one of which is unsized.  */
+/* { dg-final { scan-assembler-times "\[\t \]0x4\[\t \]+\[^\n\]*bta_nelems" 1 } } */
+/* { dg-final { scan-assembler-times "\[\t \]0\[\t \]+\[^\n\]*bta_nelems" 1 } } */
+
+extern const char FOO[];
+const char FOO[] = "foo";
diff --git a/include/btf.h b/include/btf.h
index eba67f9d599..da621353aa1 100644
--- a/include/btf.h
+++ b/include/btf.h
@@ -178,11 +178,20 @@ struct btf_param
   uint32_t type;	/* Type of parameter.  */
 };
 
+/* BTF_KIND_VAR records encode linkage information in a single
+   trailing struct btf_var.  These are the supported values.  */
+enum btf_var_linkage
+{
+  BTF_VAR_STATIC = 0,
+  BTF_VAR_GLOBAL_ALLOCATED = 1,
+  BTF_VAR_GLOBAL_EXTERN = 2,
+};
+
 /* BTF_KIND_VAR is followed by a single struct btf_var, which describes
    information about the variable.  */
 struct btf_var
 {
-  uint32_t linkage;	/* Currently only 0=static or 1=global.  */
+  uint32_t linkage;	/* 0=static, 1=global, 2=extern.  */
 };
 
 /* BTF_KIND_DATASEC is followed by VLEN struct btf_var_secinfo entries,
-- 
2.38.1


  reply	other threads:[~2022-12-13 18:44 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-13 18:44 [PATCH v2 0/3] btf: fix BTF for extern items [PR106773] David Faust
2022-12-13 18:44 ` David Faust [this message]
2022-12-13 18:44 ` [PATCH v2 2/3] btf: fix 'extern const void' variables [PR106773] David Faust
2022-12-13 18:44 ` [PATCH v2 3/3] btf: correct generation for extern funcs [PR106773] David Faust
2022-12-14  7:08 ` [PATCH v2 0/3] btf: fix BTF for extern items [PR106773] Indu Bhagat

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=20221213184426.8861-2-david.faust@oracle.com \
    --to=david.faust@oracle.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=indu.bhagat@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).