From e8fda704de72dead9a6f0aa00b8dd1451b5bd74d Mon Sep 17 00:00:00 2001 From: Juan Wajnerman Date: Wed, 6 May 2020 16:42:48 -0300 Subject: [PATCH] Force linking of `pthread_once` when compiling static binaries in musl --- src/crystal/system/unix/pthread.cr | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/crystal/system/unix/pthread.cr b/src/crystal/system/unix/pthread.cr index 67e7979cbd70..005d71cfc17f 100644 --- a/src/crystal/system/unix/pthread.cr +++ b/src/crystal/system/unix/pthread.cr @@ -178,3 +178,19 @@ class Thread @th end end + +# In musl (alpine) the calls to unwind API segfaults +# when the binary is statically linked. This is because +# some symbols like `pthread_once` are defined as "weak" +# and, for some reason, not linked into the final binary. +# Adding an explicit reference to the symbol ensures it's +# included in the statically linked binary. +{% if flag?(:musl) && flag?(:static) %} + lib LibC + fun pthread_once(Void*, Void*) + end + + fun __crystal_static_musl_workaround + LibC.pthread_once(nil, nil) + end +{% end %}