Skip to content

Library search paths wrong in musl cross builds #47693

Closed
@bossmc

Description

@bossmc

While investigating #47493 I tried compiling the linked example code (https://gist.github.com/steveklabnik/5abd59a8fe7e5abda3db58bd8c208fbb) against the x86_64-unknown-linux-musl and noticed something weird - glibc was linked into my binary (unsuccessfully but still...):

Link command:

error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-nostdlib" "-Wl,--eh-frame-hdr" "-Wl,-(" "-m64" "/home/andy/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/crt1.o" "/home/andy/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/crti.o" "-L" "/home/andy/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib" "test.test0.rcgu.o" "test.test1.rcgu.o" "test.test2.rcgu.o" "-o" "test" "-Wl,--gc-sections" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "-L" "/home/andy/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib" "-l" "c" "-Wl,-Bstatic" "/home/andy/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/libcore-9d4b7130b0117f1c.rlib" "-static" "-Wl,-Bdynamic" "/home/andy/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-musl/lib/crtn.o" "-Wl,-)"

Selected error:

/usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libc.a(iofclose.o): In function `_IO_new_fclose':
          (.text+0x256): undefined reference to `_Unwind_Resume'

Note the x86_64-linux-gnu in there!

The rust code (as is standard) merely says #[link(name="c")], which is translated into "-l" "c" in the link command, but the cc (which on my system is a glibc one) is searching in the standard glibc locations rather than the muslibc ones (which would be -L/usr/lib/x86_64-linux-musl according to musl-gcc -v <the rest of the above command>). Not sure how to guess the correct location to search, one obvious solution might be to use musl-gcc in place of cc when linking for the ...-musl targets, but that's not necessarily correct on all systems (e.g. on Alpine where cc would be correct as that system is musl-native).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions