struct btf is an efficient and convenient data structure to be used as a set of deduplicated strings. This patch adds libbpf-internal btf_raw_str() helper that gives access to strings section raw data (regardless of whether BTF object is read-only or writeable) and its size in bytes. This is going to be used by bpf_linker to implement ELF string table section. Signed-off-by: Andrii Nakryiko <andrii@xxxxxxxxxx> --- tools/lib/bpf/btf.c | 11 +++++++++++ tools/lib/bpf/libbpf_internal.h | 1 + 2 files changed, 12 insertions(+) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index e0b0a78b04fe..6ee82ffcf3ff 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -1296,6 +1296,17 @@ static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endi return NULL; } +/* + * Internal helper to get the size and direct pointer to strings section. + * This is used in cases where struct btf is used as an efficient and + * convenient strings container (e.g., bpf_linker). + */ +const void *btf_raw_strs(const struct btf *btf, size_t *size) +{ + *size = btf->hdr->str_len; + return btf->strs_data; +} + const void *btf__get_raw_data(const struct btf *btf_ro, __u32 *size) { struct btf *btf = (struct btf *)btf_ro; diff --git a/tools/lib/bpf/libbpf_internal.h b/tools/lib/bpf/libbpf_internal.h index d09860e435c8..069250e8e871 100644 --- a/tools/lib/bpf/libbpf_internal.h +++ b/tools/lib/bpf/libbpf_internal.h @@ -115,6 +115,7 @@ struct btf_type *btf_type_by_id(struct btf *btf, __u32 type_id); void *btf_add_mem(void **data, size_t *cap_cnt, size_t elem_sz, size_t cur_cnt, size_t max_cnt, size_t add_cnt); int btf_ensure_mem(void **data, size_t *cap_cnt, size_t elem_sz, size_t need_cnt); +const void *btf_raw_strs(const struct btf *btf, size_t *size); static inline bool libbpf_validate_opts(const char *opts, size_t opts_sz, size_t user_sz, -- 2.24.1