Closed cormacrelf closed 2 years ago
@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 wasm-bindgen
in macOS Monterey
$ 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`)
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?
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.[^1]: it's not 12.0! It's different, somehow even newer! See the table.
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: