OSDN Git Service

libbpf: Add weak ksym support to gen_loader
authorKumar Kartikeya Dwivedi <memxor@gmail.com>
Thu, 28 Oct 2021 06:34:56 +0000 (12:04 +0530)
committerAlexei Starovoitov <ast@kernel.org>
Thu, 28 Oct 2021 23:30:06 +0000 (16:30 -0700)
commit585a3571981d8a93e2211e1ac835d31a63e68cd8
tree89ea558bbaa738c733171c0f9093fe4ec6bc7252
parentc24941cd3766b6de682dbe6809bd6af12271ab5b
libbpf: Add weak ksym support to gen_loader

This extends existing ksym relocation code to also support relocating
weak ksyms. Care needs to be taken to zero out the src_reg (currently
BPF_PSEUOD_BTF_ID, always set for gen_loader by bpf_object__relocate_data)
when the BTF ID lookup fails at runtime.  This is not a problem for
libbpf as it only sets ext->is_set when BTF ID lookup succeeds (and only
proceeds in case of failure if ext->is_weak, leading to src_reg
remaining as 0 for weak unresolved ksym).

A pattern similar to emit_relo_kfunc_btf is followed of first storing
the default values and then jumping over actual stores in case of an
error. For src_reg adjustment, we also need to perform it when copying
the populated instruction, so depending on if copied insn[0].imm is 0 or
not, we decide to jump over the adjustment.

We cannot reach that point unless the ksym was weak and resolved and
zeroed out, as the emit_check_err will cause us to jump to cleanup
label, so we do not need to recheck whether the ksym is weak before
doing the adjustment after copying BTF ID and BTF FD.

This is consistent with how libbpf relocates weak ksym. Logging
statements are added to show the relocation result and aid debugging.

Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20211028063501.2239335-4-memxor@gmail.com
tools/lib/bpf/gen_loader.c