Thread (18 messages) 18 messages, 4 authors, 2021-03-13

[PATCH bpf-next 02/10] libbpf: add internal helper to get raw BTF strings section

flat view
STALE2054d SUPERSEDED

From: Andrii Nakryiko <andrii@kernel.org>
Date: 2021-03-10 04:05:34
Also in: bpf
Subsystem: bpf [general] (safe dynamic programs and tools), bpf [library] (libbpf), the rest · Maintainers: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi, Linus Torvalds

Status: Superseded by v4 (posted 2021-03-18). [go to v4]

Revision v1 (4 revisions archived).

Revisions (4)
  1. v1 current
  2. v2 [diff against previous]
  3. v3 [diff against previous]
  4. v4 [diff against previous]
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@kernel.org>
---
 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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help