It's often important for BPF program to know kernel version or some specific config values (e.g., CONFIG_HZ to convert jiffies to seconds) and change or adjust program logic based on their values. As of today, any such need has to be resolved by recompiling BPF program for specific kernel and kernel configuration. In practice this is usually achieved by using BCC and its embedded LLVM/Clang. With such set up #ifdef CONFIG_XXX and similar compile-time constructs allow to deal with kernel varieties. With CO-RE (Compile Once – Run Everywhere) approach, this is not an option, unfortunately. All such logic variations have to be done as a normal C language constructs (i.e., if/else, variables, etc), not a preprocessor directives. This patch series add support for such advanced scenarios through C extern variables. These extern variables will be recognized by libbpf and supplied through extra .extern internal map, similarly to global data. This .extern map is read-only, which allows BPF verifier to track its content precisely as constants. That gives an opportunity to have pre-compiled BPF program, which can potentially use BPF functionality (e.g., BPF helpers) or kernel features (types, fields, etc), that are available only on a subset of targeted kernels, while effectively eleminating (through verifier's dead code detection) such unsupported functionality for other kernels (typically, older versions). Patch #5 contains all the details. Patch #5 explicitly tests a scenario of using unsupported BPF helper, to validate the approach. As part of this patch set, libbpf also allows usage of initialized global (non-static) variables, which provides better Clang semantics, which is closer and better aligned witht kernel vs userspace BPF map contents sharing. Outline of the patch set: - patches #1-#3 do some preliminary refactorings of libbpf relocation logic and some more clean ups; - patch #4 allows non-static variables and converts few tests to use them; - patch #5 adds support for externs to libbpf; - patch #6 adds tests for externs. Andrii Nakryiko (6): selftests/bpf: ensure no DWARF relocations for BPF object files libbpf: refactor relocation handling libbpf: fix various errors and warning reported by checkpatch.pl libbpf: support initialized global variables libbpf: support libbpf-provided extern variables selftests/bpf: add tests for libbpf-provided externs tools/lib/bpf/Makefile | 17 +- tools/lib/bpf/libbpf.c | 758 +++++++++++++----- tools/lib/bpf/libbpf.h | 8 +- tools/testing/selftests/bpf/Makefile | 4 +- .../selftests/bpf/prog_tests/core_extern.c | 186 +++++ .../selftests/bpf/progs/test_core_extern.c | 43 + .../bpf/progs/test_core_reloc_arrays.c | 4 +- .../progs/test_core_reloc_bitfields_direct.c | 4 +- .../progs/test_core_reloc_bitfields_probed.c | 4 +- .../bpf/progs/test_core_reloc_existence.c | 4 +- .../bpf/progs/test_core_reloc_flavors.c | 4 +- .../bpf/progs/test_core_reloc_ints.c | 4 +- .../bpf/progs/test_core_reloc_kernel.c | 4 +- .../bpf/progs/test_core_reloc_misc.c | 4 +- .../bpf/progs/test_core_reloc_mods.c | 4 +- .../bpf/progs/test_core_reloc_nesting.c | 4 +- .../bpf/progs/test_core_reloc_primitives.c | 4 +- .../bpf/progs/test_core_reloc_ptr_as_arr.c | 4 +- .../bpf/progs/test_core_reloc_size.c | 4 +- 19 files changed, 820 insertions(+), 248 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/core_extern.c create mode 100644 tools/testing/selftests/bpf/progs/test_core_extern.c -- 2.17.1