Re: [PATCH bpf-next] libbpf: Per-symbol visibility for DSO

2018-10-16 Thread Alexei Starovoitov
On Mon, Oct 15, 2018 at 10:50:34PM -0700, Andrey Ignatov wrote:
> Make global symbols in libbpf DSO hidden by default with
> -fvisibility=hidden and export symbols that are part of ABI explicitly
> with __attribute__((visibility("default"))).
> 
> This is common practice that should prevent from accidentally exporting
> a symbol, that is not supposed to be a part of ABI what, in turn,
> improves both libbpf developer- and user-experiences. See [1] for more
> details.
> 
> Export control becomes more important since more and more projects use
> libbpf.
> 
> The patch doesn't export a bunch of netlink related functions since as
> agreed in [2] they'll be reworked. That doesn't break bpftool since
> bpftool links libbpf statically.
> 
> [1] https://www.akkadia.org/drepper/dsohowto.pdf (2.2 Export Control)
> [2] https://www.mail-archive.com/netdev@vger.kernel.org/msg251434.html
> 
> Signed-off-by: Andrey Ignatov 

Applied, Thanks



[PATCH bpf-next] libbpf: Per-symbol visibility for DSO

2018-10-15 Thread Andrey Ignatov
Make global symbols in libbpf DSO hidden by default with
-fvisibility=hidden and export symbols that are part of ABI explicitly
with __attribute__((visibility("default"))).

This is common practice that should prevent from accidentally exporting
a symbol, that is not supposed to be a part of ABI what, in turn,
improves both libbpf developer- and user-experiences. See [1] for more
details.

Export control becomes more important since more and more projects use
libbpf.

The patch doesn't export a bunch of netlink related functions since as
agreed in [2] they'll be reworked. That doesn't break bpftool since
bpftool links libbpf statically.

[1] https://www.akkadia.org/drepper/dsohowto.pdf (2.2 Export Control)
[2] https://www.mail-archive.com/netdev@vger.kernel.org/msg251434.html

Signed-off-by: Andrey Ignatov 
---
 tools/lib/bpf/Makefile |   1 +
 tools/lib/bpf/bpf.h| 118 ++
 tools/lib/bpf/btf.h|  22 +++--
 tools/lib/bpf/libbpf.h | 186 ++---
 4 files changed, 179 insertions(+), 148 deletions(-)

diff --git a/tools/lib/bpf/Makefile b/tools/lib/bpf/Makefile
index 79d84413ddf2..425b480bda75 100644
--- a/tools/lib/bpf/Makefile
+++ b/tools/lib/bpf/Makefile
@@ -125,6 +125,7 @@ override CFLAGS += $(EXTRA_WARNINGS)
 override CFLAGS += -Werror -Wall
 override CFLAGS += -fPIC
 override CFLAGS += $(INCLUDES)
+override CFLAGS += -fvisibility=hidden
 
 ifeq ($(VERBOSE),1)
   Q =
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 69a4d40c4227..258c3c178333 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -27,6 +27,10 @@
 #include 
 #include 
 
+#ifndef LIBBPF_API
+#define LIBBPF_API __attribute__((visibility("default")))
+#endif
+
 struct bpf_create_map_attr {
const char *name;
enum bpf_map_type map_type;
@@ -42,21 +46,24 @@ struct bpf_create_map_attr {
__u32 inner_map_fd;
 };
 
-int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr);
-int bpf_create_map_node(enum bpf_map_type map_type, const char *name,
-   int key_size, int value_size, int max_entries,
-   __u32 map_flags, int node);
-int bpf_create_map_name(enum bpf_map_type map_type, const char *name,
-   int key_size, int value_size, int max_entries,
-   __u32 map_flags);
-int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size,
-  int max_entries, __u32 map_flags);
-int bpf_create_map_in_map_node(enum bpf_map_type map_type, const char *name,
-  int key_size, int inner_map_fd, int max_entries,
-  __u32 map_flags, int node);
-int bpf_create_map_in_map(enum bpf_map_type map_type, const char *name,
- int key_size, int inner_map_fd, int max_entries,
- __u32 map_flags);
+LIBBPF_API int
+bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr);
+LIBBPF_API int bpf_create_map_node(enum bpf_map_type map_type, const char 
*name,
+  int key_size, int value_size,
+  int max_entries, __u32 map_flags, int node);
+LIBBPF_API int bpf_create_map_name(enum bpf_map_type map_type, const char 
*name,
+  int key_size, int value_size,
+  int max_entries, __u32 map_flags);
+LIBBPF_API int bpf_create_map(enum bpf_map_type map_type, int key_size,
+ int value_size, int max_entries, __u32 map_flags);
+LIBBPF_API int bpf_create_map_in_map_node(enum bpf_map_type map_type,
+ const char *name, int key_size,
+ int inner_map_fd, int max_entries,
+ __u32 map_flags, int node);
+LIBBPF_API int bpf_create_map_in_map(enum bpf_map_type map_type,
+const char *name, int key_size,
+int inner_map_fd, int max_entries,
+__u32 map_flags);
 
 struct bpf_load_program_attr {
enum bpf_prog_type prog_type;
@@ -74,44 +81,49 @@ struct bpf_load_program_attr {
 
 /* Recommend log buffer size */
 #define BPF_LOG_BUF_SIZE (256 * 1024)
-int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
-  char *log_buf, size_t log_buf_sz);
-int bpf_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
-size_t insns_cnt, const char *license,
-__u32 kern_version, char *log_buf,
-size_t log_buf_sz);
-int bpf_verify_program(enum bpf_prog_type type, const struct bpf_insn *insns,
-  size_t insns_cnt, int strict_alignment,
-  const char *license, __u32 kern_version,
-  char *log_buf, size_t log_buf_sz, int log_level);
+LIBBPF_API