diff options
author | Stanislav Fomichev <sdf@google.com> | 2018-11-21 12:29:44 -0500 |
---|---|---|
committer | Daniel Borkmann <daniel@iogearbox.net> | 2018-11-21 17:15:41 -0500 |
commit | 8c4905b995c649ac71e21611abc2fcefc904b56a (patch) | |
tree | 8f81fc53ebbdf9a0c08cd0b6b9b16d5a19d33655 | |
parent | 462c124c590fe633564192dbfa26e99af788a67c (diff) |
libbpf: make sure bpf headers are c++ include-able
Wrap headers in extern "C", to turn off C++ mangling.
This simplifies including libbpf in c++ and linking against it.
v2 changes:
* do the same for btf.h
v3 changes:
* test_libbpf.cpp to test for possible future c++ breakages
Signed-off-by: Stanislav Fomichev <sdf@google.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
-rw-r--r-- | tools/lib/bpf/Makefile | 15 | ||||
-rw-r--r-- | tools/lib/bpf/bpf.h | 9 | ||||
-rw-r--r-- | tools/lib/bpf/btf.h | 8 | ||||
-rw-r--r-- | tools/lib/bpf/libbpf.h | 9 | ||||
-rw-r--r-- | tools/lib/bpf/test_libbpf.cpp | 18 |
5 files changed, 56 insertions, 3 deletions
diff --git a/tools/lib/bpf/Makefile b/tools/lib/bpf/Makefile index 425b480bda75..1b4a683a00fc 100644 --- a/tools/lib/bpf/Makefile +++ b/tools/lib/bpf/Makefile | |||
@@ -66,7 +66,7 @@ ifndef VERBOSE | |||
66 | endif | 66 | endif |
67 | 67 | ||
68 | FEATURE_USER = .libbpf | 68 | FEATURE_USER = .libbpf |
69 | FEATURE_TESTS = libelf libelf-mmap bpf reallocarray | 69 | FEATURE_TESTS = libelf libelf-mmap bpf reallocarray cxx |
70 | FEATURE_DISPLAY = libelf bpf | 70 | FEATURE_DISPLAY = libelf bpf |
71 | 71 | ||
72 | INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi | 72 | INCLUDES = -I. -I$(srctree)/tools/include -I$(srctree)/tools/arch/$(ARCH)/include/uapi -I$(srctree)/tools/include/uapi |
@@ -148,6 +148,12 @@ LIB_FILE := $(addprefix $(OUTPUT),$(LIB_FILE)) | |||
148 | 148 | ||
149 | CMD_TARGETS = $(LIB_FILE) | 149 | CMD_TARGETS = $(LIB_FILE) |
150 | 150 | ||
151 | CXX_TEST_TARGET = $(OUTPUT)test_libbpf | ||
152 | |||
153 | ifeq ($(feature-cxx), 1) | ||
154 | CMD_TARGETS += $(CXX_TEST_TARGET) | ||
155 | endif | ||
156 | |||
151 | TARGETS = $(CMD_TARGETS) | 157 | TARGETS = $(CMD_TARGETS) |
152 | 158 | ||
153 | all: fixdep all_cmd | 159 | all: fixdep all_cmd |
@@ -175,6 +181,9 @@ $(OUTPUT)libbpf.so: $(BPF_IN) | |||
175 | $(OUTPUT)libbpf.a: $(BPF_IN) | 181 | $(OUTPUT)libbpf.a: $(BPF_IN) |
176 | $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^ | 182 | $(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^ |
177 | 183 | ||
184 | $(OUTPUT)test_libbpf: test_libbpf.cpp $(OUTPUT)libbpf.a | ||
185 | $(QUIET_LINK)$(CXX) $^ -lelf -o $@ | ||
186 | |||
178 | define do_install | 187 | define do_install |
179 | if [ ! -d '$(DESTDIR_SQ)$2' ]; then \ | 188 | if [ ! -d '$(DESTDIR_SQ)$2' ]; then \ |
180 | $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \ | 189 | $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2'; \ |
@@ -201,8 +210,8 @@ config-clean: | |||
201 | $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null | 210 | $(Q)$(MAKE) -C $(srctree)/tools/build/feature/ clean >/dev/null |
202 | 211 | ||
203 | clean: | 212 | clean: |
204 | $(call QUIET_CLEAN, libbpf) $(RM) *.o *~ $(TARGETS) *.a *.so .*.d .*.cmd \ | 213 | $(call QUIET_CLEAN, libbpf) $(RM) $(TARGETS) $(CXX_TEST_TARGET) \ |
205 | $(RM) LIBBPF-CFLAGS | 214 | *.o *~ *.a *.so .*.d .*.cmd LIBBPF-CFLAGS |
206 | $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf | 215 | $(call QUIET_CLEAN, core-gen) $(RM) $(OUTPUT)FEATURE-DUMP.libbpf |
207 | 216 | ||
208 | 217 | ||
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 8bdfd806253a..09e8bbe111d4 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h | |||
@@ -27,6 +27,10 @@ | |||
27 | #include <stdbool.h> | 27 | #include <stdbool.h> |
28 | #include <stddef.h> | 28 | #include <stddef.h> |
29 | 29 | ||
30 | #ifdef __cplusplus | ||
31 | extern "C" { | ||
32 | #endif | ||
33 | |||
30 | #ifndef LIBBPF_API | 34 | #ifndef LIBBPF_API |
31 | #define LIBBPF_API __attribute__((visibility("default"))) | 35 | #define LIBBPF_API __attribute__((visibility("default"))) |
32 | #endif | 36 | #endif |
@@ -132,4 +136,9 @@ LIBBPF_API int bpf_load_btf(void *btf, __u32 btf_size, char *log_buf, | |||
132 | LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, | 136 | LIBBPF_API int bpf_task_fd_query(int pid, int fd, __u32 flags, char *buf, |
133 | __u32 *buf_len, __u32 *prog_id, __u32 *fd_type, | 137 | __u32 *buf_len, __u32 *prog_id, __u32 *fd_type, |
134 | __u64 *probe_offset, __u64 *probe_addr); | 138 | __u64 *probe_offset, __u64 *probe_addr); |
139 | |||
140 | #ifdef __cplusplus | ||
141 | } /* extern "C" */ | ||
142 | #endif | ||
143 | |||
135 | #endif /* __LIBBPF_BPF_H */ | 144 | #endif /* __LIBBPF_BPF_H */ |
diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h index 386b2ffc32a3..701ad2b6c41f 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h | |||
@@ -6,6 +6,10 @@ | |||
6 | 6 | ||
7 | #include <linux/types.h> | 7 | #include <linux/types.h> |
8 | 8 | ||
9 | #ifdef __cplusplus | ||
10 | extern "C" { | ||
11 | #endif | ||
12 | |||
9 | #ifndef LIBBPF_API | 13 | #ifndef LIBBPF_API |
10 | #define LIBBPF_API __attribute__((visibility("default"))) | 14 | #define LIBBPF_API __attribute__((visibility("default"))) |
11 | #endif | 15 | #endif |
@@ -80,4 +84,8 @@ int btf_ext__reloc(struct btf *btf, struct btf_ext *btf_ext, | |||
80 | const char *sec_name, __u32 insns_cnt, void **func_info, | 84 | const char *sec_name, __u32 insns_cnt, void **func_info, |
81 | __u32 *func_info_len); | 85 | __u32 *func_info_len); |
82 | 86 | ||
87 | #ifdef __cplusplus | ||
88 | } /* extern "C" */ | ||
89 | #endif | ||
90 | |||
83 | #endif /* __LIBBPF_BTF_H */ | 91 | #endif /* __LIBBPF_BTF_H */ |
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index b1686a787102..74e57e041705 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h | |||
@@ -16,6 +16,10 @@ | |||
16 | #include <sys/types.h> // for size_t | 16 | #include <sys/types.h> // for size_t |
17 | #include <linux/bpf.h> | 17 | #include <linux/bpf.h> |
18 | 18 | ||
19 | #ifdef __cplusplus | ||
20 | extern "C" { | ||
21 | #endif | ||
22 | |||
19 | #ifndef LIBBPF_API | 23 | #ifndef LIBBPF_API |
20 | #define LIBBPF_API __attribute__((visibility("default"))) | 24 | #define LIBBPF_API __attribute__((visibility("default"))) |
21 | #endif | 25 | #endif |
@@ -335,4 +339,9 @@ int libbpf_nl_get_qdisc(int sock, unsigned int nl_pid, int ifindex, | |||
335 | libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie); | 339 | libbpf_dump_nlmsg_t dump_qdisc_nlmsg, void *cookie); |
336 | int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle, | 340 | int libbpf_nl_get_filter(int sock, unsigned int nl_pid, int ifindex, int handle, |
337 | libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie); | 341 | libbpf_dump_nlmsg_t dump_filter_nlmsg, void *cookie); |
342 | |||
343 | #ifdef __cplusplus | ||
344 | } /* extern "C" */ | ||
345 | #endif | ||
346 | |||
338 | #endif /* __LIBBPF_LIBBPF_H */ | 347 | #endif /* __LIBBPF_LIBBPF_H */ |
diff --git a/tools/lib/bpf/test_libbpf.cpp b/tools/lib/bpf/test_libbpf.cpp new file mode 100644 index 000000000000..abf3fc25c9fa --- /dev/null +++ b/tools/lib/bpf/test_libbpf.cpp | |||
@@ -0,0 +1,18 @@ | |||
1 | /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ | ||
2 | #include "libbpf.h" | ||
3 | #include "bpf.h" | ||
4 | #include "btf.h" | ||
5 | |||
6 | /* do nothing, just make sure we can link successfully */ | ||
7 | |||
8 | int main(int argc, char *argv[]) | ||
9 | { | ||
10 | /* libbpf.h */ | ||
11 | libbpf_set_print(NULL, NULL, NULL); | ||
12 | |||
13 | /* bpf.h */ | ||
14 | bpf_prog_get_fd_by_id(0); | ||
15 | |||
16 | /* btf.h */ | ||
17 | btf__new(NULL, 0, NULL); | ||
18 | } | ||