BUILD: compiler: properly distinguish weak and global symbols
While weak symbols were finally fixed with commit fb1b6f5bc ("BUILD:
compiler: use a more portable set of asm(".weak") statements"), it
was an error to think that initcall symbols were also weak. They must
not be and they're only global. The reason is that any externally
linked code loaded as a .so would drop its weak symbols when being
loaded, hence its initcalls that may contain various function
registration calls.
The ambiguity came from the fact that we initially reused the initcall's
HA_GLOBL macro for OSX then generalized it, then turned it to a choice
between .globl and .weak based on the OS, while in fact we needed a
macro to define weak symbols.
Let's rename the macro to HA_WEAK() to make it clear it's only for weak
symbols, and redefine HA_GLOBL() that initcall needs.
This will need to be backported wherever the commit above is backported
(at least 2.5 for now).
diff --git a/include/haproxy/bug.h b/include/haproxy/bug.h
index 84dabd9..5db3f15 100644
--- a/include/haproxy/bug.h
+++ b/include/haproxy/bug.h
@@ -234,8 +234,8 @@
.file = __FILE__, .line = __LINE__, \
.type = MEM_STATS_TYPE_CALLOC, \
}; \
- HA_GLOBL("__start_mem_stats"); \
- HA_GLOBL("__stop_mem_stats"); \
+ HA_WEAK("__start_mem_stats"); \
+ HA_WEAK("__stop_mem_stats"); \
_HA_ATOMIC_INC(&_.calls); \
_HA_ATOMIC_ADD(&_.size, __x * __y); \
calloc(__x,__y); \
@@ -251,8 +251,8 @@
.file = __FILE__, .line = __LINE__, \
.type = MEM_STATS_TYPE_FREE, \
}; \
- HA_GLOBL("__start_mem_stats"); \
- HA_GLOBL("__stop_mem_stats"); \
+ HA_WEAK("__start_mem_stats"); \
+ HA_WEAK("__stop_mem_stats"); \
if (__x) \
_HA_ATOMIC_INC(&_.calls); \
free(__x); \
@@ -265,8 +265,8 @@
.file = __FILE__, .line = __LINE__, \
.type = MEM_STATS_TYPE_FREE, \
}; \
- HA_GLOBL("__start_mem_stats"); \
- HA_GLOBL("__stop_mem_stats"); \
+ HA_WEAK("__start_mem_stats"); \
+ HA_WEAK("__stop_mem_stats"); \
if (__builtin_constant_p((x)) || __builtin_constant_p(*(x))) { \
HA_LINK_ERROR(call_to_ha_free_attempts_to_free_a_constant); \
} \
@@ -283,8 +283,8 @@
.file = __FILE__, .line = __LINE__, \
.type = MEM_STATS_TYPE_MALLOC, \
}; \
- HA_GLOBL("__start_mem_stats"); \
- HA_GLOBL("__stop_mem_stats"); \
+ HA_WEAK("__start_mem_stats"); \
+ HA_WEAK("__stop_mem_stats"); \
_HA_ATOMIC_INC(&_.calls); \
_HA_ATOMIC_ADD(&_.size, __x); \
malloc(__x); \
@@ -297,8 +297,8 @@
.file = __FILE__, .line = __LINE__, \
.type = MEM_STATS_TYPE_REALLOC, \
}; \
- HA_GLOBL("__start_mem_stats"); \
- HA_GLOBL("__stop_mem_stats"); \
+ HA_WEAK("__start_mem_stats"); \
+ HA_WEAK("__stop_mem_stats"); \
_HA_ATOMIC_INC(&_.calls); \
_HA_ATOMIC_ADD(&_.size, __y); \
realloc(__x,__y); \
@@ -311,8 +311,8 @@
.file = __FILE__, .line = __LINE__, \
.type = MEM_STATS_TYPE_STRDUP, \
}; \
- HA_GLOBL("__start_mem_stats"); \
- HA_GLOBL("__stop_mem_stats"); \
+ HA_WEAK("__start_mem_stats"); \
+ HA_WEAK("__stop_mem_stats"); \
_HA_ATOMIC_INC(&_.calls); \
_HA_ATOMIC_ADD(&_.size, __y); \
strdup(__x); \
diff --git a/include/haproxy/compiler.h b/include/haproxy/compiler.h
index 4859d13..a23e62a 100644
--- a/include/haproxy/compiler.h
+++ b/include/haproxy/compiler.h
@@ -88,7 +88,7 @@
#endif // USE_OBSOLETE_LINKER
-/* Declare a symbol as global and if possible, as weak. Since we don't want to
+/* Declare a symbol as weak if possible, otherwise global. Since we don't want to
* error on multiple definitions, the symbol is declared weak. On MacOS ".weak"
* does not exist and we must continue to use ".globl" instead. Note that
* ".global" is to be avoided on other platforms as llvm complains about it
@@ -97,11 +97,15 @@
* anyway (and most likely it will only work with !USE_OBSOLETE_LINKER).
*/
#if defined(__APPLE__)
-# define __HA_GLOBL(sym) __asm__(".globl " #sym)
+# define __HA_WEAK(sym) __asm__(".globl " #sym)
#else
-# define __HA_GLOBL(sym) __asm__(".weak " #sym)
+# define __HA_WEAK(sym) __asm__(".weak " #sym)
#endif
-#define HA_GLOBL(sym) __HA_GLOBL(sym)
+#define HA_WEAK(sym) __HA_WEAK(sym)
+
+/* declare a symbol as global */
+#define __HA_GLOBL(sym) __asm__(".globl " #sym)
+#define HA_GLOBL(sym) __HA_GLOBL(sym)
/* use this attribute on a variable to move it to the read_mostly section */
#if !defined(__read_mostly)