Re: [PATCH bpf-next] libbpf: Add bpf_object__rodata getter function
From: Andrii Nakryiko <hidden>
Date: 2020-03-26 19:21:01
Also in:
bpf
On Thu, Mar 26, 2020 at 8:18 AM Toke Høiland-Jørgensen [off-list ref] wrote:
quoted hunk ↗ jump to hunk
This adds a new getter function to libbpf to get the rodata area of a bpf object. This is useful if a program wants to modify the rodata before loading the object. Any such modification needs to be done before loading, since libbpf freezes the backing map after populating it (to allow the kernel to do dead code elimination based on its contents). Signed-off-by: Toke Høiland-Jørgensen <redacted> --- tools/lib/bpf/libbpf.c | 13 +++++++++++++ tools/lib/bpf/libbpf.h | 1 + tools/lib/bpf/libbpf.map | 1 + 3 files changed, 15 insertions(+)diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 085e41f9b68e..d3e3bbe12f78 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c@@ -1352,6 +1352,19 @@ bpf_object__init_internal_map(struct bpf_object *obj, enum libbpf_map_type type, return 0; } +void *bpf_object__rodata(const struct bpf_object *obj, size_t *size)
We probably don't want to expose this API. It just doesn't scale, especially if/when we add support for custom sections names for global variables. Also checking for map->mmaped is too restrictive. See how BPF skeleton solves this problem and still allows .rodata initialization even on kernels that don't support memory-mapping global variables. But basically, why can't you use BPF skeleton? Also, application can already find that map by looking at name.
quoted hunk ↗ jump to hunk
+{ + struct bpf_map *map; + + bpf_object__for_each_map(map, obj) { + if (map->libbpf_type == LIBBPF_MAP_RODATA && map->mmaped) { + *size = map->def.value_size; + return map->mmaped; + } + } + return NULL; +} + static int bpf_object__init_global_data_maps(struct bpf_object *obj) { int err;diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index d38d7a629417..d2a9beed7b8a 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h@@ -166,6 +166,7 @@ typedef void (*bpf_object_clear_priv_t)(struct bpf_object *, void *); LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv, bpf_object_clear_priv_t clear_priv); LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog); +LIBBPF_API void *bpf_object__rodata(const struct bpf_object *obj, size_t *size); LIBBPF_API int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 5129283c0284..a248f4ff3a40 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map@@ -243,5 +243,6 @@ LIBBPF_0.0.8 { bpf_link__pin; bpf_link__pin_path; bpf_link__unpin; + bpf_object__rodata; bpf_program__set_attach_target; } LIBBPF_0.0.7; --2.26.0