libbpf: add bpf_core_type_matches() helper macro
authorAndrii Nakryiko <andrii@kernel.org>
Wed, 6 Jul 2022 03:56:48 +0000 (20:56 -0700)
committerAndrii Nakryiko <andrii@kernel.org>
Wed, 6 Jul 2022 04:15:19 +0000 (21:15 -0700)
This patch finalizes support for the proposed type match relation in libbpf by
adding bpf_core_type_matches() macro which emits TYPE_MATCH relocation.

Clang support for this relocation was added in [0].

  [0] https://reviews.llvm.org/D126838

Signed-off-by: Daniel Müller <deso@posteo.net>¬
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>¬
Link: https://lore.kernel.org/bpf/20220628160127.607834-7-deso@posteo.net¬
tools/lib/bpf/bpf_core_read.h

index 2308f4990e963c80ce6f1552dd4941314655664f..496e6a8ee0dc923f85afb96186a002d2b2080757 100644 (file)
@@ -184,6 +184,16 @@ enum bpf_enum_value_kind {
 #define bpf_core_type_exists(type)                                         \
        __builtin_preserve_type_info(*(typeof(type) *)0, BPF_TYPE_EXISTS)
 
+/*
+ * Convenience macro to check that provided named type
+ * (struct/union/enum/typedef) "matches" that in a target kernel.
+ * Returns:
+ *    1, if the type matches in the target kernel's BTF;
+ *    0, if the type does not match any in the target kernel
+ */
+#define bpf_core_type_matches(type)                                        \
+       __builtin_preserve_type_info(*(typeof(type) *)0, BPF_TYPE_MATCHES)
+
 /*
  * Convenience macro to get the byte size of a provided named type
  * (struct/union/enum/typedef) in a target kernel.