From: Andrii Nakryiko <andrii@kernel.org>
To: bpf@vger.kernel.org
Cc: andrii@kernel.org, kernel-team@meta.com
Subject: [PATCH bpf-next 1/2] libbpf: Fix static linking of externs placed in allocated sections
Date: Fri, 25 Sep 2026 15:46:30 -0700 [thread overview]
Message-ID: <20260925224631.1490488-1-andrii@kernel.org> (raw)
Clang puts an extern variable that ends up with a section, an extern
map in .maps or an arena global in .addr_space.<N>, into the BTF
DATASEC of that section, next to the object's own definitions and
with offset 0. Unlike .kconfig and .ksyms, that section is a real
allocated ELF section whenever the same object also defines a
variable in it. The static linker handles such externs incorrectly in
two ways.
The output symbol of an unresolved extern gets the destination
section's index instead of staying SHN_UNDEF. For ephemeral sections
sec_idx is 0, so this only shows up for allocated sections, where the
extern turns into a size-zero NOTYPE definition. Linking the output
again then fails with "conflicting non-weak symbol" against a strong
definition of that variable, or, against a weak one, keeps the bogus
symbol and resolves every reference to offset 0 of the section.
When the extern is resolved by a later object in the same link, the
DATASEC entry appended for the extern keeps its placeholder offset,
as linker_append_btf() only updates the size of an existing entry.
The ELF symbol and the BTF secinfo then disagree and skeleton
generation fails with:
Error: Something is wrong for .addr_space.1's variable #1: need offset 0, already at 8.
Keep unresolved externs SHN_UNDEF in the output symbol table, and
once an extern is resolved take the offset of its DATASEC entry from
the ELF symbol, which already accounts for weak vs. strong resolution.
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
---
tools/lib/bpf/linker.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
index 53f64a1a1f25..0eaf98b093f0 100644
--- a/tools/lib/bpf/linker.c
+++ b/tools/lib/bpf/linker.c
@@ -2159,7 +2159,10 @@ static int linker_append_elf_sym(struct bpf_linker *linker, struct src_obj *obj,
dst_sym->st_name = name_off;
dst_sym->st_info = sym->st_info;
dst_sym->st_other = sym->st_other;
- dst_sym->st_shndx = dst_sec ? dst_sec->sec_idx : sym->st_shndx;
+ if (sym_is_extern)
+ dst_sym->st_shndx = SHN_UNDEF;
+ else
+ dst_sym->st_shndx = dst_sec ? dst_sec->sec_idx : sym->st_shndx;
dst_sym->st_value = (src_sec ? src_sec->dst_off : 0) + sym->st_value;
dst_sym->st_size = sym->st_size;
@@ -2592,6 +2595,10 @@ static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj)
continue;
dst_var = &dst_sec->sec_vars[glob_sym->var_idx];
+ if (!glob_sym->is_extern) {
+ Elf64_Sym *sym = get_sym_by_idx(linker, glob_sym->sym_idx);
+ dst_var->offset = sym->st_value;
+ }
/* Because underlying BTF type might have
* changed, so might its size have changed, so
* re-calculate and update it in sec_var.
--
2.53.0-Meta
next reply other threads:[~2026-09-25 22:46 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-25 22:46 Andrii Nakryiko [this message]
2026-09-25 22:46 ` [PATCH bpf-next 2/2] selftests/bpf: Add linked_externs test for externs in allocated sections Andrii Nakryiko
2026-09-25 22:58 ` sashiko-bot
2026-09-25 23:32 ` Andrii Nakryiko
2026-09-25 23:35 ` bot+bpf-ci
2026-09-25 23:35 ` [PATCH bpf-next 1/2] libbpf: Fix static linking of externs placed " bot+bpf-ci
2026-09-25 23:46 ` Andrii Nakryiko
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=20260925224631.1490488-1-andrii@kernel.org \
--to=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=kernel-team@meta.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.