BUG/MEDIUM: tools: avoid calling dlsym() in static builds

Since 2.4 with commit 64192392c ("MINOR: tools: add functions to retrieve
the address of a symbol"), we can resolve symbols. However some old glibc
crash in dlsym() when the program is statically built.

Fortunately even on these old libs we can detect lack of support by
calling dlopen(NULL). Normally it returns a handle to the current
program, but on a static build it returns NULL. This is sufficient to
refrain from calling dlsym() (which will be of very limited use anyway),
so we check this once at boot and use the result when needed.

This may be backported to 2.4. On stable versions, be careful to place
the init code inside an if/endif guard that checks for DL support.

(cherry picked from commit 288dc1d8eeb3340068792698e98960c59faa6c99)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit 7566c51e166fc0091c595605785da0ebe5cecc20)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit d577d9817b1208d237b0fd9fe8f956ebd71e9227)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
diff --git a/src/tools.c b/src/tools.c
index c40ad5f..c381f21 100644
--- a/src/tools.c
+++ b/src/tools.c
@@ -4826,6 +4826,23 @@
 	return ret;
 }
 
+/* dlopen() support: 0=no/not yet checked, 1=ok */
+static int dlopen_usable = 0;
+
+/* Sets dlopen_usable to true if dlopen() works and is usable. We verify if
+ * we're in a static build because some old glibcs used to crash in dlsym()
+ * in this case.
+ */
+void check_if_dlopen_usable()
+{
+	/* dlopen(NULL) returns a handle to the main program or NULL
+	 * on static builds.
+	 */
+	dlopen_usable = dlopen(NULL, RTLD_LAZY) ? 1 : 0;
+}
+
+INITCALL0(STG_PREPARE, check_if_dlopen_usable);
+
 /* Tries to retrieve the address of the first occurrence symbol <name>.
  * Note that NULL in return is not always an error as a symbol may have that
  * address in special situations.
@@ -4835,7 +4852,8 @@
 	void *ptr = NULL;
 
 #ifdef RTLD_DEFAULT
-	ptr = dlsym(RTLD_DEFAULT, name);
+	if (dlopen_usable)
+		ptr = dlsym(RTLD_DEFAULT, name);
 #endif
 	return ptr;
 }
@@ -4850,7 +4868,8 @@
 	void *ptr = NULL;
 
 #ifdef RTLD_NEXT
-	ptr = dlsym(RTLD_NEXT, name);
+	if (dlopen_usable)
+		ptr = dlsym(RTLD_NEXT, name);
 #endif
 	return ptr;
 }