This is an archive of the discontinued LLVM Phabricator instance.

[BPF] Add BTF 64bit enum value support
ClosedPublic

Authored by yonghong-song on Apr 28 2022, 3:56 PM.

Details

Summary

Current BTF only supports 32-bit value. For example,

enum T { VAL = 0xffffFFFF00000008 };

the generated BTF looks like

.long   16                              # BTF_KIND_ENUM(id = 4)
.long   100663297                       # 0x6000001
.long   8
.long   18
.long   8

The encoded value is 8 which equals to (uint32_t)0xffffFFFF00000008
and this is incorrect.

This patch introduced BTF_KIND_ENUM64 which permits to encode
64-bit value. The format for each enumerator looks like:

.long   name_offset
.long   (uint32_t)value # lower-32 bit value
.long   value >> 32     # high-32 bit value

We use two 32-bit values to represent a 64-bit value as current
BTF type subsection has 4-byte alignment and gaps are not permitted
in the subsection.

This patch also added support for kflag (the bit 31 of CommonType.Info)
such that kflag = 1 implies the value is signed and kflag = 0
implies the value is unsigned. The kernel UAPI enumerator definition is

struct btf_enum {
      __u32   name_off;
      __s32   val;
};

so kflag = 0 with unsigned value provides backward compatability.

With this patch, for

enum T { VAL = 0xffffFFFF00000008 };

the generated BTF looks like

.long   16                              # BTF_KIND_ENUM64(id = 4)
.long   3187671053                      # 0x13000001
.long   8
.long   18
.long   8                               # 0x8
.long   4294967295                      # 0xffffffff

and the enumerator value and signedness are encoded correctly.

Diff Detail

Event Timeline

yonghong-song created this revision.Apr 28 2022, 3:56 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 28 2022, 3:56 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
yonghong-song requested review of this revision.Apr 28 2022, 3:56 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 28 2022, 3:56 PM
yonghong-song edited the summary of this revision. (Show Details)
  • change kflag meaning: kflag = 0 for unsigned and kflag = 1 for signed.
  • change the ordering of enum64 val_lo32 and val_hi32.
ast accepted this revision.May 11 2022, 4:53 PM
This revision is now accepted and ready to land.May 11 2022, 4:53 PM
  • also set signed enum for dwarf::DW_ATE_signed_char int.
ast accepted this revision.May 12 2022, 8:34 PM

good catch.

This revision was landed with ongoing or failed builds.Jun 6 2022, 11:36 AM
This revision was automatically updated to reflect the committed changes.