-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
macOS: Apple linker not aware of the default 10.7 deployment target, causing issues on macOS 12.0 Monterey #90342
Comments
@rustbot label O-macos A-linkage |
I ran into this, but only with the ld64 that comes with Xcode 13.1 not with Xcode 13. Are you sure you can reproduce it with Xcode 13 as well? |
Oh, indeed, I am running 13.1. I don't know about Xcode 13. |
Thanks so much for this! Took a few hours of debugging before I finally ran into this page, solved it for me |
@rustbot claim |
Thanks for reporting this. I had an issue when compiling $ cargo install wasm-pack
# ...
ld: Assertion failed: (_mode == modeFinalAddress), function finalAddress, file ld.hpp, line 1190.
clang: error: linker command failed with exit code 1 (use -v to see invocation) But following the suggestion on the issue fixed it! $ export MACOSX_DEPLOYMENT_TARGET=10.7
$ cargo install wasm-pack
# ...
Installed package `wasm-pack v0.10.1` (executable `wasm-pack`) |
Include note about bug while building on macOS in mdbook See rust-lang/rust#90342. I can watch the thread and push another PR to delete this note once this bug is resolved.
Compilation currently fails for curl on macOS Monterey due to upstream rustc issue rust-lang/rust#90342. To make this problem hurt users less, we can work around this by avoiding the specific issue that this bug causes. To avoid the rustc issue, we cannot directly reference any symbol that is configured to be in a constructor linker section, which we were previously doing intentionally to work around a different rustc issue rust-lang/rust#47384. We should be able to avoid both bugs by defining our constructor symbol as a public item in the root module, though not directly referencing it in other code. Since the root module is always used (`init` is called on-demand in key places in the code) it should not be removed by optimization. Also add a quick unit test to make sure the constructor is still working for the platforms we have CI for. Fixes #417.
Compilation currently fails for curl on macOS Monterey due to upstream rustc issue rust-lang/rust#90342. To make this problem hurt users less, we can work around this by avoiding the specific issue that this bug causes. To avoid the rustc issue, we cannot directly reference any symbol that is configured to be in a constructor linker section, which we were previously doing intentionally to work around a different rustc issue rust-lang/rust#47384. We should be able to avoid both bugs by defining our constructor symbol as a public item in the root module, though not directly referencing it in other code. Since the root module is always used (`init` is called on-demand in key places in the code) it should not be removed by optimization. Also add a quick unit test to make sure the constructor is still working for the platforms we have CI for. Fixes #417.
We've worked around this issue in the curl crate so that we won't trigger this bug -- curl 0.4.41 should now compile on Monterey without needing to use the environment variable workaround. Though this bug still ought to be fixed. |
I'm no longer having this issue, even without updating curl to 0.4.41. |
@russweas Are you sure that's not just the absence of a lockfile, with cargo just fetching latest curl from crates.io? |
Update curl dependency, remove M1 macOS build error note Curl subversion 41 includes a workaround for the m1 macOS build error. See rust-lang/rust#90342 (comment)
fd8ce77 ci: remove macos-latest build with nightly and beta (edouard) Pull request description: rust-lang/rust#90342 ACKs for top commit: edouardparis: ACK fd8ce77 Tree-SHA512: 36d84bb2583aa104d24f9eb194793680eb39142a2422851762ad9fcebbe5d4e1bee450367af18db62f93d98a517479ec45eea4c7d81749557293aaf0d6642141
TL;DR for passers-by who just want to compile code on Monterey
Try this, it might fix an error like
ld: reference to symbol (which has not been assigned an address)
orld: Assertion failed: (_mode == modeFinalAddress), function finalAddress, file ld.hpp, line 1190.
Core problem
macOS targets are tricky because LLVM behaves differently depending on the MACOSX_DEPLOYMENT_TARGET environment variable, or a version specified in the target triple you tell LLVM to use.
However, another component also uses the deployment target information to customise its output. That is the linker,
ld
from Xcode / the Command Line Tools.Rustc's default deployment target is 10.7. It only passes this to LLVM, and not to
ld
.When you invoke rustc using
env MACOSX_DEPLOYMENT_TARGET=10.7 cargo/rustc/etc
, it does what it should be doing by default, because it allows that env var to pass through told
(akacc
). When you do not provide the environment variable, it results in LLVM using 10.7 butld
using a much, much newer one.1 I believe this to be a bug in its own right -- you would expect rustc's default deployment target to apply to both the compiler and the Apple linker, but it does not.Solution: set
MACOSX_DEPLOYMENT_TARGET=10.7
if it is not already present in the environment for thecc
invocation that ultimately callsld
to create a finished binary.Observing this in practice / a linker error repro
I found this when compiling https://lib.rs/curl on an M1/aarch64 Monterey machine. It involves the
link_section="__DATA,__mod_init_func"
technique see e.g. here. The cause of the error is that with the 'much, much newer' deployment target thatld
uses by default, the linker transforms this into something completely different, and it prevents linking to the static function pointer, something that works with any deployment target set.But without further ado, this fails with a linker error if you build it in debug mode on macOS 12.0, using Xcode 13 or the Command Line Tools. It is very similar to this code in curl-rust.
The error message is a bit fragile, it seems to depend on whether you are compiling a finished binary or some intermediate crate. When compiling a crate that depends on
curl
, you get this:But with this repro or
cargo test
in the curl-rust repo, you get this (very similar in spirit):Bonus: Linkers and pre-main init hooks in Mach-O
There appear to be some changes around this recently. If you compile the equivalent C code, you actually get the exact same problem.
Using ld from Apple LLVM 13.0.0 on a Monterey machine, linking a C file with
__attribute__((section("__DATA,__mod_init_func"))) typeof(myinit) *__init = myinit;
__init(...)
as a function from main__DATA,__mod_init_func
__DATA_CONST,__mod_init_func
__TEXT,__init_offsets
, with a completely different formatIf you tell clang to link the static in the
__DATA_CONST,__mod_init_func
section instead, then it doesn't work at all, it doesn't run before main. Clearly the "API" is to use the well-known __DATA,__mod_init_func, and the only guarantee is that it will execute that function before main.The above particular Rust code not compiling is therefore not really a rustc bug in its own right. Every platform has its own way of doing this, and "newer macOS" is just another variation that needs to be added. Hacky platform-specific linker section stuff is almost certainly out of scope for stable/guaranteed behaviour. To do this correctly I think you would need a
build.rs
that always has access to aMACOSX_DEPLOYMENT_TARGET
env variable, i.e. cargo should set the env var to 10.7 if it is not already set. Then you could set some cfgs in build.rs to determine which link section to add when the target_os is macos. That solution also works for informingld
, the only difference being build.rs might get it from cargo.Meta
rustc --version --verbose
:This happens in beta/nightly-2021-10-26 too.
ld version:
Footnotes
it's not 12.0! It's different, somehow even newer! See the table. ↩
The text was updated successfully, but these errors were encountered: