Skip to content

Commit d867ffb

Browse files
cyanogilvieclaude
andcommitted
Support musl libc in the test drivers' standard library tables
c2mir-driver.c, mir-bin-run.c, and mir-bin-driver.c resolve external symbols for executed programs by dlopening hardcoded glibc paths (/lib64/libc.so.6, /lib/<triplet>-linux-gnu/libc.so.6, ...), so on musl distributions like Alpine every executed test fails with e.g. "can not load symbol printf" (issue #307), and mir-bin-driver exits before running anything. On musl, libc, libm, libpthread, and libdl are all one shared object installed as the dynamic loader /lib/ld-musl-<arch>.so.1: add that path (selected by __linux__ && !__GLIBC__) to all three tables. Fixes #307. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 99c6507 commit d867ffb

3 files changed

Lines changed: 65 additions & 3 deletions

File tree

c2mir/c2mir-driver.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,29 @@ struct lib {
6565
typedef struct lib lib_t;
6666

6767
#if defined(__unix__)
68-
#if UINTPTR_MAX == 0xffffffff
68+
#if defined(__linux__) && !defined(__GLIBC__)
69+
/* A non-glibc Linux libc, e.g. musl on Alpine: libc, libm, libpthread, and
70+
libdl all live in one shared object, installed as the dynamic loader
71+
/lib/ld-musl-<arch>.so.1 on musl. */
72+
#if defined(__x86_64__)
73+
#define MUSL_LIB_ARCH "x86_64"
74+
#elif defined(__aarch64__)
75+
#define MUSL_LIB_ARCH "aarch64"
76+
#elif defined(__PPC64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
77+
#define MUSL_LIB_ARCH "powerpc64le"
78+
#elif defined(__PPC64__)
79+
#define MUSL_LIB_ARCH "powerpc64"
80+
#elif defined(__s390x__)
81+
#define MUSL_LIB_ARCH "s390x"
82+
#elif defined(__riscv)
83+
#define MUSL_LIB_ARCH "riscv64"
84+
#else
85+
#error cannot recognize the non-glibc linux target
86+
#endif
87+
static lib_t std_libs[]
88+
= {{"/lib/ld-musl-" MUSL_LIB_ARCH ".so.1", NULL}, {"/usr/lib/libc.so", NULL}};
89+
static const char *std_lib_dirs[] = {"/lib", "/usr/lib"};
90+
#elif UINTPTR_MAX == 0xffffffff
6991
static lib_t std_libs[]
7092
= {{"/lib/libc.so", NULL}, {"/lib/libm.so", NULL}, {"/lib/libc.so.6", NULL},
7193
{"/lib32/libc.so.6", NULL}, {"/lib/libm.so.6", NULL}, {"/lib32/libm.so.6", NULL},

mir-bin-driver.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,25 @@ static struct lib {
2424
char *name;
2525
void *handler;
2626
} libs[] = {
27-
#if !defined(__APPLE__)
27+
#if defined(__linux__) && !defined(__GLIBC__)
28+
/* A non-glibc Linux libc, e.g. musl on Alpine: everything lives in one
29+
shared object, installed as the dynamic loader on musl. */
30+
#if defined(__x86_64__)
31+
{"/lib/ld-musl-x86_64.so.1", NULL}
32+
#elif defined(__aarch64__)
33+
{"/lib/ld-musl-aarch64.so.1", NULL}
34+
#elif defined(__PPC64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
35+
{"/lib/ld-musl-powerpc64le.so.1", NULL}
36+
#elif defined(__PPC64__)
37+
{"/lib/ld-musl-powerpc64.so.1", NULL}
38+
#elif defined(__s390x__)
39+
{"/lib/ld-musl-s390x.so.1", NULL}
40+
#elif defined(__riscv)
41+
{"/lib/ld-musl-riscv64.so.1", NULL}
42+
#else
43+
#error cannot recognize the non-glibc linux target
44+
#endif
45+
#elif !defined(__APPLE__)
2846
{"/lib64/libc.so.6", NULL}, {"/lib64/libm.so.6", NULL}, {"/lib64/libpthread.so.0", NULL}
2947
#else
3048
{"/usr/lib/libc.dylib", NULL}, {"/usr/lib/libm.dylib", NULL}

mir-bin-run.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,29 @@ typedef struct lib lib_t;
2828

2929
/* stdlibs according to c2mir */
3030
#if defined(__unix__)
31-
#if UINTPTR_MAX == 0xffffffff
31+
#if defined(__linux__) && !defined(__GLIBC__)
32+
/* A non-glibc Linux libc, e.g. musl on Alpine: libc, libm, libpthread, and
33+
libdl all live in one shared object, installed as the dynamic loader
34+
/lib/ld-musl-<arch>.so.1 on musl. */
35+
#if defined(__x86_64__)
36+
#define MUSL_LIB_ARCH "x86_64"
37+
#elif defined(__aarch64__)
38+
#define MUSL_LIB_ARCH "aarch64"
39+
#elif defined(__PPC64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
40+
#define MUSL_LIB_ARCH "powerpc64le"
41+
#elif defined(__PPC64__)
42+
#define MUSL_LIB_ARCH "powerpc64"
43+
#elif defined(__s390x__)
44+
#define MUSL_LIB_ARCH "s390x"
45+
#elif defined(__riscv)
46+
#define MUSL_LIB_ARCH "riscv64"
47+
#else
48+
#error cannot recognize the non-glibc linux target
49+
#endif
50+
static lib_t std_libs[]
51+
= {{"/lib/ld-musl-" MUSL_LIB_ARCH ".so.1", NULL}, {"/usr/lib/libc.so", NULL}};
52+
static const char *std_lib_dirs[] = {"/lib", "/usr/lib"};
53+
#elif UINTPTR_MAX == 0xffffffff
3254
static lib_t std_libs[]
3355
= {{"/lib/libc.so.6", NULL}, {"/lib32/libc.so.6", NULL}, {"/lib/libm.so.6", NULL},
3456
{"/lib32/libm.so.6", NULL}, {"/lib/libpthread.so.0", NULL}, {"/lib32/libpthread.so.0", NULL}};

0 commit comments

Comments
 (0)