]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUILD: compiler: properly distinguish weak and global symbols
authorWilly Tarreau <w@1wt.eu>
Tue, 26 Apr 2022 17:35:38 +0000 (19:35 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 26 Apr 2022 17:49:33 +0000 (19:49 +0200)
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).

include/haproxy/bug.h
include/haproxy/compiler.h

index 84dabd9097667cf14a4eb156b976263c38fb5208..5db3f15b6cb398b6d4d3bf78f478f1259ddba89b 100644 (file)
@@ -234,8 +234,8 @@ struct mem_stats {
                .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 @@ struct mem_stats {
                .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 @@ struct mem_stats {
                .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 @@ struct mem_stats {
                .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 @@ struct mem_stats {
                .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 @@ struct mem_stats {
                .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);                                                    \
index 4859d1301c47b5b80031925f031b5fe0e30c3335..a23e62a21eeaca46d275082c046b856997c8a6b9 100644 (file)
@@ -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
  * 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)