]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
btf: emit linkage information in BTF_KIND_FUNC entries
authorJose E. Marchesi <jose.marchesi@oracle.com>
Fri, 8 Jul 2022 16:32:02 +0000 (18:32 +0200)
committerJose E. Marchesi <jose.marchesi@oracle.com>
Mon, 1 Aug 2022 17:44:12 +0000 (19:44 +0200)
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
gcc/ctfc.cc
gcc/ctfc.h
gcc/dwarf2ctf.cc
gcc/testsuite/gcc.dg/debug/btf/btf-function-4.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/debug/btf/btf-function-5.c [new file with mode: 0644]

index 31af50521da203bb9aea2fdef8bb512a32bd5c78..594cba8491059730f9ffaea86caaca0b4f883230 100644 (file)
@@ -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");
 }
 
index f24e7bff94874c0643756019c9fb4a75fd3397f9..9773358a4758b9e0883fd43d37df19cc76ea3981 100644 (file)
@@ -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;
index 001e544ef083a318ab92c3657437abde95f1e0b5..bcf3a43ae1b884fdb79ea55426eabdaca49d313c 100644 (file)
@@ -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);
 
index a6329ab6ee48ad9bb149b394d67aa2701500958c..397100004c21cf9b1bedb92a7ee69414619d2b6b 100644 (file)
@@ -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 (file)
index 0000000..fd31244
--- /dev/null
@@ -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 (file)
index 0000000..12ee97f
--- /dev/null
@@ -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;
+}