c11/threads: resolve link issues with -O0
authorEmil Velikov <emil.velikov@collabora.com>
Tue, 24 May 2016 15:23:09 +0000 (16:23 +0100)
committerEmil Velikov <emil.l.velikov@gmail.com>
Tue, 24 May 2016 19:21:31 +0000 (20:21 +0100)
Add weak symbol notation for the pthread_mutexattr* symbols, thus making
the linker happy. When building with -O1 or greater the optimiser will
kick in and remove the said functions as they are dead/unreachable code.

Ideally we'll enable the optimisations locally, yet that does not seem
to work atm.

v2: Add the AX_GCC_FUNC_ATTRIBUTE([weak]) hunk in configure.

Cc: Alejandro PiƱeiro <apinheiro@igalia.com>
Cc: Ben Widawsky <ben@bwidawsk.net>
Cc: Ilia Mirkin <imirkin@alum.mit.edu>
Cc: Rob Herring <robh@kernel.org>
Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
Tested-by: Rob Clark <robdclark@gmail.com>
Tested-by: Mark Janes <mark.a.janes@intel.com>
configure.ac
include/c11/threads_posix.h

index 47c138a6277e928c84b89717e3d0a525533975f5..fc0b1db2eb52fb312a5276302cff2a10de52cf4b 100644 (file)
@@ -233,6 +233,7 @@ AX_GCC_FUNC_ATTRIBUTE([pure])
 AX_GCC_FUNC_ATTRIBUTE([returns_nonnull])
 AX_GCC_FUNC_ATTRIBUTE([unused])
 AX_GCC_FUNC_ATTRIBUTE([warn_unused_result])
+AX_GCC_FUNC_ATTRIBUTE([weak])
 
 AM_CONDITIONAL([GEN_ASM_OFFSETS], test "x$GEN_ASM_OFFSETS" = xyes)
 
index 11d36e462399161e4a0614d4dc833928f34aa6cb..61b7fabf6378f920e54b4d767ea27f11ca784d04 100644 (file)
@@ -169,6 +169,32 @@ mtx_destroy(mtx_t *mtx)
     pthread_mutex_destroy(mtx);
 }
 
+/*
+ * XXX: Workaround when building with -O0 and without pthreads link.
+ *
+ * In such cases constant folding and dead code elimination won't be
+ * available, thus the compiler will always add the pthread_mutexattr*
+ * functions into the binary. As we try to link, we'll fail as the
+ * symbols are unresolved.
+ *
+ * Ideally we'll enable the optimisations locally, yet that does not
+ * seem to work.
+ *
+ * So the alternative workaround is to annotate the symbols as weak.
+ * Thus the linker will be happy and things don't clash when building
+ * with -O1 or greater.
+ */
+#ifdef HAVE_FUNC_ATTRIBUTE_WEAK
+__attribute__((weak))
+int pthread_mutexattr_init(pthread_mutexattr_t *attr);
+
+__attribute__((weak))
+int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
+
+__attribute__((weak))
+int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
+#endif
+
 // 7.25.4.2
 static inline int
 mtx_init(mtx_t *mtx, int type)