mirror of
https://github.com/netdata/libbpf.git
synced 2026-11-08 10:50:15 +08:00
libbpf: Support sanitization of BTF layout for older kernels
Add a FEAT_BTF_LAYOUT feature check which checks if the kernel supports BTF layout information. Also sanitize BTF if it contains layout data but the kernel does not support it. The sanitization requires rewriting raw BTF data to update the header and eliminate the layout section (since it lies between the types and strings), so refactor sanitization to do the raw BTF retrieval and creation of updated BTF, returning that new BTF on success. Signed-off-by: Alan Maguire <alan.maguire@oracle.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20260326145444.2076244-7-alan.maguire@oracle.com
This commit is contained in:
1 parent
c95ad95d0a
commit
fd04b4eae1
4 files changed
+131
-40
No files matched your search
+29
-19
@@ -218,6 +218,34 @@ int libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts)
|
||||
return libbpf_err(ret);
|
||||
}
|
||||
|
||||
int libbpf__load_raw_btf_hdr(const struct btf_header *hdr, const char *raw_types,
|
||||
const char *str_sec, const char *layout_sec,
|
||||
int token_fd)
|
||||
{
|
||||
LIBBPF_OPTS(bpf_btf_load_opts, opts,
|
||||
.token_fd = token_fd,
|
||||
.btf_flags = token_fd ? BPF_F_TOKEN_FD : 0,
|
||||
);
|
||||
int btf_fd, btf_len;
|
||||
__u8 *raw_btf;
|
||||
|
||||
btf_len = hdr->hdr_len + hdr->type_off + hdr->type_len + hdr->str_len + hdr->layout_len;
|
||||
raw_btf = malloc(btf_len);
|
||||
if (!raw_btf)
|
||||
return -ENOMEM;
|
||||
|
||||
memcpy(raw_btf, hdr, sizeof(*hdr));
|
||||
memcpy(raw_btf + hdr->hdr_len + hdr->type_off, raw_types, hdr->type_len);
|
||||
memcpy(raw_btf + hdr->hdr_len + hdr->str_off, str_sec, hdr->str_len);
|
||||
if (layout_sec)
|
||||
memcpy(raw_btf + hdr->hdr_len + hdr->layout_off, layout_sec, hdr->layout_len);
|
||||
|
||||
btf_fd = bpf_btf_load(raw_btf, btf_len, &opts);
|
||||
|
||||
free(raw_btf);
|
||||
return btf_fd;
|
||||
}
|
||||
|
||||
int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
|
||||
const char *str_sec, size_t str_len,
|
||||
int token_fd)
|
||||
@@ -230,26 +258,8 @@ int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
|
||||
.str_off = types_len,
|
||||
.str_len = str_len,
|
||||
};
|
||||
LIBBPF_OPTS(bpf_btf_load_opts, opts,
|
||||
.token_fd = token_fd,
|
||||
.btf_flags = token_fd ? BPF_F_TOKEN_FD : 0,
|
||||
);
|
||||
int btf_fd, btf_len;
|
||||
__u8 *raw_btf;
|
||||
|
||||
btf_len = hdr.hdr_len + hdr.type_len + hdr.str_len;
|
||||
raw_btf = malloc(btf_len);
|
||||
if (!raw_btf)
|
||||
return -ENOMEM;
|
||||
|
||||
memcpy(raw_btf, &hdr, sizeof(hdr));
|
||||
memcpy(raw_btf + hdr.hdr_len, raw_types, hdr.type_len);
|
||||
memcpy(raw_btf + hdr.hdr_len + hdr.type_len, str_sec, hdr.str_len);
|
||||
|
||||
btf_fd = bpf_btf_load(raw_btf, btf_len, &opts);
|
||||
|
||||
free(raw_btf);
|
||||
return btf_fd;
|
||||
return libbpf__load_raw_btf_hdr(&hdr, raw_types, str_sec, NULL, token_fd);
|
||||
}
|
||||
|
||||
static int load_local_storage_btf(void)
|
||||
|
||||
Reference in new issue
Block a user