• Martin KaFai Lau's avatar
    libbpf: Support extern kernel function · 5bd022ec
    Martin KaFai Lau authored
    This patch is to make libbpf able to handle the following extern
    kernel function declaration and do the needed relocations before
    loading the bpf program to the kernel.
    
    extern int foo(struct sock *) __attribute__((section(".ksyms")))
    
    In the collect extern phase, needed changes is made to
    bpf_object__collect_externs() and find_extern_btf_id() to collect
    extern function in ".ksyms" section.  The func in the BTF datasec also
    needs to be replaced by an int var.  The idea is similar to the existing
    handling in extern var.  In case the BTF may not have a var, a dummy ksym
    var is added at the beginning of bpf_object__collect_externs()
    if there is func under ksyms datasec.  It will also change the
    func linkage from extern to global which the kernel can support.
    It also assigns a param name if it does not have one.
    
    In the collect relo phase, it will record the kernel function
    call as RELO_EXTERN_FUNC.
    
    bpf_object__resolve_ksym_func_btf_id() is added to find the func
    btf_id of the running kernel.
    
    During actual relocation, it will patch the BPF_CALL instruction with
    src_reg = BPF_PSEUDO_FUNC_CALL and insn->imm set to the running
    kernel func's btf_id.
    
    The required LLVM patch: https://reviews.llvm.org/D93563Signed-off-by: default avatarMartin KaFai Lau <kafai@fb.com>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20210325015234.1548923-1-kafai@fb.com
    5bd022ec
libbpf.c 295 KB