linkermuslmusl-gcc

Error dynamically loading libatomic.so with musl


Consider this simple build:

#!/bin/sh -eu

: ${CC:=gcc}

cat > libmain.c <<'EOF'
#include <stdatomic.h>
struct two{ long a,b; };
_Atomic struct two atwo;
int main(){ atomic_exchange(&atwo, ((struct two){1,2})); }
EOF

echo > empty.c

set -x
$CC -fpic -shared libmain.c -o libmain.so -latomic
$CC  empty.c $PWD/libmain.so 
./a.out

If I run it with CC=gcc or CC=clang, everything runs OK, but with CC=musl-gcc I get

+ musl-gcc -fpic -shared libmain.c -o libmain.so -latomic
+ musl-gcc empty.c /tmp/atomic/libmain.so
+ ./a.out
Error loading shared library libatomic.so.1: No such file or directory (needed by /tmp/atomic/libmain.so)
Error relocating /tmp/atomic/libmain.so: __atomic_exchange_16: symbol not found

What could be the problem?


Solution

  • You are trying to load a library (libatomic.so.1 here) which was linked against GLIBC:

    nm -D /lib64/libatomic.so.1 | grep GLIBC
                     w __cxa_finalize@GLIBC_2.2.5
                     U memcmp@GLIBC_2.2.5
                     U memcpy@GLIBC_2.14
                     U pthread_mutex_lock@GLIBC_2.2.5
                     U pthread_mutex_unlock@GLIBC_2.2.5
    

    into a program built with Musl libc.

    Musl FAQ says:

    Is musl compatible with glibc?
    
    Yes and no. ...
    
    Binary compatibility is much more limited, but it will steadily increase with new versions of musl. At present, some glibc-linked shared libraries can be loaded with musl, but all but the simplest glibc-linked applications will fail if musl is dropped-in in place of /lib/ld-linux.so.2.
    

    See also this answer.