Skip to content

Commit df738ff

Browse files
committed
Detect musl by probing filesystem for native builds
LLVM's default target triple on Alpine reports x86_64-unknown-linux-gnu instead of musl, causing the embedded LLD path to set the wrong dynamic linker (/lib64/ld-linux-x86-64.so.2 instead of /lib/ld-musl-x86_64.so.1). Binaries linked this way fail with "not found" because the interpreter doesn't exist on Alpine. For native compilation, probe for the musl dynamic linker on the filesystem rather than trusting the triple. Cross-compilation still relies on the triple since the host filesystem isn't relevant.
1 parent 7f5620c commit df738ff

1 file changed

Lines changed: 30 additions & 7 deletions

File tree

src/libponyc/codegen/genexe.cc

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,39 @@ static const char* elf_emulation(compile_t* c)
302302
}
303303
}
304304

305+
static bool file_exists(const char* path)
306+
{
307+
struct stat st;
308+
return stat(path, &st) == 0;
309+
}
310+
311+
// Check whether the host is actually musl. LLVM's default target triple
312+
// may report "gnu" on musl systems (e.g., Alpine's LLVM reports
313+
// x86_64-unknown-linux-gnu). For native compilation, probe the filesystem
314+
// rather than trusting the triple.
315+
static bool host_is_musl(compile_t* c, llvm::Triple::ArchType arch)
316+
{
317+
if(is_cross_compiling(c))
318+
return false;
319+
320+
const char* musl_linker = NULL;
321+
switch(arch)
322+
{
323+
case llvm::Triple::x86_64: musl_linker = "/lib/ld-musl-x86_64.so.1"; break;
324+
case llvm::Triple::aarch64: musl_linker = "/lib/ld-musl-aarch64.so.1"; break;
325+
case llvm::Triple::riscv64: musl_linker = "/lib/ld-musl-riscv64.so.1"; break;
326+
case llvm::Triple::arm:
327+
case llvm::Triple::thumb: musl_linker = "/lib/ld-musl-arm.so.1"; break;
328+
default: return false;
329+
}
330+
331+
return file_exists(musl_linker);
332+
}
333+
305334
static const char* dynamic_linker_path(compile_t* c)
306335
{
307336
llvm::Triple triple(c->opt->triple);
308-
bool is_musl = triple.isMusl();
337+
bool is_musl = triple.isMusl() || host_is_musl(c, triple.getArch());
309338

310339
switch(triple.getArch())
311340
{
@@ -345,12 +374,6 @@ static const char* system_triple(compile_t* c)
345374
return stringtab(result.c_str());
346375
}
347376

348-
static bool file_exists(const char* path)
349-
{
350-
struct stat st;
351-
return stat(path, &st) == 0;
352-
}
353-
354377
static const char* find_libc_crt_dir(const char* sysroot,
355378
const char* sys_triple)
356379
{

0 commit comments

Comments
 (0)