Skip to contentSkip to conversation

libbpf: fix cross-endian build with newer libbpf

PATCH Applied Dominique Martinet 4 participants 5 messages Nov 16 – Nov 23

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 libbpf: fix cross-endian build with newer libbpf

commit 087f3964f4b3 ("libbpf: Support layout section handling in BTF") added extra data to the BTF header.

This extra data is ignored in the native case, but when building with different endianness the bswap code in old libbpf would check for strict size equality, leading to build failures such as this when BTF data is generated with a newer version of libbpf (through pahole)
BTFIDS vmlinux.unstripped
libbpf: Can't load BTF with non-native endianness due to unsupported header length 32
FAILED: load BTF from vmlinux.unstripped: Operation not supported make[2]: *** [../scripts/Makefile.vmlinux:72: vmlinux.unstripped] Error 255

Newer libbpf versions also properly ignore trailing data in the bswap case, but the patch is too intrusive to backport, so take the header length comparison fix from it instead.

Fixes 3289959b97ca ("libbpf: Support BTF loading and raw data output in both endianness")

---
v1 -> v2:
- link to v1: https://lore.kernel.org/r/[email protected]
- use offsetofend() instead of sizeof() to match the support layout section handling patch as suggested by Alan
I checked this builds on 5.10 (non-cross-endian build) and 6.18 (ppc64 cross build)
- add Fixes tag as suggested by sashiko

 tools/lib/bpf/btf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/btf.c +2 -2
index 9f141395c074..0ab11be8ebc5 100644
@@ -229,14 +229,14 @@ static int btf_parse_hdr(struct btf *btf)
 	struct btf_header *hdr = btf->hdr;
 	__u32 meta_left;
 
