libbpf: Add BPF_EMBED_OBJ macro for embedding BPF .o files
authorAndrii Nakryiko <andriin@fb.com>
Sat, 14 Dec 2019 01:43:28 +0000 (17:43 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Sun, 15 Dec 2019 23:58:04 +0000 (15:58 -0800)
Add a convenience macro BPF_EMBED_OBJ, which allows to embed other files
(typically used to embed BPF .o files) into a hosting userspace programs. To
C program it is exposed as struct bpf_embed_data, containing a pointer to
raw data and its size in bytes.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Acked-by: Martin KaFai Lau <kafai@fb.com>
Link: https://lore.kernel.org/bpf/20191214014341.3442258-5-andriin@fb.com
tools/lib/bpf/libbpf.h
tools/testing/selftests/bpf/prog_tests/attach_probe.c

index 2698fbcb0c798a664aed92afef7f2f00f7c5d15a..fa803dde1f4609fd60ce901857f7a100e2d1b5b9 100644 (file)
@@ -615,6 +615,41 @@ bpf_program__bpil_offs_to_addr(struct bpf_prog_info_linear *info_linear);
  */
 LIBBPF_API int libbpf_num_possible_cpus(void);
 
+struct bpf_embed_data {
+       void *data;
+       size_t size;
+};
+
+#define BPF_EMBED_OBJ_DECLARE(NAME)                                    \
+extern struct bpf_embed_data NAME##_embed;                             \
+extern char NAME##_data[];                                             \
+extern char NAME##_data_end[];
+
+#define __BPF_EMBED_OBJ(NAME, PATH, SZ, ASM_TYPE)                      \
+asm (                                                                  \
+"      .pushsection \".rodata\", \"a\", @progbits              \n"     \
+"      .global "#NAME"_data                                    \n"     \
+#NAME"_data:                                                   \n"     \
+"      .incbin \"" PATH "\"                                    \n"     \
+"      .global "#NAME"_data_end                                \n"     \
+#NAME"_data_end:                                               \n"     \
+"      .global "#NAME"_embed                                   \n"     \
+"      .type "#NAME"_embed, @object                            \n"     \
+"      .size "#NAME"_size, "#SZ"                               \n"     \
+"      .align 8,                                               \n"     \
+#NAME"_embed:                                                  \n"     \
+"      "ASM_TYPE" "#NAME"_data                                 \n"     \
+"      "ASM_TYPE" "#NAME"_data_end - "#NAME"_data              \n"     \
+"      .popsection                                             \n"     \
+);                                                                     \
+BPF_EMBED_OBJ_DECLARE(NAME)
+
+#if __SIZEOF_POINTER__ == 4
+#define BPF_EMBED_OBJ(NAME, PATH) __BPF_EMBED_OBJ(NAME, PATH, 8, ".long")
+#else
+#define BPF_EMBED_OBJ(NAME, PATH) __BPF_EMBED_OBJ(NAME, PATH, 16, ".quad")
+#endif
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
index a83111a32d4a5758c746015310de55247f97ed15..b2e7c1424b07f92a99a49fa87543315ad639ce6b 100644 (file)
@@ -1,24 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0
 #include <test_progs.h>
 
-#define EMBED_FILE(NAME, PATH)                                             \
-asm (                                                                      \
-"      .pushsection \".rodata\", \"a\", @progbits              \n"         \
-"      .global "#NAME"_data                                    \n"         \
-#NAME"_data:                                                   \n"         \
-"      .incbin \"" PATH "\"                                    \n"         \
-#NAME"_data_end:                                               \n"         \
-"      .global "#NAME"_size                                    \n"         \
-"      .type "#NAME"_size, @object                             \n"         \
-"      .size "#NAME"_size, 4                                   \n"         \
-"      .align 4,                                               \n"         \
-#NAME"_size:                                                   \n"         \
-"      .int "#NAME"_data_end - "#NAME"_data                    \n"         \
-"      .popsection                                             \n"         \
-);                                                                         \
-extern char NAME##_data[];                                                 \
-extern int NAME##_size;
-
 ssize_t get_base_addr() {
        size_t start;
        char buf[256];
@@ -39,7 +21,7 @@ ssize_t get_base_addr() {
        return -EINVAL;
 }
 
-EMBED_FILE(probe, "test_attach_probe.o");
+BPF_EMBED_OBJ(probe, "test_attach_probe.o");
 
 void test_attach_probe(void)
 {
@@ -73,7 +55,8 @@ void test_attach_probe(void)
        uprobe_offset = (size_t)&get_base_addr - base_addr;
 
        /* open object */
-       obj = bpf_object__open_mem(probe_data, probe_size, &open_opts);
+       obj = bpf_object__open_mem(probe_embed.data, probe_embed.size,
+                                  &open_opts);
        if (CHECK(IS_ERR(obj), "obj_open_mem", "err %ld\n", PTR_ERR(obj)))
                return;