Skip to content

rustc 1.59/1.60 builds musl binaries that segfault, when compiling with musl-gcc wrappers, due to static-pie default #95926

Open
@joshtriplett

Description

@joshtriplett

With the x86_64-unknown-linux-musl target installed:

$ cat hello.rs 
fn main() {
    println!("Hello, world!");
}
$ rustc hello.rs --target x86_64-unknown-linux-musl && file hello && ./hello 
hello: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), static-pie linked, BuildID[sha1]=29ba716c85a1f3f9990246f160b61003b38b63a9, with debug_info, not stripped
Hello, world!
$ rustc hello.rs --target x86_64-unknown-linux-musl -C linker=musl-gcc && file hello && ./hello 
hello: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, with debug_info, not stripped
Segmentation fault

A gdb backtrace shows the segfault in _start_c () at ../src_musl/crt/../ldso/dlstart.c:141.

This segfault happens because the musl-gcc wrappers (including cross-compile wrappers like x86_64-linux-musl-gcc and aarch64-linux-musl-gcc) don't support static-pie in the way rustc is invoking them.

This is a regression in Rust 1.59 and 1.60; 1.58 and before produced working binaries. 1.59 includes #70740 , which not only marked the musl targets as supporting static-pie (which they may or may not depending on the linker), but also made x86_64-unknown-linux-musl default to static-pie.

For context, the musl-gcc wrappers are commonly used for a variety of reasons, including cross-compilation (particularly when linking native C code). Attempting to link using x86_64-linux-gnu-gcc or aarch64-linux-gnu-gcc cross-compilers (rather than the corresponding -musl-gcc cross-compilers) produces compatibility issues, especially with projects that build and link native C dependencies.

I noticed this problem when working with someone trying to cross-compile a project from aarch64 to x86-64, using the x86_64-linux-musl-gcc cross-compiler wrapper, installed via Debian. (I hadn't previously noticed the regression myself, because I hadn't arranged to use the cross-compiler wrappers on x86_64 since my native host is x86_64.)

This problem doesn't currently show up when cross-compiling from x86_64 to aarch64, since aarch64 doesn't use static-pie by default, but enabling PIE with -C relocation-model=pie triggers the same bug and makes the resulting binaries segfault.

I'm currently working around this, but I think this is a stable regression.

(For reference, two possible workarounds: use -C relocation-model=static, or use linker=x86_64-linux-gnu-gcc and hope it works for you.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.E-help-wantedCall for participation: Help is requested to fix this issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.O-muslTarget: The musl libcP-highHigh priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnosticsregression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions