Skip to content

Commit 19e00c8

Browse files
alan-maguireanakryiko
authored andcommitted
libbpf: Split BTF relocation
Map distilled base BTF type ids referenced in split BTF and their references to the base BTF passed in, and if the mapping succeeds, reparent the split BTF to the base BTF. Relocation is done by first verifying that distilled base BTF only consists of named INT, FLOAT, ENUM, FWD, STRUCT and UNION kinds; then we sort these to speed lookups. Once sorted, the base BTF is iterated, and for each relevant kind we check for an equivalent in distilled base BTF. When found, the mapping from distilled -> base BTF id and string offset is recorded. In establishing mappings, we need to ensure we check STRUCT/UNION size when the STRUCT/UNION is embedded in a split BTF STRUCT/UNION, and when duplicate names exist for the same STRUCT/UNION. Otherwise size is ignored in matching STRUCT/UNIONs. Once all mappings are established, we can update type ids and string offsets in split BTF and reparent it to the new base. Signed-off-by: Alan Maguire <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent eb20e72 commit 19e00c8

File tree

6 files changed

+542
-1
lines changed

6 files changed

+542
-1
lines changed

tools/lib/bpf/Build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
libbpf-y := libbpf.o bpf.o nlattr.o btf.o libbpf_errno.o str_error.o \
22
netlink.o bpf_prog_linfo.o libbpf_probes.o hashmap.o \
33
btf_dump.o ringbuf.o strset.o linker.o gen_loader.o relo_core.o \
4-
usdt.o zip.o elf.o features.o
4+
usdt.o zip.o elf.o features.o btf_relocate.o

tools/lib/bpf/btf.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5583,3 +5583,20 @@ int btf__distill_base(const struct btf *src_btf, struct btf **new_base_btf,
55835583

55845584
return 0;
55855585
}
5586+
5587+
const struct btf_header *btf_header(const struct btf *btf)
5588+
{
5589+
return btf->hdr;
5590+
}
5591+
5592+
void btf_set_base_btf(struct btf *btf, const struct btf *base_btf)
5593+
{
5594+
btf->base_btf = (struct btf *)base_btf;
5595+
btf->start_id = btf__type_cnt(base_btf);
5596+
btf->start_str_off = base_btf->hdr->str_len;
5597+
}
5598+
5599+
int btf__relocate(struct btf *btf, const struct btf *base_btf)
5600+
{
5601+
return libbpf_err(btf_relocate(btf, base_btf, NULL));
5602+
}

tools/lib/bpf/btf.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,20 @@ struct btf_dedup_opts {
252252

253253
LIBBPF_API int btf__dedup(struct btf *btf, const struct btf_dedup_opts *opts);
254254

255+
/**
256+
* @brief **btf__relocate()** will check the split BTF *btf* for references
257+
* to base BTF kinds, and verify those references are compatible with
258+
* *base_btf*; if they are, *btf* is adjusted such that is re-parented to
259+
* *base_btf* and type ids and strings are adjusted to accommodate this.
260+
*
261+
* If successful, 0 is returned and **btf** now has **base_btf** as its
262+
* base.
263+
*
264+
* A negative value is returned on error and the thread-local `errno` variable
265+
* is set to the error code as well.
266+
*/
267+
LIBBPF_API int btf__relocate(struct btf *btf, const struct btf *base_btf);
268+
255269
struct btf_dump;
256270

257271
struct btf_dump_opts {

0 commit comments

Comments
 (0)