linux/tools
Andrii Nakryiko a46349227c libbpf: Add linker extern resolution support for functions and global variables
Add BPF static linker logic to resolve extern variables and functions across
multiple linked together BPF object files.

For that, linker maintains a separate list of struct glob_sym structures,
which keeps track of few pieces of metadata (is it extern or resolved global,
is it a weak symbol, which ELF section it belongs to, etc) and ties together
BTF type info and ELF symbol information and keeps them in sync.

With adding support for extern variables/funcs, it's now possible for some
sections to contain both extern and non-extern definitions. This means that
some sections may start out as ephemeral (if only externs are present and thus
there is not corresponding ELF section), but will be "upgraded" to actual ELF
section as symbols are resolved or new non-extern definitions are appended.

Additional care is taken to not duplicate extern entries in sections like
.kconfig and .ksyms.

Given libbpf requires BTF type to always be present for .kconfig/.ksym
externs, linker extends this requirement to all the externs, even those that
are supposed to be resolved during static linking and which won't be visible
to libbpf. With BTF information always present, static linker will check not
just ELF symbol matches, but entire BTF type signature match as well. That
logic is stricter that BPF CO-RE checks. It probably should be re-used by
.ksym resolution logic in libbpf as well, but that's left for follow up
patches.

To make it unnecessary to rewrite ELF symbols and minimize BTF type
rewriting/removal, ELF symbols that correspond to externs initially will be
updated in place once they are resolved. Similarly for BTF type info, VAR/FUNC
and var_secinfo's (sec_vars in struct bpf_linker) are staying stable, but
types they point to might get replaced when extern is resolved. This might
leave some left-over types (even though we try to minimize this for common
cases of having extern funcs with not argument names vs concrete function with
names properly specified). That can be addresses later with a generic BTF
garbage collection. That's left for a follow up as well.

Given BTF type appending phase is separate from ELF symbol
appending/resolution, special struct glob_sym->underlying_btf_id variable is
used to communicate resolution and rewrite decisions. 0 means
underlying_btf_id needs to be appended (it's not yet in final linker->btf), <0
values are used for temporary storage of source BTF type ID (not yet
rewritten), so -glob_sym->underlying_btf_id is BTF type id in obj-btf. But by
the end of linker_append_btf() phase, that underlying_btf_id will be remapped
and will always be > 0. This is the uglies part of the whole process, but
keeps the other parts much simpler due to stability of sec_var and VAR/FUNC
types, as well as ELF symbol, so please keep that in mind while reviewing.

BTF-defined maps require some extra custom logic and is addressed separate in
the next patch, so that to keep this one smaller and easier to review.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20210423181348.1801389-12-andrii@kernel.org
2021-04-23 14:05:27 -07:00
..
accounting
arch tools headers UAPI: Sync KVM's kvm.h and vmx.h headers with the kernel sources 2021-03-06 16:54:30 -03:00
bootconfig tools/bootconfig: Add tracing_on support to helper scripts 2021-01-14 10:32:20 -05:00
bpf bpftool: Dump more info about DATASEC members 2021-04-23 14:05:26 -07:00
build perf build: Move feature cleanup under tools/build 2021-03-06 16:54:24 -03:00
cgroup
debugging docs: Update documentation to reflect what TAINT_CPU_OUT_OF_SPEC means 2020-12-08 10:53:58 -07:00
edid
firewire
firmware
gpio tools: gpio: remove uAPI v1 code no longer used by selftests 2021-02-15 11:43:28 +01:00
hv
iio
include bpf: Add a bpf_snprintf helper 2021-04-19 15:27:36 -07:00
io_uring
kvm/kvm_stat tools/kvm_stat: Exempt time-based counters 2020-12-11 19:18:51 -05:00
laptop
leds
lib libbpf: Add linker extern resolution support for functions and global variables 2021-04-23 14:05:27 -07:00
memory-model tools/memory-model: Fix typo in klitmus7 compatibility table 2021-01-04 14:40:50 -08:00
objtool objtool,x86: Fix uaccess PUSHF/POPF validation 2021-03-12 09:15:49 +01:00
pci
pcmcia
perf Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next 2021-03-09 18:07:05 -08:00
power platform-drivers-x86 for v5.12-1 2021-02-22 08:50:01 -08:00
scripts tools: Allow proper CC/CXX/... override with LLVM=1 in Makefile.include 2021-04-15 16:50:21 -07:00
spi
testing selftests/bpf: Add docs target as all dependency 2021-04-20 15:01:59 -07:00
thermal/tmon
time
tracing tracing/tools: fix a couple of spelling mistakes 2021-02-25 12:54:16 -05:00
usb
virtio tools/virtio: add barrier for aarch64 2020-12-18 16:14:30 -05:00
vm
wmi
Makefile tracing/tools: Add the latency-collector to tools directory 2021-02-12 11:52:59 -05:00