Avoid unnecessary symbol exposure with ELF-based systems

GCC's default visibility is "public" but can be changed to
"hidden" with the argument "-fvisibility=hidden".

Tested with x86_64 / Ubuntu Linux 18.04 LTS:

[before]
$ nm -g -C libmimalloc.so | grep "T " | wc -l
142

[after]
$ nm -g -C libmimalloc.so | grep "T " | wc -l
93

This patch does not change the build on macOS.
This commit is contained in:
Jim Huang 2019-06-23 23:04:43 +08:00 committed by Jim Huang
parent 91cb4cee8a
commit 256eb97849
2 changed files with 10 additions and 1 deletions

View File

@ -82,6 +82,7 @@ if(CMAKE_C_COMPILER_ID MATCHES "AppleClang|Clang|GNU")
list(APPEND mi_cflags -Wall -Wextra -Wno-unknown-pragmas -ftls-model=initial-exec) list(APPEND mi_cflags -Wall -Wextra -Wno-unknown-pragmas -ftls-model=initial-exec)
if(CMAKE_C_COMPILER_ID MATCHES "GNU") if(CMAKE_C_COMPILER_ID MATCHES "GNU")
list(APPEND mi_cflags -Wno-invalid-memory-model) list(APPEND mi_cflags -Wno-invalid-memory-model)
list(APPEND mi_cflags -fvisibility=hidden)
endif() endif()
endif() endif()

View File

@ -25,7 +25,7 @@ terms of the MIT license. A copy of the license can be found in the file
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__MACH__) #if (defined(__GNUC__) || defined(__clang__)) && !defined(__MACH__)
// use aliasing to alias the exported function to one of our `mi_` functions // use aliasing to alias the exported function to one of our `mi_` functions
#define MI_FORWARD(fun) __attribute__((alias(#fun), used)); #define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default")));
#define MI_FORWARD1(fun,x) MI_FORWARD(fun) #define MI_FORWARD1(fun,x) MI_FORWARD(fun)
#define MI_FORWARD2(fun,x,y) MI_FORWARD(fun) #define MI_FORWARD2(fun,x,y) MI_FORWARD(fun)
#define MI_FORWARD0(fun,x) MI_FORWARD(fun) #define MI_FORWARD0(fun,x) MI_FORWARD(fun)
@ -60,6 +60,10 @@ terms of the MIT license. A copy of the license can be found in the file
void free(void* p) mi_attr_noexcept MI_FORWARD0(mi_free, p) void free(void* p) mi_attr_noexcept MI_FORWARD0(mi_free, p)
#endif #endif
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__MACH__)
#pragma GCC visibility push(default)
#endif
// ------------------------------------------------------ // ------------------------------------------------------
// Override new/delete // Override new/delete
// This is not really necessary as they usually call // This is not really necessary as they usually call
@ -189,4 +193,8 @@ void* reallocarray( void* p, size_t count, size_t size ) { // BSD
} }
#endif #endif
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__MACH__)
#pragma GCC visibility pop
#endif
#endif // MI_MALLOC_OVERRIDE & !_WIN32 #endif // MI_MALLOC_OVERRIDE & !_WIN32