bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: Andrii Nakryiko <andrii@kernel.org>, <bpf@vger.kernel.org>,
	<netdev@vger.kernel.org>, <ast@fb.com>, <daniel@iogearbox.net>
Cc: <kernel-team@fb.com>
Subject: Re: [PATCH v3 bpf-next 11/18] libbpf: add linker extern resolution support for functions and global variables
Date: Fri, 23 Apr 2021 11:46:39 -0700	[thread overview]
Message-ID: <56eaffb0-b9da-371c-d1e1-6ae5f998e158@fb.com> (raw)
In-Reply-To: <20210423181348.1801389-12-andrii@kernel.org>



On 4/23/21 11:13 AM, Andrii Nakryiko wrote:
> 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>

Acked-by: Yonghong Song <yhs@fb.com>

  reply	other threads:[~2021-04-23 18:47 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-23 18:13 [PATCH v3 bpf-next 00/18] BPF static linker: support externs Andrii Nakryiko
2021-04-23 18:13 ` [PATCH v3 bpf-next 01/18] bpftool: support dumping BTF VAR's "extern" linkage Andrii Nakryiko
2021-04-23 18:13 ` [PATCH v3 bpf-next 02/18] bpftool: dump more info about DATASEC members Andrii Nakryiko
2021-04-23 18:13 ` [PATCH v3 bpf-next 03/18] libbpf: suppress compiler warning when using SEC() macro with externs Andrii Nakryiko
2021-04-23 18:13 ` [PATCH v3 bpf-next 04/18] libbpf: mark BPF subprogs with hidden visibility as static for BPF verifier Andrii Nakryiko
2021-04-23 18:23   ` Yonghong Song
2021-04-23 18:13 ` [PATCH v3 bpf-next 05/18] libbpf: allow gaps in BPF program sections to support overriden weak functions Andrii Nakryiko
2021-04-23 18:13 ` [PATCH v3 bpf-next 06/18] libbpf: refactor BTF map definition parsing Andrii Nakryiko
2021-04-23 18:36   ` Yonghong Song
2021-04-23 18:13 ` [PATCH v3 bpf-next 07/18] libbpf: factor out symtab and relos sanity checks Andrii Nakryiko
2021-04-23 18:13 ` [PATCH v3 bpf-next 08/18] libbpf: make few internal helpers available outside of libbpf.c Andrii Nakryiko
2021-04-23 18:13 ` [PATCH v3 bpf-next 09/18] libbpf: extend sanity checking ELF symbols with externs validation Andrii Nakryiko
2021-04-23 18:39   ` Yonghong Song
2021-04-23 18:13 ` [PATCH v3 bpf-next 10/18] libbpf: tighten BTF type ID rewriting with error checking Andrii Nakryiko
2021-04-23 18:40   ` Yonghong Song
2021-04-23 18:13 ` [PATCH v3 bpf-next 11/18] libbpf: add linker extern resolution support for functions and global variables Andrii Nakryiko
2021-04-23 18:46   ` Yonghong Song [this message]
2021-04-23 18:13 ` [PATCH v3 bpf-next 12/18] libbpf: support extern resolution for BTF-defined maps in .maps section Andrii Nakryiko
2021-04-23 18:49   ` Yonghong Song
2021-04-23 18:13 ` [PATCH v3 bpf-next 13/18] selftests/bpf: use -O0 instead of -Og in selftests builds Andrii Nakryiko
2021-04-23 18:13 ` [PATCH v3 bpf-next 14/18] selftests/bpf: omit skeleton generation for multi-linked BPF object files Andrii Nakryiko
2021-04-23 18:13 ` [PATCH v3 bpf-next 15/18] selftests/bpf: add function linking selftest Andrii Nakryiko
2021-04-23 18:13 ` [PATCH v3 bpf-next 16/18] selftests/bpf: add global variables " Andrii Nakryiko
2021-04-23 18:13 ` [PATCH v3 bpf-next 17/18] selftests/bpf: add map " Andrii Nakryiko
2021-04-23 18:53   ` Yonghong Song
2021-04-23 21:23     ` Alexei Starovoitov
2021-04-23 21:39       ` Andrii Nakryiko
2021-04-23 18:13 ` [PATCH v3 bpf-next 18/18] selftests/bpf: document latest Clang fix expectations for linking tests Andrii Nakryiko
2021-04-23 18:51   ` Yonghong Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=56eaffb0-b9da-371c-d1e1-6ae5f998e158@fb.com \
    --to=yhs@fb.com \
    --cc=andrii@kernel.org \
    --cc=ast@fb.com \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).