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

Linking to private symbols from frameworks between compilation units #91372

Closed
madsmtm opened this issue Nov 29, 2021 · 17 comments · Fixed by #91870
Closed

Linking to private symbols from frameworks between compilation units #91372

madsmtm opened this issue Nov 29, 2021 · 17 comments · Fixed by #91870
Assignees
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-macos Operating system: macOS P-critical Critical priority regression-from-stable-to-beta Performance or correctness regression from stable to beta.
Milestone

Comments

@madsmtm
Copy link
Contributor

madsmtm commented Nov 29, 2021

This used to work on Rust nightly-2021-11-24:

// Cargo.toml
[package]
name = "foo"
version = "0.1.0"
edition = "2021"

// lib.rs
#[link(name = "AppKit", kind = "framework")]
extern "C" {}

// main.rs
use foo as _;

#[link(name = "CoreGraphics", kind = "framework")]
extern "C" {}

extern "C" {
    fn CGDisplayCreateUUIDFromDisplayID(display: u32) -> *const std::os::raw::c_void;
}

fn main() {
    unsafe { CGDisplayCreateUUIDFromDisplayID(0) };
}

CGDisplayCreateUUIDFromDisplayID is present in the ColorSync framework (on macOS +10.13; on lower versions, in the CoreGraphics framework). The AppKit framework uses ColorSync internally, so the symbol is available through that (I think).

Linking like this worked previously, but since Rust nightly-2021-11-26 it started failing.

This is affecting winit's CI.

Meta

OS: macOS Mojave 10.14.6.

rustc --version --verbose:

rustc 1.58.0-nightly (dd549dcab 2021-11-25)
binary: rustc
commit-hash: dd549dcab404ec4c7d07b5a83aca5bdd7171138f
commit-date: 2021-11-25
host: x86_64-apple-darwin
release: 1.58.0-nightly
LLVM version: 13.0.0
Backtrace

   Compiling link_error v0.1.0 (./link_error)
error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "-arch" "x86_64" "./link_error/target/debug/deps/link_error-df545b853490a663.1684psdcp1c8t8w.rcgu.o" "./link_error/target/debug/deps/link_error-df545b853490a663.3mg4gkj495esxxn3.rcgu.o" "./link_error/target/debug/deps/link_error-df545b853490a663.4lqpqqd6m4wsa5ac.rcgu.o" "./link_error/target/debug/deps/link_error-df545b853490a663.4q6v301re75li0ws.rcgu.o" "./link_error/target/debug/deps/link_error-df545b853490a663.4spajtpt70opf5uj.rcgu.o" "./link_error/target/debug/deps/link_error-df545b853490a663.5gpk52ejgwv2r4xx.rcgu.o" "./link_error/target/debug/deps/link_error-df545b853490a663.rk5agyyp0moj2ik.rcgu.o" "./link_error/target/debug/deps/link_error-df545b853490a663.aueco4pi09j3onj.rcgu.o" "-L" "./link_error/target/debug/deps" "-L" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "-framework" "CoreGraphics" "./link_error/target/debug/deps/liblink_error-9c8bbc70359362ee.rlib" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-12c85425d8bdabd2.rlib" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-c69df909f9b81081.rlib" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libobject-f0c16355db7307b7.rlib" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libmemchr-d69ae56aea540e36.rlib" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libaddr2line-3fd4756c45c77fb7.rlib" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libgimli-1d2aab3cb0cd976a.rlib" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd_detect-1491074c150e2040.rlib" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-5a7d603578c76924.rlib" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-a5bcb345953a0f7b.rlib" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-3ffc0cbe23a29292.rlib" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-ee442041a1b0c90d.rlib" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-e0c705be41bf34ff.rlib" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-23f0461cd27b5763.rlib" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-7db0f39aca3e5046.rlib" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-7454d006639b0b9c.rlib" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-07b1a016408f5808.rlib" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-852eb1ab762cbb0b.rlib" "-framework" "AppKit" "-lSystem" "-lresolv" "-lc" "-lm" "-liconv" "-L" "~/.rustup/toolchains/nightly-2021-11-26-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "-o" "./link_error/target/debug/deps/link_error-df545b853490a663" "-Wl,-dead_strip" "-nodefaultlibs"
  = note: Undefined symbols for architecture x86_64:
            "_CGDisplayCreateUUIDFromDisplayID", referenced from:
                link_error::main::h68dbd3e6b69f3cec in link_error-df545b853490a663.5gpk52ejgwv2r4xx.rcgu.o
          ld: symbol(s) not found for architecture x86_64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: could not compile `link_error` due to previous error

@madsmtm madsmtm added the C-bug Category: This is a bug. label Nov 29, 2021
@madsmtm
Copy link
Contributor Author

madsmtm commented Nov 29, 2021

Sorry if the issue is not very well explained, I'm very inexperienced with how linking works

madsmtm added a commit to madsmtm/winit that referenced this issue Nov 29, 2021
See also rust-windowing#1626.

The `cocoa` crate links to AppKit, which made the symbol `CGDisplayCreateUUIDFromDisplayID` from ColorSync (which AppKit uses internally) available to us (on macOS 10.13 or above).

Lately something changed in rustc so that this no longer works, see rust-lang/rust#91372. So instead, we link to AppKit directly within the `winit` crate, which, for now, allows us to use the private symbol.
@madsmtm
Copy link
Contributor Author

madsmtm commented Nov 29, 2021

This is affecting coreaudio as well from what I can tell.

@ehuss
Copy link
Contributor

ehuss commented Nov 30, 2021

This appears to have regressed via #90499, cc @hkratz.

As a temporary workaround, you can try setting the environment variable MACOSX_DEPLOYMENT_TARGET to something like 11.0.

@ehuss ehuss added O-macos Operating system: macOS A-linkage Area: linking into static, shared libraries and binaries labels Nov 30, 2021
@hkratz
Copy link
Contributor

hkratz commented Nov 30, 2021

With #90499 Rust sets MACOSX_DEPLOYMENT_TARGET to 10.7 for the linker, if not set, to match what we are passing to LLVM. Setting it to 10.8 or above get rids of the linker failure.

I will look into this more.

@hkratz
Copy link
Contributor

hkratz commented Nov 30, 2021

This is affecting coreaudio as well from what I can tell.

@madsmtm Can you elaborate on that? Building it locally with cargo +nightly build --target x86_64-apple-darwin worked fine for me on an M1.

@madsmtm
Copy link
Contributor Author

madsmtm commented Nov 30, 2021

I can confirm that using MACOSX_DEPLOYMENT_TARGET=10.8 gets rid of the linker failure in winit.
For coreaudio, MACOSX_DEPLOYMENT_TARGET=10.11 is required.

This is affecting coreaudio as well from what I can tell.

@madsmtm Can you elaborate on that? Building it locally with cargo +nightly build --target x86_64-apple-darwin worked fine for me on an M1.

You need to build a binary, so that the linker is actually invoked. Try building an example (e.g. cargo +nightly build --target x86_64-apple-darwin --example feedback).

@cormacrelf
Copy link
Contributor

I can clarify this a little more: If you were building a Rust program for an Apple platform that linked to APIs with availability somewhere between 10.7+ and 12.0+, and you weren't setting your own deployment target, #90499 has revealed a bug in your build process. Your binaries were only ever going to run on recent OS releases, but you would have been unaware without testing.

For coreaudio, if it really requires APIs from 11.0+, that seems a bit recent/limiting for a library that's existed since forever on macOS. If the authors were unaware of this, #90499 has revealed that they need to do more work to attain backwards compatibility. There will be more breakage like it, but it is the good kind, because anyone using newer Apple APIs should be aware of their minimum platform requirement, just as Xcode forces you to be.

@hkratz
Copy link
Contributor

hkratz commented Dec 1, 2021

Hmm, this change had more impact than I thought.

(Open-source) clang does a dance to determine which minimum Macos version to pass as part of the target triple to LLVM and to ld via -platform_version by looking at the various command line options and environment variables that have accumulated over the years. If nothing is specified, it apparently defaults to the minimum of the DefaultDeploymentTargetVersion specified in the SDKROOTs SDKSettings.json and the host Macos version here.

I think ideally we should probably try to mimic this behavior somewhat so we don't confront users with having to set MACOSX_DEPLOYMENT_TARGET, while they don't have to do it using clang. Not sure how we can easily accomplish this though.

madsmtm added a commit to madsmtm/winit that referenced this issue Dec 1, 2021
See also rust-windowing#1626.

The `cocoa` crate links to AppKit, which made the symbol `CGDisplayCreateUUIDFromDisplayID` from ColorSync (which AppKit uses internally) available to us (on macOS 10.13 or above).

Lately something changed in rustc so that this no longer works, see rust-lang/rust#91372. So instead, we link to AppKit directly within the `winit` crate, which, for now, allows us to use the private symbol.
@madsmtm
Copy link
Contributor Author

madsmtm commented Dec 1, 2021

I can clarify this a little more: If you were building a Rust program for an Apple platform that linked to APIs with availability somewhere between 10.7+ and 12.0+, and you weren't setting your own deployment target, #90499 has revealed a bug in your build process. Your binaries were only ever going to run on recent OS releases, but you would have been unaware without testing.

For coreaudio, if it really requires APIs from 11.0+, that seems a bit recent/limiting for a library that's existed since forever on macOS. If the authors were unaware of this, #90499 has revealed that they need to do more work to attain backwards compatibility. There will be more breakage like it, but it is the good kind, because anyone using newer Apple APIs should be aware of their minimum platform requirement, just as Xcode forces you to be.

Actually, that makes a lot of sense, thanks! I fully agree that it's better to catch these issues when developing (instead of later, when users complain that the application doesn't work on their older system)!

I've opened rust-windowing/winit#2078 to fix the linking in winit, and RustAudio/coreaudio-sys#49 would fix the linking in coreaudio.

@madsmtm
Copy link
Contributor Author

madsmtm commented Dec 13, 2021

Note that this is a breaking change for macOS users, before, they didn't have to specify MACOSX_DEPLOYMENT_TARGET, now they do. Furthermore, they have no easy way to find out that they need to (they just get a confusing linker error)!

Additionally, the affected crates (that we know about) are quite widely used (at least half of all Rust games depend on winit and/or coreaudio-sys), and have not yet fixed the issue, nor yet released updated versions which includes these fixes. And even when this has been done, it will take time before users have run cargo update and received working versions (can probably be helped along by yanking the affected versions).

I don't really know the procedure for this, but I would like to propose reverting #90499 (at least from the current beta) to give the affected crates more time to update.

Again, I really do agree with the change, I just also want to minimize the breakage macOS users will experience from upgrading Rust.

@Mark-Simulacrum Mark-Simulacrum added this to the 1.58.0 milestone Dec 13, 2021
@Mark-Simulacrum Mark-Simulacrum added the regression-from-stable-to-beta Performance or correctness regression from stable to beta. label Dec 13, 2021
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Dec 13, 2021
@hkratz
Copy link
Contributor

hkratz commented Dec 13, 2021

Like I wrote above I think ideally we should match clang's behavior and thus fall back to the default of the currently configured Macos SDK. That would mean looking at the Version property of the SDKSettings.json in the currently configured SDK. Unfortunately I don't really see a reliable way to do get the path to that without running xcrun --show-sdk-path.

@jyn514
Copy link
Member

jyn514 commented Dec 13, 2021

I think ideally we should probably try to mimic this behavior

@hkratz I think we should revert the change until you find a way to do this - it seems to be causing a lot of breakage and my understanding is the goal was just to turn a runtime error into a link time error.

@hkratz
Copy link
Contributor

hkratz commented Dec 13, 2021

@jyn514 Oh sure. I am not against a revert until that is sorted out. I will open a PR.

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 14, 2021
…rt, r=Mark-Simulacrum

Revert setting a default for the MACOSX_DEPLOYMENT_TARGET env var for linking

This reverts commit b376f56, which is the main part of rust-lang#90499, because it turns out that this causes a good amount of breakage in crates relying on the old behavior. In particular `winit`, `coreaudio` and crates that depend on them are affected. Fixes rust-lang#91372.

Background:
Before rust-lang#90499 the behavior was the following: If MACOSX_DEPLOYMENT_TARGET is not set,  we pass the minimum supported OS version to LLVM but not to the linker. The linker default depends on the Xcode version and the version of the OS it is running on. That caused one known problem in libcurl with the most recent Xcode versions. rust-lang#90499 passed the minumum supported version (10.7 for Macos x86-64) to the linker instead. This has shown to be problematic because some crates such as winit, coreaudio implicitly expect a newer minimum OS version. The libcurl issue has been fixed independently (see alexcrichton/curl-rust#417), so a revert should not really be problematic.

Eventually we should probably mimic clang's behavior and fall back to the default of the currently configured Macos SDK for both the LLVM min os target version and MACOSX_DEPLOYMENT_TARGET for linking. That would entail looking at the `Version` property of the `SDKSettings.json` in the currently configured SDK.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 14, 2021
…rt, r=Mark-Simulacrum

Revert setting a default for the MACOSX_DEPLOYMENT_TARGET env var for linking

This reverts commit b376f56, which is the main part of rust-lang#90499, because it turns out that this causes a good amount of breakage in crates relying on the old behavior. In particular `winit`, `coreaudio` and crates that depend on them are affected. Fixes rust-lang#91372.

Background:
Before rust-lang#90499 the behavior was the following: If MACOSX_DEPLOYMENT_TARGET is not set,  we pass the minimum supported OS version to LLVM but not to the linker. The linker default depends on the Xcode version and the version of the OS it is running on. That caused one known problem in libcurl with the most recent Xcode versions. rust-lang#90499 passed the minumum supported version (10.7 for Macos x86-64) to the linker instead. This has shown to be problematic because some crates such as winit, coreaudio implicitly expect a newer minimum OS version. The libcurl issue has been fixed independently (see alexcrichton/curl-rust#417), so a revert should not really be problematic.

Eventually we should probably mimic clang's behavior and fall back to the default of the currently configured Macos SDK for both the LLVM min os target version and MACOSX_DEPLOYMENT_TARGET for linking. That would entail looking at the `Version` property of the `SDKSettings.json` in the currently configured SDK.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 14, 2021
…rt, r=Mark-Simulacrum

Revert setting a default for the MACOSX_DEPLOYMENT_TARGET env var for linking

This reverts commit b376f56, which is the main part of rust-lang#90499, because it turns out that this causes a good amount of breakage in crates relying on the old behavior. In particular `winit`, `coreaudio` and crates that depend on them are affected. Fixes rust-lang#91372.

Background:
Before rust-lang#90499 the behavior was the following: If MACOSX_DEPLOYMENT_TARGET is not set,  we pass the minimum supported OS version to LLVM but not to the linker. The linker default depends on the Xcode version and the version of the OS it is running on. That caused one known problem in libcurl with the most recent Xcode versions. rust-lang#90499 passed the minumum supported version (10.7 for Macos x86-64) to the linker instead. This has shown to be problematic because some crates such as winit, coreaudio implicitly expect a newer minimum OS version. The libcurl issue has been fixed independently (see alexcrichton/curl-rust#417), so a revert should not really be problematic.

Eventually we should probably mimic clang's behavior and fall back to the default of the currently configured Macos SDK for both the LLVM min os target version and MACOSX_DEPLOYMENT_TARGET for linking. That would entail looking at the `Version` property of the `SDKSettings.json` in the currently configured SDK.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 15, 2021
…rt, r=Mark-Simulacrum

Revert setting a default for the MACOSX_DEPLOYMENT_TARGET env var for linking

This reverts commit b376f56, which is the main part of rust-lang#90499, because it turns out that this causes a good amount of breakage in crates relying on the old behavior. In particular `winit`, `coreaudio` and crates that depend on them are affected. Fixes rust-lang#91372.

Background:
Before rust-lang#90499 the behavior was the following: If MACOSX_DEPLOYMENT_TARGET is not set,  we pass the minimum supported OS version to LLVM but not to the linker. The linker default depends on the Xcode version and the version of the OS it is running on. That caused one known problem in libcurl with the most recent Xcode versions. rust-lang#90499 passed the minumum supported version (10.7 for Macos x86-64) to the linker instead. This has shown to be problematic because some crates such as winit, coreaudio implicitly expect a newer minimum OS version. The libcurl issue has been fixed independently (see alexcrichton/curl-rust#417), so a revert should not really be problematic.

Eventually we should probably mimic clang's behavior and fall back to the default of the currently configured Macos SDK for both the LLVM min os target version and MACOSX_DEPLOYMENT_TARGET for linking. That would entail looking at the `Version` property of the `SDKSettings.json` in the currently configured SDK.
@bors bors closed this as completed in 3011154 Dec 15, 2021
@rustbot rustbot removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Dec 15, 2021
@pnkfelix
Copy link
Member

reopening as this is a beta-regression, and we keep those open until beta fix lands (or we explicitly decline to fix).

@pnkfelix pnkfelix reopened this Dec 16, 2021
@pnkfelix pnkfelix added the P-critical Critical priority label Dec 16, 2021
Mark-Simulacrum pushed a commit to Mark-Simulacrum/rust that referenced this issue Jan 5, 2022
…f not set."

This reverts commit b376f56, which is
the main part of rust-lang#90499, because it turns out that this causes a good
amount of breakage in crates relying on the old behavior.

Fixes rust-lang#91372.
@pietroalbini
Copy link
Member

Backport merged.

@madsmtm
Copy link
Contributor Author

madsmtm commented Jan 27, 2022

Just for reference, the linking in the noted crates have since been fixed:

JanCVanB added a commit to roc-lang/roc that referenced this issue Jan 30, 2022
This seems related to rust-lang/rust#91372 .

Without `resolver = "2"`:

```
[nix-shell:~/code/clones/roc]$ cargo run examples/hello-rust/Hello.roc
    Finished dev [unoptimized + debuginfo] target(s) in 0.28s
     Running `target/debug/roc examples/hello-rust/Hello.roc`
thread '<unnamed>' panicked at 'Failed to rebuild src/lib.rs - stderr of the `cargo build` command was:
    Updating crates.io index
   Compiling libc v0.2.116
   Compiling cfg-if v1.0.0
   Compiling proc-macro2 v1.0.36
   ...
   Compiling gilrs v0.8.2
   Compiling gltf-json v0.16.0
   Compiling wgpu-hal v0.12.4
error: DX12 API enabled on non-Windows OS. If your project is not using resolver="2" in Cargo.toml, it should.
  --> /Users/jan/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-hal-0.12.4/src/lib.rs:53:1
   |
53 | compile_error!("DX12 API enabled on non-Windows OS. If your project is not using resolver=\"2\" in Cargo.toml, it should.");
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0432]: unresolved import `super::dx12`
  --> /Users/jan/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-hal-0.12.4/src/lib.rs:68:20
   |
68 |     pub use super::dx12::Api as Dx12;
   |                    ^^^^ could not find `dx12` in the crate root

   Compiling hexasphere v6.0.0
   Compiling bevy_macro_utils v0.6.0
For more information about this error, try `rustc --explain E0432`.
error: could not compile `wgpu-hal` due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: build failed
', compiler/build/src/link.rs:984:27
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Any { .. }', cli/src/build.rs:235:50
🔨 Rebuilding host...
[nix-shell:~/code/clones/roc]$
```

With `resolver = "2"`:

```
[nix-shell:~/code/clones/roc]$ cargo run examples/hello-rust/Hello.roc
    Finished dev [unoptimized + debuginfo] target(s) in 0.86s
     Running `target/debug/roc examples/hello-rust/Hello.roc`
🔨 Rebuilding host... Done!
Undefined symbols for architecture x86_64:
  "_AudioComponentFindNext", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit14new_with_flags17heb6ec65f112e010bE in host.o
  "_AudioComponentInstanceDispose", referenced from:
      __ZN74_$LT$coreaudio..audio_unit..AudioUnit$u20$as$u20$core..ops..drop..Drop$GT$4drop17h75c103dc6ced4686E in host.o
  "_AudioComponentInstanceNew", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit14new_with_flags17heb6ec65f112e010bE in host.o
  "_AudioObjectGetPropertyData", referenced from:
      __ZN4cpal4host9coreaudio5macos9enumerate13audio_devices17haf753430364e01ffE in host.o
      __ZN4cpal4host9coreaudio5macos9enumerate20default_input_device17h2d1534837211f2f8E in host.o
      __ZN4cpal4host9coreaudio5macos9enumerate21default_output_device17h083911c236280accE in host.o
      __ZN4cpal4host9coreaudio5macos6Device4name17h68e2f994f5671fa5E in host.o
      __ZN4cpal4host9coreaudio5macos6Device17supported_configs17h11dc942236696a63E in host.o
      __ZN4cpal4host9coreaudio5macos6Device14default_config17hdbd3efef31ad18f9E in host.o
      __ZN4cpal4host9coreaudio5macos6Device22build_input_stream_raw13rate_listener17h0d393e1cb81ceb75E in host.o
      ...
  "_AudioObjectGetPropertyDataSize", referenced from:
      __ZN4cpal4host9coreaudio5macos9enumerate13audio_devices17haf753430364e01ffE in host.o
      __ZN4cpal4host9coreaudio5macos6Device17supported_configs17h11dc942236696a63E in host.o
  "_AudioOutputUnitStart", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit5start17h3f1dd3512794e2c6E in host.o
  "_AudioOutputUnitStop", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit4stop17h8195cacdfa008254E in host.o
  "_AudioUnitGetProperty", referenced from:
      __ZN9coreaudio10audio_unit12get_property17hcf8e3ea3616b46c8E in host.o
      __ZN9coreaudio10audio_unit12get_property17h521e60fcc0df83d1E in host.o
      __ZN9coreaudio10audio_unit12get_property17hbd4c129c859bc308E in host.o
  "_AudioUnitInitialize", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit14new_with_flags17heb6ec65f112e010bE in host.o
      __ZN9coreaudio10audio_unit9AudioUnit10initialize17hb6a2c15df5611a8bE in host.o
  "_AudioUnitSetProperty", referenced from:
      __ZN9coreaudio10audio_unit12set_property17h7ac04f580fde2d6bE in host.o
      __ZN9coreaudio10audio_unit12set_property17h7fe7a189aa012be8E in host.o
      __ZN9coreaudio10audio_unit12set_property17h4c55c6b1a7e57f73E in host.o
      __ZN9coreaudio10audio_unit12set_property17he956119f0b29327cE in host.o
  "_AudioUnitUninitialize", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit12uninitialize17h3386111a26093dcbE in host.o
      __ZN74_$LT$coreaudio..audio_unit..AudioUnit$u20$as$u20$core..ops..drop..Drop$GT$4drop17h75c103dc6ced4686E in host.o
  "_CFStringGetCString", referenced from:
      __ZN4cpal4host9coreaudio5macos6Device4name17h68e2f994f5671fa5E in host.o
  "_CFStringGetCStringPtr", referenced from:
      __ZN4cpal4host9coreaudio5macos6Device4name17h68e2f994f5671fa5E in host.o
ld: symbol(s) not found for architecture x86_64

[nix-shell:~/code/clones/roc]$
```
JanCVanB added a commit to roc-lang/roc that referenced this issue Jan 31, 2022
This seems related to rust-lang/rust#91372 .

```
[nix-shell:~/code/clones/roc]$ cargo run examples/hello-rust/Hello.roc
    Finished dev [unoptimized + debuginfo] target(s) in 0.40s
     Running `target/debug/roc examples/hello-rust/Hello.roc`
🔨 Rebuilding host... Done!
Undefined symbols for architecture x86_64:
  "_AudioComponentFindNext", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit14new_with_flags17heb6ec65f112e010bE in host.o
  "_AudioComponentInstanceDispose", referenced from:
      __ZN74_$LT$coreaudio..audio_unit..AudioUnit$u20$as$u20$core..ops..drop..Drop$GT$4drop17h75c103dc6ced4686E in host.o
  "_AudioComponentInstanceNew", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit14new_with_flags17heb6ec65f112e010bE in host.o
  "_AudioObjectGetPropertyData", referenced from:
      __ZN4cpal4host9coreaudio5macos9enumerate13audio_devices17haf753430364e01ffE in host.o
      __ZN4cpal4host9coreaudio5macos9enumerate20default_input_device17h2d1534837211f2f8E in host.o
      __ZN4cpal4host9coreaudio5macos9enumerate21default_output_device17h083911c236280accE in host.o
      __ZN4cpal4host9coreaudio5macos6Device4name17h68e2f994f5671fa5E in host.o
      __ZN4cpal4host9coreaudio5macos6Device17supported_configs17h11dc942236696a63E in host.o
      __ZN4cpal4host9coreaudio5macos6Device14default_config17hdbd3efef31ad18f9E in host.o
      __ZN4cpal4host9coreaudio5macos6Device22build_input_stream_raw13rate_listener17h0d393e1cb81ceb75E in host.o
      ...
  "_AudioObjectGetPropertyDataSize", referenced from:
      __ZN4cpal4host9coreaudio5macos9enumerate13audio_devices17haf753430364e01ffE in host.o
      __ZN4cpal4host9coreaudio5macos6Device17supported_configs17h11dc942236696a63E in host.o
  "_AudioOutputUnitStart", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit5start17h3f1dd3512794e2c6E in host.o
  "_AudioOutputUnitStop", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit4stop17h8195cacdfa008254E in host.o
  "_AudioUnitGetProperty", referenced from:
      __ZN9coreaudio10audio_unit12get_property17hcf8e3ea3616b46c8E in host.o
      __ZN9coreaudio10audio_unit12get_property17h521e60fcc0df83d1E in host.o
      __ZN9coreaudio10audio_unit12get_property17hbd4c129c859bc308E in host.o
  "_AudioUnitInitialize", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit14new_with_flags17heb6ec65f112e010bE in host.o
      __ZN9coreaudio10audio_unit9AudioUnit10initialize17hb6a2c15df5611a8bE in host.o
  "_AudioUnitSetProperty", referenced from:
      __ZN9coreaudio10audio_unit12set_property17h7ac04f580fde2d6bE in host.o
      __ZN9coreaudio10audio_unit12set_property17h7fe7a189aa012be8E in host.o
      __ZN9coreaudio10audio_unit12set_property17h4c55c6b1a7e57f73E in host.o
      __ZN9coreaudio10audio_unit12set_property17he956119f0b29327cE in host.o
  "_AudioUnitUninitialize", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit12uninitialize17h3386111a26093dcbE in host.o
      __ZN74_$LT$coreaudio..audio_unit..AudioUnit$u20$as$u20$core..ops..drop..Drop$GT$4drop17h75c103dc6ced4686E in host.o
ld: symbol(s) not found for architecture x86_64

[nix-shell:~/code/clones/roc]$
```
ayazhafiz pushed a commit to roc-lang/roc that referenced this issue Jan 31, 2022
This seems related to rust-lang/rust#91372 .

```
[nix-shell:~/code/clones/roc]$ cargo run examples/hello-rust/Hello.roc
    Finished dev [unoptimized + debuginfo] target(s) in 0.40s
     Running `target/debug/roc examples/hello-rust/Hello.roc`
🔨 Rebuilding host... Done!
Undefined symbols for architecture x86_64:
  "_AudioComponentFindNext", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit14new_with_flags17heb6ec65f112e010bE in host.o
  "_AudioComponentInstanceDispose", referenced from:
      __ZN74_$LT$coreaudio..audio_unit..AudioUnit$u20$as$u20$core..ops..drop..Drop$GT$4drop17h75c103dc6ced4686E in host.o
  "_AudioComponentInstanceNew", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit14new_with_flags17heb6ec65f112e010bE in host.o
  "_AudioObjectGetPropertyData", referenced from:
      __ZN4cpal4host9coreaudio5macos9enumerate13audio_devices17haf753430364e01ffE in host.o
      __ZN4cpal4host9coreaudio5macos9enumerate20default_input_device17h2d1534837211f2f8E in host.o
      __ZN4cpal4host9coreaudio5macos9enumerate21default_output_device17h083911c236280accE in host.o
      __ZN4cpal4host9coreaudio5macos6Device4name17h68e2f994f5671fa5E in host.o
      __ZN4cpal4host9coreaudio5macos6Device17supported_configs17h11dc942236696a63E in host.o
      __ZN4cpal4host9coreaudio5macos6Device14default_config17hdbd3efef31ad18f9E in host.o
      __ZN4cpal4host9coreaudio5macos6Device22build_input_stream_raw13rate_listener17h0d393e1cb81ceb75E in host.o
      ...
  "_AudioObjectGetPropertyDataSize", referenced from:
      __ZN4cpal4host9coreaudio5macos9enumerate13audio_devices17haf753430364e01ffE in host.o
      __ZN4cpal4host9coreaudio5macos6Device17supported_configs17h11dc942236696a63E in host.o
  "_AudioOutputUnitStart", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit5start17h3f1dd3512794e2c6E in host.o
  "_AudioOutputUnitStop", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit4stop17h8195cacdfa008254E in host.o
  "_AudioUnitGetProperty", referenced from:
      __ZN9coreaudio10audio_unit12get_property17hcf8e3ea3616b46c8E in host.o
      __ZN9coreaudio10audio_unit12get_property17h521e60fcc0df83d1E in host.o
      __ZN9coreaudio10audio_unit12get_property17hbd4c129c859bc308E in host.o
  "_AudioUnitInitialize", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit14new_with_flags17heb6ec65f112e010bE in host.o
      __ZN9coreaudio10audio_unit9AudioUnit10initialize17hb6a2c15df5611a8bE in host.o
  "_AudioUnitSetProperty", referenced from:
      __ZN9coreaudio10audio_unit12set_property17h7ac04f580fde2d6bE in host.o
      __ZN9coreaudio10audio_unit12set_property17h7fe7a189aa012be8E in host.o
      __ZN9coreaudio10audio_unit12set_property17h4c55c6b1a7e57f73E in host.o
      __ZN9coreaudio10audio_unit12set_property17he956119f0b29327cE in host.o
  "_AudioUnitUninitialize", referenced from:
      __ZN9coreaudio10audio_unit9AudioUnit12uninitialize17h3386111a26093dcbE in host.o
      __ZN74_$LT$coreaudio..audio_unit..AudioUnit$u20$as$u20$core..ops..drop..Drop$GT$4drop17h75c103dc6ced4686E in host.o
ld: symbol(s) not found for architecture x86_64

[nix-shell:~/code/clones/roc]$
```
@madsmtm
Copy link
Contributor Author

madsmtm commented Sep 9, 2024

I root-caused this a little bit more today, sharing my findings:

The problem can be reproduced with MACOSX_DEPLOYMENT_TARGET=10.7 rustc --target=x86_64-apple-darwin and the following file:

#[link(name = "AppKit", kind = "framework")]
#[link(name = "CoreGraphics", kind = "framework")]
extern "C" {
    pub fn CGDisplayCreateUUIDFromDisplayID(display: u32) -> *const std::os::raw::c_void;
}

fn main() {
    unsafe { CGDisplayCreateUUIDFromDisplayID(0) };
}

Weirdly enough, if you remove the linking to CoreGraphics, everything links fine.

I think this is actually a bug in ld64, it seems like:

  • The linker is reading the symbol tables from AppKit.framework/AppKit.tbd and CoreGraphics.framework/CoreGraphics.tbd to try to figure out where CGDisplayCreateUUIDFromDisplayID comes from.
  • When reading AppKit.tbd, it sees that it has a few "reexported-libraries", which includes /System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices, which is where the symbol actually comes from (well, nowadays from ColorSync, but that is re-exported from ApplicationServices).
  • But when reading CoreGraphics.tbd , it finds the symbol $ld$install_name$os10.7$/System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices, which is intended to indicate that "when linking CoreGraphics on older platforms, use ApplicationServices instead"
  • But instead, it ends up making it seem like CoreGraphics is actually the real ApplicationServices, and CoreGraphics does not expose _CGDisplayCreateUUIDFromDisplayID, so the linking fails.

Again, I believe this to be a linker bug because the problem does not happen with lld (try passing -Clinker=rust-lld).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-macos Operating system: macOS P-critical Critical priority regression-from-stable-to-beta Performance or correctness regression from stable to beta.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants