libbpf: detect supported kernel BTF features and sanitize BTF
authorAndrii Nakryiko <andriin@fb.com>
Fri, 10 May 2019 21:13:15 +0000 (14:13 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Sun, 12 May 2019 23:31:20 +0000 (01:31 +0200)
commitd7c4b3980c18e81c0470f5df6d96d832f446d26f
tree40533816350a46db39797d06ac310ed229a4519a
parentff1f28c03f6a7cb5ee5802288258c2fc07ed9b07
libbpf: detect supported kernel BTF features and sanitize BTF

Depending on used versions of libbpf, Clang, and kernel, it's possible to
have valid BPF object files with valid BTF information, that still won't
load successfully due to Clang emitting newer BTF features (e.g.,
BTF_KIND_FUNC, .BTF.ext's line_info/func_info, BTF_KIND_DATASEC, etc), that
are not yet supported by older kernel.

This patch adds detection of BTF features and sanitizes BPF object's BTF
by substituting various supported BTF kinds, which have compatible layout:
  - BTF_KIND_FUNC -> BTF_KIND_TYPEDEF
  - BTF_KIND_FUNC_PROTO -> BTF_KIND_ENUM
  - BTF_KIND_VAR -> BTF_KIND_INT
  - BTF_KIND_DATASEC -> BTF_KIND_STRUCT

Replacement is done in such a way as to preserve as much information as
possible (names, sizes, etc) where possible without violating kernel's
validation rules.

v2->v3:
  - remove duplicate #defines from libbpf_util.h

v1->v2:
  - add internal libbpf_internal.h w/ common stuff
  - switch SK storage BTF to use new libbpf__probe_raw_btf()

Reported-by: Alexei Starovoitov <ast@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
tools/lib/bpf/libbpf.c
tools/lib/bpf/libbpf_internal.h [new file with mode: 0644]
tools/lib/bpf/libbpf_probes.c