Skip to content

Commit 189c663

Browse files
authored
Merge pull request #435 from cyanogilvie/support-musl-std-libs
Support musl libc (Alpine) in the test harness
2 parents b0f3c30 + 1bb2fe6 commit 189c663

6 files changed

Lines changed: 83 additions & 7 deletions

File tree

c-tests/runtests.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ else
3434
ARCH="other"
3535
fi
3636

37+
# busybox diff (e.g. on Alpine) lacks --strip-trailing-cr
38+
if diff --strip-trailing-cr /dev/null /dev/null >/dev/null 2>&1;then
39+
DIFF_STRIP_CR=--strip-trailing-cr
40+
else
41+
DIFF_STRIP_CR=
42+
fi
43+
3744
runtest () {
3845
t=$1
3946
if test -f $t.mach && ! $GREP -E "`uname -m`" $t.mach >/dev/null; then
@@ -57,14 +64,14 @@ runtest () {
5764
$TIMEOUT sh $execution_program $compiler $t $add_main 2>$stderrf >$outf
5865
code=$?
5966
if test $code = $expect_code; then
60-
if test x$expect_out != x && ! diff --strip-trailing-cr -up $expect_out $outf >$errf;then
67+
if test x$expect_out != x && ! diff $DIFF_STRIP_CR -up $expect_out $outf >$errf;then
6168
$ECHO Output mismatch
6269
cat $errf
6370
else
6471
ok=`expr $ok + 1`
6572
$ECHO -ne "OK \r"
6673
fi
67-
if test x$stderr_expect_out != x && ! diff --strip-trailing-cr -up $stderr_expect_out $stderrf >$errf;then
74+
if test x$stderr_expect_out != x && ! diff $DIFF_STRIP_CR -up $stderr_expect_out $stderrf >$errf;then
6875
$ECHO Stderr mismatch
6976
cat $errf
7077
else

c2mir/aarch64/mirc_aarch64_linux.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@ static char aarch64_mirc[]
5252
"#define __UINT32_MAX__ (__INT32_MAX__ * 2u + 1u)\n"
5353
"#define __UINT64_MAX__ (__INT64_MAX__ * 2u + 1u)\n"
5454
#if defined(__linux__)
55-
"#define __WCHAR_MAX__ 0x7fffffff\n"
55+
/* wchar_t is unsigned int in AAPCS64 */
56+
"#define __WCHAR_MAX__ 4294967295u\n"
57+
"#define __WCHAR_MIN__ 0u\n"
5658
#else
5759
"#define __WCHAR_MAX__ 2147483647\n"
58-
#endif
5960
"#define __WCHAR_MIN__ (-__WCHAR_MAX__ - 1)\n"
61+
#endif
6062
"#define __SCHAR_MAX__ __INT8_MAX__\n"
6163
"#define __SHRT_MAX__ __INT16_MAX__\n"
6264
"#define __INT_MAX__ __INT32_MAX__\n"

c2mir/aarch64/mirc_aarch64_stddef.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ static char stddef_str[]
1010
"typedef long ptrdiff_t;\n"
1111
"typedef unsigned long size_t;\n"
1212
"typedef long double max_align_t;\n"
13+
#if defined(__APPLE__)
1314
"typedef int wchar_t;\n"
15+
#else
16+
/* unsigned in AAPCS64; matters because musl's alltypes.h redeclares it */
17+
"typedef unsigned int wchar_t;\n"
18+
#endif
1419
"\n"
1520
"#define NULL ((void *) 0)\n"
1621
"\n"

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)