libbpf: Support weak typed ksyms.
authorHao Luo <haoluo@google.com>
Thu, 12 Aug 2021 00:38:19 +0000 (17:38 -0700)
committerAndrii Nakryiko <andrii@kernel.org>
Fri, 13 Aug 2021 22:56:28 +0000 (15:56 -0700)
commit2211c825e7b6b99bbcabab4e0130a2779275dcc3
treec2d65fe354da96b6f72b97aa4f95325d975d095b
parentcf7a5cba86fc2d3000c555b9568f7dd0f43bf0d4
libbpf: Support weak typed ksyms.

Currently weak typeless ksyms have default value zero, when they don't
exist in the kernel. However, weak typed ksyms are rejected by libbpf
if they can not be resolved. This means that if a bpf object contains
the declaration of a nonexistent weak typed ksym, it will be rejected
even if there is no program that references the symbol.

Nonexistent weak typed ksyms can also default to zero just like
typeless ones. This allows programs that access weak typed ksyms to be
accepted by verifier, if the accesses are guarded. For example,

extern const int bpf_link_fops3 __ksym __weak;

/* then in BPF program */

if (&bpf_link_fops3) {
   /* use bpf_link_fops3 */
}

If actual use of nonexistent typed ksym is not guarded properly,
verifier would see that register is not PTR_TO_BTF_ID and wouldn't
allow to use it for direct memory reads or passing it to BPF helpers.

Signed-off-by: Hao Luo <haoluo@google.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20210812003819.2439037-1-haoluo@google.com
tools/lib/bpf/libbpf.c
tools/testing/selftests/bpf/prog_tests/ksyms_btf.c
tools/testing/selftests/bpf/progs/test_ksyms_weak.c [new file with mode: 0644]