The kernel bpftool expects BTF_KIND_FUNC entries in BTF to include an
annotation reflecting the linkage of functions (static, global).  For
whatever reason they abuse the `vlen' field of the BTF_KIND_FUNC entry
instead of adding a variable-part to the record like it is done with
other entry kinds.

This patch makes GCC to include this linkage info in BTF_KIND_FUNC
entries.

Tested in bpf-unknown-none target.

gcc/ChangeLog:

        PR debug/106263
        * ctfc.h (struct ctf_dtdef): Add field linkage.
        * ctfc.cc (ctf_add_function): Set ctti_linkage.
        * dwarf2ctf.cc (gen_ctf_function_type): Pass a linkage for
        function types and subprograms.
        * btfout.cc (btf_asm_func_type): Emit linkage information for the
        function.
        (btf_dtd_emit_preprocess_cb): Propagate the linkage information
        for functions.

gcc/testsuite/ChangeLog:

        PR debug/106263
        * gcc.dg/debug/btf/btf-function-4.c: New test.
        * gcc.dg/debug/btf/btf-function-5.c: Likewise.
---
 gcc/btfout.cc                                   |  6 +++++-
 gcc/ctfc.cc                                     |  3 ++-
 gcc/ctfc.h                                      |  3 ++-
 gcc/dwarf2ctf.cc                                |  4 +++-
 gcc/testsuite/gcc.dg/debug/btf/btf-function-4.c | 14 ++++++++++++++
 gcc/testsuite/gcc.dg/debug/btf/btf-function-5.c | 14 ++++++++++++++
 6 files changed, 40 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-function-4.c
 create mode 100644 gcc/testsuite/gcc.dg/debug/btf/btf-function-5.c

diff --git a/gcc/btfout.cc b/gcc/btfout.cc
index 31af50521da..594cba84910 100644
--- a/gcc/btfout.cc
+++ b/gcc/btfout.cc
@@ -463,6 +463,7 @@ btf_dtd_emit_preprocess_cb (ctf_container_ref ctfc, 
ctf_dtdef_ref dtd)
       ctf_dtdef_ref func_dtd = ggc_cleared_alloc<ctf_dtdef_t> ();
       func_dtd->dtd_data = dtd->dtd_data;
       func_dtd->dtd_data.ctti_type = dtd->dtd_type;
+      func_dtd->linkage = dtd->linkage;
 
       vec_safe_push (funcs, func_dtd);
       num_types_created++;
@@ -740,7 +741,10 @@ static void
 btf_asm_func_type (ctf_dtdef_ref dtd)
 {
   dw2_asm_output_data (4, dtd->dtd_data.ctti_name, "btt_name");
-  dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_FUNC, 0, 0), "btt_info");
+  dw2_asm_output_data (4, BTF_TYPE_INFO (BTF_KIND_FUNC, 0,
+                                         dtd->linkage),
+                       "btt_info: kind=%u, kflag=%u, linkage=%u",
+                       BTF_KIND_FUNC, 0, dtd->linkage);
   dw2_asm_output_data (4, get_btf_id (dtd->dtd_data.ctti_type), "btt_type");
 }
 
diff --git a/gcc/ctfc.cc b/gcc/ctfc.cc
index f24e7bff948..9773358a475 100644
--- a/gcc/ctfc.cc
+++ b/gcc/ctfc.cc
@@ -777,7 +777,7 @@ ctf_add_function_arg (ctf_container_ref ctfc, dw_die_ref 
func,
 ctf_id_t
 ctf_add_function (ctf_container_ref ctfc, uint32_t flag, const char * name,
                  const ctf_funcinfo_t * ctc, dw_die_ref die,
-                 bool from_global_func)
+                 bool from_global_func, int linkage)
 {
   ctf_dtdef_ref dtd;
   ctf_id_t type;
@@ -791,6 +791,7 @@ ctf_add_function (ctf_container_ref ctfc, uint32_t flag, 
const char * name,
   type = ctf_add_generic (ctfc, flag, name, &dtd, die);
 
   dtd->from_global_func = from_global_func;
+  dtd->linkage = linkage;
   dtd->dtd_data.ctti_info = CTF_TYPE_INFO (CTF_K_FUNCTION, flag, vlen);
   /* Caller must make sure CTF types for ctc->ctc_return are already added.  */
   dtd->dtd_data.ctti_type = (uint32_t) ctc->ctc_return;
diff --git a/gcc/ctfc.h b/gcc/ctfc.h
index 001e544ef08..bcf3a43ae1b 100644
--- a/gcc/ctfc.h
+++ b/gcc/ctfc.h
@@ -161,6 +161,7 @@ struct GTY ((for_user)) ctf_dtdef
   ctf_itype_t dtd_data;              /* Type node.  */
   bool from_global_func; /* Whether this type was added from a global
                            function.  */
+  uint32_t linkage;           /* Used in function types.  0=local, 1=global.  
*/
   union GTY ((desc ("ctf_dtu_d_union_selector (&%1)")))
   {
     /* struct, union, or enum.  */
@@ -423,7 +424,7 @@ extern ctf_id_t ctf_add_forward (ctf_container_ref, 
uint32_t, const char *,
 extern ctf_id_t ctf_add_typedef (ctf_container_ref, uint32_t, const char *,
                                 ctf_id_t, dw_die_ref);
 extern ctf_id_t ctf_add_function (ctf_container_ref, uint32_t, const char *,
-                                 const ctf_funcinfo_t *, dw_die_ref, bool);
+                                 const ctf_funcinfo_t *, dw_die_ref, bool, 
int);
 extern ctf_id_t ctf_add_sou (ctf_container_ref, uint32_t, const char *,
                             uint32_t, size_t, dw_die_ref);
 
diff --git a/gcc/dwarf2ctf.cc b/gcc/dwarf2ctf.cc
index a6329ab6ee4..397100004c2 100644
--- a/gcc/dwarf2ctf.cc
+++ b/gcc/dwarf2ctf.cc
@@ -644,6 +644,7 @@ gen_ctf_function_type (ctf_container_ref ctfc, dw_die_ref 
function,
 
   ctf_funcinfo_t func_info;
   uint32_t num_args = 0;
+  int linkage = get_AT_flag (function, DW_AT_external);
 
   ctf_id_t return_type_id;
   ctf_id_t function_type_id;
@@ -687,7 +688,8 @@ gen_ctf_function_type (ctf_container_ref ctfc, dw_die_ref 
function,
                                       function_name,
                                       (const ctf_funcinfo_t *)&func_info,
                                       function,
-                                      from_global_func);
+                                      from_global_func,
+                                       linkage);
 
   /* Second pass on formals: generate the CTF types corresponding to
      them and add them as CTF function args.  */
diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-function-4.c 
b/gcc/testsuite/gcc.dg/debug/btf/btf-function-4.c
new file mode 100644
index 00000000000..fd3124488f5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/btf/btf-function-4.c
@@ -0,0 +1,14 @@
+/* Test BTF linkage for functions.
+
+   We expect to see one BTF_KIND_FUNC type with static linkage encoded in the
+   BTF type's vlen field.  */
+
+/* { dg-do compile } */
+/* { dg-options "-O0 -gbtf -dA" } */
+
+/* { dg-final { scan-assembler-times "btt_info: kind=12, kflag=0, linkage=0" 1 
} } */
+
+static int funfoo (void)
+{
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-function-5.c 
b/gcc/testsuite/gcc.dg/debug/btf/btf-function-5.c
new file mode 100644
index 00000000000..12ee97fb5e3
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/debug/btf/btf-function-5.c
@@ -0,0 +1,14 @@
+/* Test BTF linkage for functions.
+
+   We expect to see one BTF_KIND_FUNC type with global linkage encoded in the
+   BTF type's vlen field.  */
+
+/* { dg-do compile } */
+/* { dg-options "-O0 -gbtf -dA" } */
+
+/* { dg-final { scan-assembler-times "btt_info: kind=12, kflag=0, linkage=1" 1 
} } */
+
+int funfoo (void)
+{
+  return 0;
+}
-- 
2.11.0

Reply via email to