PR debug/112768 - btf: fix asm comment output for BTF_KIND_FUNC* kinds

The patch adds a small function to abstract out the detail and return
the name of the type.  The patch also fixes the issue of BTF_KIND_FUNC
appearing in the comments with a 'null' string.

For btf-function-6.c testcase, after the patch:

        .long   0       # TYPE 2 BTF_KIND_FUNC_PROTO ''
        .long   0xd000002       # btt_info: kind=13, kflag=0, vlen=2
        .long   0x1     # btt_type: (BTF_KIND_INT 'int')
        .long   0       # farg_name
        .long   0x1     # farg_type: (BTF_KIND_INT 'int')
        .long   0       # farg_name
        .long   0x1     # farg_type: (BTF_KIND_INT 'int')
        .long   0       # TYPE 3 BTF_KIND_FUNC_PROTO ''
        .long   0xd000001       # btt_info: kind=13, kflag=0, vlen=1
        .long   0x1     # btt_type: (BTF_KIND_INT 'int')
        .long   0x68    # farg_name
        .long   0x1     # farg_type: (BTF_KIND_INT 'int')
        .long   0x5     # TYPE 4 BTF_KIND_FUNC 'extfunc'
        .long   0xc000002       # btt_info: kind=12, kflag=0, linkage=2
        .long   0x2     # btt_type: (BTF_KIND_FUNC_PROTO '')
        .long   0xd     # TYPE 5 BTF_KIND_FUNC 'foo'
        .long   0xc000001       # btt_info: kind=12, kflag=0, linkage=1
        .long   0x3     # btt_type: (BTF_KIND_FUNC_PROTO '')

gcc/ChangeLog:

        PR debug/112768
        * btfout.cc (get_btf_type_name): New definition.
        (btf_collect_datasec): Update dtd_name to the original type name
        string.
        (btf_asm_type_ref): Use the new get_btf_type_name function
        instead.
        (btf_asm_type): Likewise.
        (btf_asm_func_type): Likewise.

gcc/testsuite/ChangeLog:

        PR debug/112768
        * gcc.dg/debug/btf/btf-function-6.c: Empty string expected with
        BTF_KIND_FUNC_PROTO.

Testing notes:
  - bootstrapped and reg tested on x86_64
  - No regressions in btf.exp on BPF target

---
 gcc/btfout.cc                                 | 22 +++++++++++++++----
 .../gcc.dg/debug/btf/btf-function-6.c         |  4 ++--
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/gcc/btfout.cc b/gcc/btfout.cc
index 5f2e99ce472..1c25404b2c0 100644
--- a/gcc/btfout.cc
+++ b/gcc/btfout.cc
@@ -158,6 +158,19 @@ get_btf_kind (uint32_t ctf_kind)
   return BTF_KIND_UNKN;
 }
 
+/* Some BTF types, like BTF_KIND_FUNC_PROTO, are anonymous.  The machinery
+   in btfout to emit BTF, may reset dtd_data->ctti_name, but does not update
+   the name in the ctf_dtdef_ref type object (deliberate choice).  This
+   interface helps abstract out that state of affairs, while giving access to
+   the name of the type as intended.  */
+
+static const char *
+get_btf_type_name (ctf_dtdef_ref dtd)
+{
+  const char *anon = "";
+  return (dtd->dtd_data.ctti_name) ? dtd->dtd_name : anon;
+}
+
 /* Helper routines to map between 'relative' and 'absolute' IDs.
 
    In BTF all records (including variables) are output in one long list, and 
all
@@ -425,6 +438,7 @@ btf_collect_datasec (ctf_container_ref ctfc)
       func_dtd->dtd_data = dtd->dtd_data;
       func_dtd->dtd_data.ctti_type = dtd->dtd_type;
       func_dtd->linkage = dtd->linkage;
+      func_dtd->dtd_name = dtd->dtd_name;
       func_dtd->dtd_type = num_types_added + num_types_created;
 
       /* Only the BTF_KIND_FUNC type actually references the name. The
@@ -722,7 +736,7 @@ btf_asm_type_ref (const char *prefix, ctf_container_ref 
ctfc, ctf_id_t ref_id)
       size_t func_id = btf_relative_func_id (ref_id);
       ctf_dtdef_ref ref_type = (*funcs)[func_id];
       dw2_asm_output_data (4, ref_id, "%s: (BTF_KIND_FUNC '%s')",
-                          prefix, ref_type->dtd_name);
+                          prefix, get_btf_type_name (ref_type));
     }
   else
     {
@@ -733,7 +747,7 @@ btf_asm_type_ref (const char *prefix, ctf_container_ref 
ctfc, ctf_id_t ref_id)
 
       dw2_asm_output_data (4, ref_id, "%s: (BTF_KIND_%s '%s')",
                           prefix, btf_kind_name (ref_kind),
-                          ref_type->dtd_name);
+                          get_btf_type_name (ref_type));
     }
 }
 
@@ -809,7 +823,7 @@ btf_asm_type (ctf_container_ref ctfc, ctf_dtdef_ref dtd)
   dw2_asm_output_data (4, dtd->dtd_data.ctti_name,
                       "TYPE %" PRIu64 " BTF_KIND_%s '%s'",
                       get_btf_id (dtd->dtd_type), btf_kind_name (btf_kind),
-                      dtd->dtd_name);
+                      get_btf_type_name (dtd));
   dw2_asm_output_data (4, BTF_TYPE_INFO (btf_kind, btf_kflag, btf_vlen),
                       "btt_info: kind=%u, kflag=%u, vlen=%u",
                       btf_kind, btf_kflag, btf_vlen);
@@ -950,7 +964,7 @@ btf_asm_func_type (ctf_container_ref ctfc, ctf_dtdef_ref 
dtd, ctf_id_t id)
   ctf_id_t ref_id = dtd->dtd_data.ctti_type;
   dw2_asm_output_data (4, dtd->dtd_data.ctti_name,
                       "TYPE %" PRIu64 " BTF_KIND_FUNC '%s'",
-                      btf_absolute_func_id (id), dtd->dtd_name);
+                      btf_absolute_func_id (id), get_btf_type_name (dtd));
   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);
diff --git a/gcc/testsuite/gcc.dg/debug/btf/btf-function-6.c 
b/gcc/testsuite/gcc.dg/debug/btf/btf-function-6.c
index e014d9990a9..802bc6d1c74 100644
--- a/gcc/testsuite/gcc.dg/debug/btf/btf-function-6.c
+++ b/gcc/testsuite/gcc.dg/debug/btf/btf-function-6.c
@@ -6,8 +6,8 @@
 /* { dg-do compile } */
 /* { dg-options "-O0 -gbtf -dA" } */
 
-/* { dg-final { scan-assembler-times " 
BTF_KIND_FUNC\[^\\r\\n\]*\[\\r\\n\]+\[^\\r\\n\]*linkage=2\[\\r\\n\]+\[^\\r\\n\]*\\(BTF_KIND_FUNC_PROTO
 'extfunc'" 1 } } */
-/* { dg-final { scan-assembler-times " 
BTF_KIND_FUNC\[^\\r\\n\]*\[\\r\\n\]+\[^\\r\\n\]*linkage=1\[\\r\\n\]+\[^\\r\\n\]*\\(BTF_KIND_FUNC_PROTO
 'foo'" 1 } } */
+/* { dg-final { scan-assembler-times " 
BTF_KIND_FUNC\[^\\r\\n\]*\[\\r\\n\]+\[^\\r\\n\]*linkage=2\[\\r\\n\]+\[^\\r\\n\]*\\(BTF_KIND_FUNC_PROTO
 ''" 1 } } */
+/* { dg-final { scan-assembler-times " 
BTF_KIND_FUNC\[^\\r\\n\]*\[\\r\\n\]+\[^\\r\\n\]*linkage=1\[\\r\\n\]+\[^\\r\\n\]*\\(BTF_KIND_FUNC_PROTO
 ''" 1 } } */
 
 extern int extfunc(int a, int b);
 
-- 
2.41.0

Reply via email to