This is an archive of the discontinued LLVM Phabricator instance.

[BPF] Generate BTF info using 'btf:type_tag' annotation
AcceptedPublic

Authored by eddyz87 on Mar 12 2023, 6:47 PM.

Details

Summary

This is a follow-up for BPF mailing list discussion at [1].

Previous commit in a series updated DWARF generation for the following
example:

int __attribute__((btf_type_tag("tag1"))) *g;

To generate DWARF that looks as follows:

0x0000001e:   DW_TAG_variable
                DW_AT_name      ("g")
                DW_AT_type      (0x00000029 "int *")

0x00000029:   DW_TAG_pointer_type
                DW_AT_type      (0x00000032 "int")

0x00000032:   DW_TAG_base_type
                DW_AT_name      ("int")

0x00000036:     DW_TAG_LLVM_annotation
                  DW_AT_name    ("btf:type_tag")
                  DW_AT_const_value     ("tag1")

The fresh part is attachment of btf:type_tag annotations to types
other than pointers. This commit changes BTF generation to rely on
btf:type_tag annotations to generate TYPE_TAG entries.
This necessitates the following changes:

  • The logic for BTFTypeTypeTag chains creation is moved to BTFDebug::addType();
  • Special logic is added to avoid duplicate BTF entries for tagged and un-tagged type variants, e.g. in the following case:

    #define tag1 attribute((btf_type_tag("tag1"))) #define tag2 attribute((btf_type_tag("tag2")))

    struct foo {};

    struct bar { struct foo tag1 aa; struct foo tag2 bb; struct foo cc; };

    Debug information generated for this example contains three instances of DICompositeType(name: "foo") with different annotations fields, however single BTF definition for structure "foo" should be generated. Field BTFDebug::DIDedupMap and method BTFDebug::lookupType() are responsible for this logic;
  • Care is taken to avoid references to type tags in relocation entries.

[1] https://lore.kernel.org/bpf/87r0w9jjoq.fsf@oracle.com/

Depends on D143967

Diff Detail

Event Timeline

eddyz87 created this revision.Mar 12 2023, 6:47 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 12 2023, 6:47 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
eddyz87 requested review of this revision.Mar 12 2023, 6:47 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 12 2023, 6:47 PM
eddyz87 updated this revision to Diff 506445.Mar 19 2023, 3:55 PM
  • Safer hash/equality checks for type tags BTF de-duplication, instead of kind/name comparison complete shallow type structure is considered;
  • Support for subroutine types in type tags BTF de-duplication;
  • New script print_btf.py for use with tests. It can print BTF in the same format bpftool does;
  • Type tag BTF de-duplication tests moved to used print_btf.py.
eddyz87 updated this revision to Diff 506452.Mar 19 2023, 5:34 PM

Added %python to print_btf.py invocation to handle CI error on Windows.

eddyz87 updated this revision to Diff 524305.May 22 2023, 7:41 AM

Move type tags before CVR qualifiers when BTF is generated.

eddyz87 updated this revision to Diff 524729.May 23 2023, 8:09 AM

Simplified type tags movement algorithm, added comments and tests.

eddyz87 updated this revision to Diff 558087.Nov 13 2023, 2:57 PM

Rebase, added direct include for SmallSet.h

yonghong-song accepted this revision.Nov 14 2023, 4:36 AM

I tried a few examples and the result looks good to me. Thanks!

This revision is now accepted and ready to land.Nov 14 2023, 4:36 AM