Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library search paths wrong in musl cross builds #47693

Closed
bossmc opened this issue Jan 24, 2018 · 2 comments
Closed

Library search paths wrong in musl cross builds #47693

bossmc opened this issue Jan 24, 2018 · 2 comments

Comments

@bossmc
Copy link
Contributor

bossmc commented Jan 24, 2018

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).

@sfackler
Copy link
Member

If you're cross compiling, you're generally going to have to override the linker, to musl-gcc in this case. This is configured in .cargo/config: https://doc.rust-lang.org/cargo/reference/config.html#configuration-keys.

@bossmc
Copy link
Contributor Author

bossmc commented Jan 24, 2018

Ah, thank you, that's perfect. There's a bit of a lack of information about how to use the -musl targets (or how to cross compile), there used to be https://doc.rust-lang.org/1.12.1/book/advanced-linking.html but this seems to have been removed in newer editions.

@bossmc bossmc closed this as completed Feb 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants