Skip to contentSkip to conversation

btf: support typedef DW_TAG_LLVM_annotation

PATCH Superseded Yonghong Song 2 participants 5 messages Oct 27, 2021 – Nov 2, 2021 BPF

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover btf: support typedef DW_TAG_LLVM_annotation

Latest llvm is able to generate DW_TAG_LLVM_annotation for typedef declarations. Latest bpf-next supports BTF_KIND_DECL_TAG for typedef declarations. This patch implemented dwarf DW_TAG_LLVM_annotation to btf BTF_KIND_DECL_TAG conversion. Patch 1 is for dwarf_loader to process DW_TAG_LLVM_annotation tags. Patch 2 is for the dwarf->btf conversion.

Yonghong Song (2):
dwarf_loader: support typedef DW_TAG_LLVM_annotation
btf_encoder: generate BTF_KIND_DECL_TAGs for typedef btf_decl_tag
attributes

 btf_encoder.c  | 12 +++++++++---
 dwarf_loader.c |  7 ++-----
 2 files changed, 11 insertions(+), 8 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/2 dwarf_loader: support typedef DW_TAG_LLVM_annotation

llvm commit ([1]) added support for btf_decl_tag attribute with typedef declaration. Eventually, DW_TAG_LLVM_annotation tag may appear inside dwarf typedef declaration tag.

kernel support for typedef BTF_KIND_DECL_TAG support is introduced in [2]. There is no additional libbpf change needed as the previous libbpf BTF_KIND_DECL_TAG support is generic enough to cover new typedef use cases.

This patch added parsing of DW_TAG_LLVM_annotation for dwarf typedef decl.

  $ cat t.c
  $ clang -O2 -g -c t.c
  $ llvm-dwarfdump --debug-info t.o
    ......
    0x00000033:   DW_TAG_typedef
                    DW_AT_type      (0x00000051 "structure ")
                    DW_AT_name      ("__t")
                    DW_AT_decl_file ("/home/yhs/t.c")
                    DW_AT_decl_line (3)
    0x0000003e:     DW_TAG_LLVM_annotation
                      DW_AT_name    ("btf_decl_tag")
                      DW_AT_const_value     ("tag1")

    0x00000047:     DW_TAG_LLVM_annotation
                      DW_AT_name    ("btf_decl_tag")
                      DW_AT_const_value     ("tag2")

    0x00000050:     NULL

Previously, pahole will issue a warning if typedef tag contains any child tag. I removed this warning since it is not true any more. Note that dwarf standard doesn't prevent typedef decl tag from having nested tags. In the future if we need to process any tag inside typedef tag, we can just add code to process it.

[1] https://reviews.llvm.org/D110127
[2] https://lore.kernel.org/bpf/[email protected]

---
 dwarf_loader.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
dwarf_loader.c +2 -5
index c5bda81..f748bd7 100644
@@ -1296,11 +1296,8 @@ static struct tag *die__create_new_typedef(Dwarf_Die *die, struct cu *cu, struct
 	if (tdef == NULL)
 		return NULL;
 
-	if (dwarf_haschildren(die)) {
-		struct dwarf_tag *dtag = tdef->namespace.tag.priv;
-		fprintf(stderr, "%s: DW_TAG_typedef %llx WITH children!\n",
-			__func__, (unsigned long long)dtag->id);
-	}
+	if (add_child_llvm_annotations(die, -1, conf, &tdef->namespace.annots))
+		return NULL;
 
 	return &tdef->namespace.tag;
 }
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/2 btf_encoder: generate BTF_KIND_DECL_TAGs for typedef btf_decl_tag attributes

In reply to Yonghong Song

Emit BTF BTF_KIND_DECL_TAGs for btf_decl_tag attributes attached to typedef declarations. The following is a simple example:

  $ cat t.c
    #define __tag1 __attribute__((btf_decl_tag("tag1")))
    #define __tag2 __attribute__((btf_decl_tag("tag2")))
    typedef struct { int a; int b; } __t __tag1 __tag2;
    __t g;
  $ clang -O2 -g -c t.c
  $ pahole -JV t.o
    btf_encoder__new: 't.o' doesn't have '.data..percpu' section
    Found 0 per-CPU variables!
    File t.o:
    [1] TYPEDEF __t type_id=2
    [2] STRUCT (anon) size=8
            a type_id=3 bits_offset=0
            b type_id=3 bits_offset=32
    [3] INT int size=4 nr_bits=32 encoding=SIGNED
    [4] DECL_TAG tag1 type_id=1 component_idx=-1
    [5] DECL_TAG tag2 type_id=1 component_idx=-1
---
 btf_encoder.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
btf_encoder.c +9 -3
index 40f6aa3..2f1f4ae 100644
@@ -1437,19 +1437,25 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu)
 	}
 
 	cu__for_each_type(cu, core_id, pos) {
+		const char *tag_name = "typedef";
 		struct namespace *ns;
 
-		if (pos->tag != DW_TAG_structure_type && pos->tag != DW_TAG_union_type)
+		if (pos->tag != DW_TAG_structure_type && pos->tag != DW_TAG_union_type &&
+		    pos->tag != DW_TAG_typedef)
 			continue;
 
+		if (pos->tag == DW_TAG_structure_type)
+			tag_name = "struct";
+		else if (pos->tag == DW_TAG_union_type)
+			tag_name = "union";
+
 		btf_type_id = type_id_off + core_id;
 		ns = tag__namespace(pos);
 		list_for_each_entry(annot, &ns->annots, node) {
 			tag_type_id = btf_encoder__add_decl_tag(encoder, annot->value, btf_type_id, annot->component_idx);
 			if (tag_type_id < 0) {
 				fprintf(stderr, "error: failed to encode tag '%s' to %s '%s' with component_idx %d\n",
-					annot->value, pos->tag == DW_TAG_structure_type ? "struct" : "union",
-					namespace__name(ns), annot->component_idx);
+					annot->value, tag_name, namespace__name(ns), annot->component_idx);
 				goto out;
 			}
 		}
Signature · 1 line
2.30.2
#3 lore raw

Re: [PATCH dwarves 2/2] btf_encoder: generate BTF_KIND_DECL_TAGs for typedef btf_decl_tag attributes

Quoted 36 lines · Yonghong Song

Emit BTF BTF_KIND_DECL_TAGs for btf_decl_tag attributes attached to typedef declarations. The following is a simple example:

  $ cat t.c
    #define __tag1 __attribute__((btf_decl_tag("tag1")))
    #define __tag2 __attribute__((btf_decl_tag("tag2")))
    typedef struct { int a; int b; } __t __tag1 __tag2;
    __t g;
  $ clang -O2 -g -c t.c
  $ pahole -JV t.o
    btf_encoder__new: 't.o' doesn't have '.data..percpu' section
    Found 0 per-CPU variables!
    File t.o:
    [1] TYPEDEF __t type_id=2
    [2] STRUCT (anon) size=8
            a type_id=3 bits_offset=0
            b type_id=3 bits_offset=32
    [3] INT int size=4 nr_bits=32 encoding=SIGNED
    [4] DECL_TAG tag1 type_id=1 component_idx=-1
    [5] DECL_TAG tag2 type_id=1 component_idx=-1
---
 btf_encoder.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
btf_encoder.c +1 -0
index 40f6aa3..2f1f4ae 100644
@@ -1437,19 +1437,25 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu)
        }
 
        cu__for_each_type(cu, core_id, pos) {
+               const char *tag_name = "typedef";
                struct namespace *ns;

btf_encoder.c int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu) · line 1450

Quoted 10 lines
btf_encoder.c +6 -1
 
-               if (pos->tag != DW_TAG_structure_type && pos->tag != DW_TAG_union_type)
+               if (pos->tag != DW_TAG_structure_type && pos->tag != DW_TAG_union_type &&
+                   pos->tag != DW_TAG_typedef)
                        continue;
 
+               if (pos->tag == DW_TAG_structure_type)
+                       tag_name = "struct";
+               else if (pos->tag == DW_TAG_union_type)
+                       tag_name = "union";

nit: switch instead of these two related sets of if/else blocks would be cleaner

Quoted 16 lines
btf_encoder.c +2 -3
+
                btf_type_id = type_id_off + core_id;
                ns = tag__namespace(pos);
                list_for_each_entry(annot, &ns->annots, node) {
                        tag_type_id = btf_encoder__add_decl_tag(encoder, annot->value, btf_type_id, annot->component_idx);
                        if (tag_type_id < 0) {
                                fprintf(stderr, "error: failed to encode tag '%s' to %s '%s' with component_idx %d\n",
-                                       annot->value, pos->tag == DW_TAG_structure_type ? "struct" : "union",
-                                       namespace__name(ns), annot->component_idx);
+                                       annot->value, tag_name, namespace__name(ns), annot->component_idx);
                                goto out;
                        }
                }
--

2.30.2

#4 lore raw

Re: [PATCH dwarves 2/2] btf_encoder: generate BTF_KIND_DECL_TAGs for typedef btf_decl_tag attributes

Quoted 43 lines · Andrii Nakryiko
Quoted 42 lines · Yonghong Song

Emit BTF BTF_KIND_DECL_TAGs for btf_decl_tag attributes attached to typedef declarations. The following is a simple example:

   $ cat t.c
     #define __tag1 __attribute__((btf_decl_tag("tag1")))
     #define __tag2 __attribute__((btf_decl_tag("tag2")))
     typedef struct { int a; int b; } __t __tag1 __tag2;
     __t g;
   $ clang -O2 -g -c t.c
   $ pahole -JV t.o
     btf_encoder__new: 't.o' doesn't have '.data..percpu' section
     Found 0 per-CPU variables!
     File t.o:
     [1] TYPEDEF __t type_id=2
     [2] STRUCT (anon) size=8
             a type_id=3 bits_offset=0
             b type_id=3 bits_offset=32
     [3] INT int size=4 nr_bits=32 encoding=SIGNED
     [4] DECL_TAG tag1 type_id=1 component_idx=-1
     [5] DECL_TAG tag2 type_id=1 component_idx=-1
---
  btf_encoder.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
btf_encoder.c +3 -1
index 40f6aa3..2f1f4ae 100644
@@ -1437,19 +1437,25 @@ int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu)
         }
 
         cu__for_each_type(cu, core_id, pos) {
+               const char *tag_name = "typedef";
                 struct namespace *ns;
 
-               if (pos->tag != DW_TAG_structure_type && pos->tag != DW_TAG_union_type)
+               if (pos->tag != DW_TAG_structure_type && pos->tag != DW_TAG_union_type &&
+                   pos->tag != DW_TAG_typedef)
                         continue;
 
Quoted 6 lines

btf_encoder.c int btf_encoder__encode_cu(struct btf_encoder *encoder, struct cu *cu) · line 1450

Quoted 4 lines
btf_encoder.c +4 -0
+               if (pos->tag == DW_TAG_structure_type)
+                       tag_name = "struct";
+               else if (pos->tag == DW_TAG_union_type)
+                       tag_name = "union";

nit: switch instead of these two related sets of if/else blocks would be cleaner

Sure. Will make the change in v2.

Quoted 18 lines
Quoted 18 lines
btf_encoder.c +2 -3
+
                 btf_type_id = type_id_off + core_id;
                 ns = tag__namespace(pos);
                 list_for_each_entry(annot, &ns->annots, node) {
                         tag_type_id = btf_encoder__add_decl_tag(encoder, annot->value, btf_type_id, annot->component_idx);
                         if (tag_type_id < 0) {
                                 fprintf(stderr, "error: failed to encode tag '%s' to %s '%s' with component_idx %d\n",
-                                       annot->value, pos->tag == DW_TAG_structure_type ? "struct" : "union",
-                                       namespace__name(ns), annot->component_idx);
+                                       annot->value, tag_name, namespace__name(ns), annot->component_idx);
                                 goto out;
                         }
                 }
--

2.30.2