[PATCH bpf 1/2] libbpf: Fix stale raw data after btf__relocate()

From: Jiayuan Chen

Date: Sun Nov 01 2026 - 10:57:01 EST


btf_relocate() rewrites type ids in place but doesn't drop the cached
raw_data, so btf__raw_data() after btf__relocate() returns the old
bytes with pre-relocation ids.

Call btf_ensure_modifiable() first, like other BTF mutators do, so the
cache is invalidated.

Fixes: 19e00c897d50 ("libbpf: Split BTF relocation")
Reported-by: 洛 <wanghai8081@xxxxxxxxx>
Closes: https://lore.kernel.org/bpf/455f7257-25b9-4070-8313-42af8005b948@xxxxxxxxx/T/#t
Signed-off-by: Jiayuan Chen <jiayuan.chen@xxxxxxxxx>
---
tools/lib/bpf/btf.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index d2ff6f5d6d5b..b55dce9fa003 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -6640,8 +6640,13 @@ void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)

int btf__relocate(struct btf *btf, const struct btf *base_btf)
{
- int err = btf_relocate(btf, base_btf, NULL, NULL);
+ int err;
+
+ err = btf_ensure_modifiable(btf);
+ if (err)
+ return libbpf_err(err);

+ err = btf_relocate(btf, base_btf, NULL, NULL);
if (!err)
btf->owns_base = false;
return libbpf_err(err);
--
2.43.0