-	if (btf->raw_size < sizeof(struct btf_header)) {
+	if (btf->raw_size < offsetofend(struct btf_header, str_len)) {
 		pr_debug("BTF header not found\n");
 		return -EINVAL;
 	}
 
 	if (hdr->magic == bswap_16(BTF_MAGIC)) {
 		btf->swapped_endian = true;
-		if (bswap_32(hdr->hdr_len) != sizeof(struct btf_header)) {
+		if (bswap_32(hdr->hdr_len) < offsetofend(struct btf_header, str_len)) {
 			pr_warn("Can't load BTF with non-native endianness due to unsupported header length %u\n",
 				bswap_32(hdr->hdr_len));
 			return -ENOTSUP;
Signature · 1 line
2.56.1
maintainer
#1 lore raw
Quoted 56 lines · Dominique Martinet

commit 087f3964f4b3 ("libbpf: Support layout section handling in BTF") added extra data to the BTF header.

This extra data is ignored in the native case, but when building with different endianness the bswap code in old libbpf would check for strict size equality, leading to build failures such as this when BTF data is generated with a newer version of libbpf (through pahole)
BTFIDS vmlinux.unstripped
libbpf: Can't load BTF with non-native endianness due to unsupported header length 32
FAILED: load BTF from vmlinux.unstripped: Operation not supported make[2]: *** [../scripts/Makefile.vmlinux:72: vmlinux.unstripped] Error 255

Newer libbpf versions also properly ignore trailing data in the bswap case, but the patch is too intrusive to backport, so take the header length comparison fix from it instead.

Fixes 3289959b97ca ("libbpf: Support BTF loading and raw data output in both endianness")

---
v1 -> v2:
- link to v1: https://lore.kernel.org/r/[email protected]
- use offsetofend() instead of sizeof() to match the support layout section handling patch as suggested by Alan
I checked this builds on 5.10 (non-cross-endian build) and 6.18 (ppc64 cross build)
- add Fixes tag as suggested by sashiko

 tools/lib/bpf/btf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/btf.c +2 -2
index 9f141395c074..0ab11be8ebc5 100644
@@ -229,14 +229,14 @@ static int btf_parse_hdr(struct btf *btf)
 	struct btf_header *hdr = btf->hdr;
 	__u32 meta_left;
 
-	if (btf->raw_size < sizeof(struct btf_header)) {
+	if (btf->raw_size < offsetofend(struct btf_header, str_len)) {
 		pr_debug("BTF header not found\n");
 		return -EINVAL;
 	}
 
 	if (hdr->magic == bswap_16(BTF_MAGIC)) {
 		btf->swapped_endian = true;
-		if (bswap_32(hdr->hdr_len) != sizeof(struct btf_header)) {
+		if (bswap_32(hdr->hdr_len) < offsetofend(struct btf_header, str_len)) {
 			pr_warn("Can't load BTF with non-native endianness due to unsupported header length %u\n",
 				bswap_32(hdr->hdr_len));
 			return -ENOTSUP;
Signature · 1 line
2.56.1

<formletter>

This is not the correct way to submit patches for inclusion in the stable kernel tree. Please read:
https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.

</formletter>

maintainer Applied
#2 lore raw
In reply to Dominique Martinet
Quoted 3 lines

Newer libbpf versions also properly ignore trailing data in the bswap case, but the patch is too intrusive to backport, so take the header length comparison fix from it instead.

Queued for 6.18, 6.12, 6.6, 6.1, 5.15 and 5.10, thanks.

Signature · 2 lines
Thanks,
Sasha
#3 lore raw
In reply to Dominique Martinet
Quoted 12 lines · Dominique Martinet

commit 087f3964f4b3 ("libbpf: Support layout section handling in BTF") added extra data to the BTF header.

This extra data is ignored in the native case, but when building with different endianness the bswap code in old libbpf would check for strict size equality, leading to build failures such as this when BTF data is generated with a newer version of libbpf (through pahole)
BTFIDS vmlinux.unstripped
libbpf: Can't load BTF with non-native endianness due to unsupported header length 32
FAILED: load BTF from vmlinux.unstripped: Operation not supported make[2]: *** [../scripts/Makefile.vmlinux:72: vmlinux.unstripped] Error 255

Quoted 3 lines

Newer libbpf versions also properly ignore trailing data in the bswap case, but the patch is too intrusive to backport, so take the header length comparison fix from it instead.

Why not always just use the libbpf version from newer kernels?

And why not backport the full series, one-off are almost always more work over time.

thanks,

greg k-h

#4 lore raw

Greg KH wrote on Mon, Nov 23, 2026 at 08:01:33AM +0100:

Quoted 13 lines · Dominique Martinet
Quoted 12 lines

commit 087f3964f4b3 ("libbpf: Support layout section handling in BTF") added extra data to the BTF header.

This extra data is ignored in the native case, but when building with different endianness the bswap code in old libbpf would check for strict size equality, leading to build failures such as this when BTF data is generated with a newer version of libbpf (through pahole)
BTFIDS vmlinux.unstripped
libbpf: Can't load BTF with non-native endianness due to unsupported header length 32
FAILED: load BTF from vmlinux.unstripped: Operation not supported make[2]: *** [../scripts/Makefile.vmlinux:72: vmlinux.unstripped] Error 255

Quoted 5 lines
Quoted 3 lines

Newer libbpf versions also properly ignore trailing data in the bswap case, but the patch is too intrusive to backport, so take the header length comparison fix from it instead.

Why not always just use the libbpf version from newer kernels?

I'm a simple user as far as this part of the tree goes, but the build system uses two libbpf versions:
- indirectly, whatever was used to build pahole to generate BTF debuginfos
- directly, what is in the build tree to generate BTFIDs from the above BTF debuginfos (tools/bpf/resolve_btfids)

This build failure happens if pahole uses a newer libbpf version, that the libbpf version in the kernel isn't able to parse because of a too strict check in the cross-endian case.

Technically resolve_btfids could probably be built with the system libbpf instead of the one in the kernel tree but then we'd need to backport potential build failures instead if the libbpf API changes, I'm not sure it'd be less work in the long run.

Quoted 2 lines

And why not backport the full series, one-off are almost always more work over time.

Happy to send a revert of the above patch + full series backport if that's what you/libbpf maintainers prefer...
In this case the current code already supports newer libbpf versions and the bug was only in a corner case with cross-endian handling (e.g. I'm not expecting further fixes to be needed in that area), so it seemed simpler to just backport that part of the patch instead of backporting a "feature patch", but I'm not fussy.

I'd argue that the cross-endian fix should have been a separate commit instead of being part of a new feature commit in the first place, but hindsight 20/20 and it probably wasn't obvious that it'd cause build failures at the time...

Anyway, I stand by my opinion that minimal patch sounds better to me, but just say the word and I can look at backporting the full layout section handling code instead. Not sure I'd be able to get prereqs right for very old kernels but 5.10/5.15 are gone next month anyway...

Thanks,

Signature · 1 line
Dominique