aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLasse Collin <lasse.collin@tukaani.org>2023-10-22 17:08:39 +0300
committerJia Tan <jiat0218@gmail.com>2023-10-31 18:44:59 +0800
commit6961a5ac7df178bfc2b7a181c40575847bc3035f (patch)
tree052f9eeb33ba24ec41f23c2f3b5c8ac2316f758b
parentliblzma: Refer to MinGW-w64 instead of MinGW in the API headers. (diff)
downloadxz-6961a5ac7df178bfc2b7a181c40575847bc3035f.tar.xz
liblzma: #define lzma_attr_visibility_hidden in common.h.
In ELF shared libs: -fvisibility=hidden affects definitions of symbols but not declarations.[*] This doesn't affect direct calls to functions inside liblzma as a linker can replace a call to lzma_foo@plt with a call directly to lzma_foo when -fvisibility=hidden is used. [*] It has to be like this because otherwise every installed header file would need to explictly set the symbol visibility to default. When accessing extern variables that aren't defined in the same translation unit, compiler assumes that the variable has the default visibility and thus indirection is needed. Unlike function calls, linker cannot optimize this. Using __attribute__((__visibility__("hidden"))) with the extern variable declarations tells the compiler that indirection isn't needed because the definition is in the same shared library. About 15+ years ago, someone told me that it would be good if the CRC tables would be defined in the same translation unit as the C code of the CRC functions. While I understood that it could help a tiny amount, I didn't want to change the code because a separate translation unit for the CRC tables was needed for the x86 assembly code anyway. But when visibility attributes are supported, simply marking the extern declaration with the hidden attribute will get identical result. When there are only a few affected variables, this is trivial to do. I wish I had understood this back then already.
-rw-r--r--src/liblzma/common/common.h11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/liblzma/common/common.h b/src/liblzma/common/common.h
index 176c3a8f..b09e8ddf 100644
--- a/src/liblzma/common/common.h
+++ b/src/liblzma/common/common.h
@@ -17,17 +17,28 @@
#include "mythread.h"
#include "tuklib_integer.h"
+// LZMA_API_EXPORT is used to mark the exported API functions.
+// It's used to define the LZMA_API macro.
+//
+// lzma_attr_visibility_hidden is used for marking *declarations* of extern
+// variables that are internal to liblzma (-fvisibility=hidden alone is
+// enough to hide the *definitions*). Such markings allow slightly more
+// efficient code to accesses those variables in ELF shared libraries.
#if defined(_WIN32) || defined(__CYGWIN__)
# ifdef DLL_EXPORT
# define LZMA_API_EXPORT __declspec(dllexport)
# else
# define LZMA_API_EXPORT
# endif
+# define lzma_attr_visibility_hidden
// Don't use ifdef or defined() below.
#elif HAVE_VISIBILITY
# define LZMA_API_EXPORT __attribute__((__visibility__("default")))
+# define lzma_attr_visibility_hidden \
+ __attribute__((__visibility__("hidden")))
#else
# define LZMA_API_EXPORT
+# define lzma_attr_visibility_hidden
#endif
#define LZMA_API(type) LZMA_API_EXPORT type LZMA_API_CALL