Skip to content

Commit 9af44bc

Browse files
anakryikoAlexei Starovoitov
authored andcommitted
libbpf: Add generic BTF type shallow copy API
Add btf__add_type() API that performs shallow copy of a given BTF type from the source BTF into the destination BTF. All the information and type IDs are preserved, but all the strings encountered are added into the destination BTF and corresponding offsets are rewritten. BTF type IDs are assumed to be correct or such that will be (somehow) modified afterwards. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20210318194036.3521577-6-andrii@kernel.org
1 parent 90d76d3 commit 9af44bc

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

tools/lib/bpf/btf.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,54 @@ static int btf_commit_type(struct btf *btf, int data_sz)
16291629
return btf->start_id + btf->nr_types - 1;
16301630
}
16311631

1632+
struct btf_pipe {
1633+
const struct btf *src;
1634+
struct btf *dst;
1635+
};
1636+
1637+
static int btf_rewrite_str(__u32 *str_off, void *ctx)
1638+
{
1639+
struct btf_pipe *p = ctx;
1640+
int off;
1641+
1642+
if (!*str_off) /* nothing to do for empty strings */
1643+
return 0;
1644+
1645+
off = btf__add_str(p->dst, btf__str_by_offset(p->src, *str_off));
1646+
if (off < 0)
1647+
return off;
1648+
1649+
*str_off = off;
1650+
return 0;
1651+
}
1652+
1653+
int btf__add_type(struct btf *btf, const struct btf *src_btf, const struct btf_type *src_type)
1654+
{
1655+
struct btf_pipe p = { .src = src_btf, .dst = btf };
1656+
struct btf_type *t;
1657+
int sz, err;
1658+
1659+
sz = btf_type_size(src_type);
1660+
if (sz < 0)
1661+
return sz;
1662+
1663+
/* deconstruct BTF, if necessary, and invalidate raw_data */
1664+
if (btf_ensure_modifiable(btf))
1665+
return -ENOMEM;
1666+
1667+
t = btf_add_type_mem(btf, sz);
1668+
if (!t)
1669+
return -ENOMEM;
1670+
1671+
memcpy(t, src_type, sz);
1672+
1673+
err = btf_type_visit_str_offs(t, btf_rewrite_str, &p);
1674+
if (err)
1675+
return err;
1676+
1677+
return btf_commit_type(btf, sz);
1678+
}
1679+
16321680
/*
16331681
* Append new BTF_KIND_INT type with:
16341682
* - *name* - non-empty, non-NULL type name;

tools/lib/bpf/btf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ LIBBPF_API struct btf *libbpf_find_kernel_btf(void);
9393

9494
LIBBPF_API int btf__find_str(struct btf *btf, const char *s);
9595
LIBBPF_API int btf__add_str(struct btf *btf, const char *s);
96+
LIBBPF_API int btf__add_type(struct btf *btf, const struct btf *src_btf,
97+
const struct btf_type *src_type);
9698

9799
LIBBPF_API int btf__add_int(struct btf *btf, const char *name, size_t byte_sz, int encoding);
98100
LIBBPF_API int btf__add_float(struct btf *btf, const char *name, size_t byte_sz);

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,4 +354,5 @@ LIBBPF_0.3.0 {
354354
LIBBPF_0.4.0 {
355355
global:
356356
btf__add_float;
357+
btf__add_type;
357358
} LIBBPF_0.3.0;

0 commit comments

Comments
 (0)