Skip to content

fails to link curl::init()'s call to INIT_CTOR on macOS 12.0 (Monterey) #417

Closed
@cormacrelf

Description

@cormacrelf

Summary

Only on macOS. May occur on macOS earlier than 12 using Xcode 13, I'm not sure. Underlying issue is rust-lang/rust#90342

You get a linker error, there is a workaround for end users of the crate (see the rust issue; set the env var). I think curl-rust specifically can avoid this problem.

Repro:

  • rust 1.56 stable
  • macOS 12.0.1 Monterey
  • cargo test in the curl-rust repo (currently at df64ee4).

This is using either system libcurl or with --features static-curl. Makes no difference.

$ cargo test

... very long cc -arch arm64 invocation
...
  = note: ld: reference to symbol (which has not been assigned an address) __ZN4curl4init9INIT_CTOR17h97cc33cf050cb462E in '__ZN4curl4init17ha644d831c2a57f65E' from /Users/cormac/git/tryout/libcurl-monterey/target/debug/deps/libcurl-0f9cbb7dde66dd88.rlib(curl-0f9cbb7dde66dd88.curl.0b6dcf6e-cgu.2.rcgu.o) for architecture arm64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

Possible solutions, other than waiting for the rust issue

The relevant symbol is defined at:

curl-rust/src/lib.rs

Lines 93 to 101 in df64ee4

/// An exported constructor function. On supported platforms, this will be
/// invoked automatically before the program's `main` is called.
#[cfg_attr(
any(target_os = "linux", target_os = "freebsd", target_os = "android"),
link_section = ".init_array"
)]
#[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")]
#[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
static INIT_CTOR: extern "C" fn() = init_inner;

I think the actual bug is due to the the workaround for rust-lang/rust#47384 referenced further down, in which the init function calls INIT_CTOR();.

I think you can solve it by:

  • Adding #[used] to INIT_CTOR (any MSRV issue? not sure)
  • Moving INIT_CTOR outside the init function
  • Change pub fn init() { INIT_CTOR(); } to pub fn init() { init_inner(); }

It works on macOS 12, at least. But I think it would be good enough, because:

  • We're just trying to get rust to preserve INIT_CTOR, despite that rust-lang/rust issue 47384 where not using any code from the module means it won't get linked at all
  • Does curl-rust have a problem with people using the library but managing not to use any code from it? If no code is used, do we need the pre-main initializer to run at all?
  • I don't think so. It's not obvious to me that curl-rust is actually affected by the thing it has a workaround for.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions