• W
    bpf tools: Collect symbol table from SHT_SYMTAB section · bec7d68c
    Wang Nan 提交于
    This patch collects symbols section. This section is useful when linking
    BPF maps.
    
    What 'bpf_map_xxx()' functions actually require are map's file
    descriptors (and the internal verifier converts fds into pointers to
    'struct bpf_map'), which we don't know when compiling. Therefore, we
    should make compiler generate a 'ldr_64 r1, <imm>' instruction, and
    fill the 'imm' field with the actual file descriptor when loading in
    libbpf.
    
    BPF programs should be written in this way:
    
     struct bpf_map_def SEC("maps") my_map = {
        .type = BPF_MAP_TYPE_HASH,
        .key_size = sizeof(unsigned long),
        .value_size = sizeof(unsigned long),
        .max_entries = 1000000,
     };
    
     SEC("my_func=sys_write")
     int my_func(void *ctx)
     {
         ...
         bpf_map_update_elem(&my_map, &key, &value, BPF_ANY);
         ...
     }
    
    Compiler should convert '&my_map' into a 'ldr_64, r1, <imm>'
    instruction, where imm should be the address of 'my_map'. According to
    the address, libbpf knows which map it actually referenced, and then
    fills the imm field with the 'fd' of that map created by it.
    
    However, since we never really 'link' the object file, the imm field is
    only a record in relocation section. Therefore libbpf should do the
    relocation:
    
     1. In relocation section (type == SHT_REL), positions of each such
        'ldr_64' instruction are recorded with a reference of an entry in
        symbol table (SHT_SYMTAB);
    
     2. From records in symbol table we can find the indics of map
        variables.
    
    Libbpf first record SHT_SYMTAB and positions of each instruction which
    required bu such operation. Then create file descriptor. Finally, after
    map creation complete, replace the imm field.
    
    This is the first patch of BPF map related stuff. It records SHT_SYMTAB
    into object's efile field for further use.
    Signed-off-by: NWang Nan <wangnan0@huawei.com>
    Acked-by: NAlexei Starovoitov <ast@plumgrid.com>
    Cc: Brendan Gregg <brendan.d.gregg@gmail.com>
    Cc: Daniel Borkmann <daniel@iogearbox.net>
    Cc: David Ahern <dsahern@gmail.com>
    Cc: He Kuang <hekuang@huawei.com>
    Cc: Jiri Olsa <jolsa@kernel.org>
    Cc: Kaixu Xia <xiakaixu@huawei.com>
    Cc: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
    Cc: Namhyung Kim <namhyung@kernel.org>
    Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
    Cc: Zefan Li <lizefan@huawei.com>
    Cc: pi3orama@163.com
    Link: http://lkml.kernel.org/r/1435716878-189507-12-git-send-email-wangnan0@huawei.comSigned-off-by: NArnaldo Carvalho de Melo <acme@redhat.com>
    bec7d68c
libbpf.c 8.8 KB