Skip to content

Creating Objective-C selectors fails compilation at ld phase #80019

Closed
@nvzqz

Description

@nvzqz

I tried this code:

use std::ffi::c_void;

#[link(name = "objc", kind = "dylib")]
extern "C" {}

#[repr(transparent)]
#[derive(Debug)]
pub struct SEL(*const c_void);

unsafe impl Sync for SEL {}

#[link_section = "__TEXT,__objc_methname,cstring_literals"]
#[no_mangle]
#[used]
static aClass: [u8; 6] = *b"class\0";

#[link_section = "__DATA,__objc_selrefs,literal_pointers,no_dead_strip"]
#[used]
static CLASS_SEL: SEL = SEL(aClass.as_ptr().cast());

fn main() {
    let sel: &SEL = &CLASS_SEL;

    println!("{:?}", sel);
}

I expected to see this happen: The Objective-C runtime to register CLASS_SEL on startup and the pointer within CLASS_SEL be printed according to the Debug impl.

Instead, this happened: a compile failure when invoking ld.

Output:

error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-m64" "-arch" "x86_64" "-L" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.23krzzzlv7ezytc3.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.27yqtl2ylu03xj6q.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.2grqpfl6x3qw6iog.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.3dt6412lsuw6p800.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.3s3n8189tgrmo8kg.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.40m92p34zpuv3v5w.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.49tlrzb126jrkxo6.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.4ao0x1z97rza55sf.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.4yul2o9rg14svn1q.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.9o9lxkvkswp6als.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.eq4nwdbclem533e.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.jnq2xws0k8hqned.rcgu.o" "-o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.38all9tx0n3ois7v.rcgu.o" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps" "-L" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "-lobjc" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-688c1376a25c049d.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-078f89c2cdb6d46b.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libobject-fec020208bc1ad3c.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libaddr2line-38540dcacc9fd218.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libgimli-6f613179f618c598.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-4d1a5d7118aaeaf2.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-d88fab3b1b9d8356.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-a21754532a052f2f.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-2e9ebc4127641a96.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-82d0f961232a05ca.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-370b1b71f08bac3c.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-816106c1f35f5421.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-20f3a030f1a56a86.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-60333aa00936c5ce.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-116aaeaea873ef94.rlib" "-lSystem" "-lresolv" "-lc" "-lm"
  = note: 0  0x109353078  __assert_rtn + 123
          1  0x109354581  mach_o::relocatable::PointerToCStringSection<x86_64>::targetCString(mach_o::relocatable::Atom<x86_64> const*, ld::IndirectBindingTable const&) const (.cold.2) + 0
          2  0x109231810  mach_o::relocatable::PointerToCStringSection<x86_64>::targetCString(mach_o::relocatable::Atom<x86_64> const*, ld::IndirectBindingTable const&) const + 152
          3  0x109231678  mach_o::relocatable::PointerToCStringSection<x86>::contentHash(mach_o::relocatable::Atom<x86> const*, ld::IndirectBindingTable const&) const + 70
          4  0x109230485  mach_o::relocatable::Atom<arm64>::contentHash(ld::IndirectBindingTable const&) const + 39
          5  0x109284568  std::__1::__hash_iterator<std::__1::__hash_node<std::__1::__hash_value_type<ld::Atom const*, unsigned int>, void*>*> std::__1::__hash_table<std::__1::__hash_value_type<ld::Atom const*, unsigned int>, std::__1::__unordered_map_hasher<ld::Atom const*, std::__1::__hash_value_type<ld::Atom const*, unsigned int>, ld::tool::SymbolTable::ReferencesHashFuncs, true>, std::__1::__unordered_map_equal<ld::Atom const*, std::__1::__hash_value_type<ld::Atom const*, unsigned int>, ld::tool::SymbolTable::ReferencesHashFuncs, true>, std::__1::allocator<std::__1::__hash_value_type<ld::Atom const*, unsigned int> > >::find<ld::Atom const*>(ld::Atom const* const&) + 40
          6  0x10927f999  ld::tool::SymbolTable::findSlotForReferences(ld::Atom const*, ld::Atom const**) + 127
          7  0x1092884d0  ld::tool::Resolver::convertReferencesToIndirect(ld::Atom const&) + 190
          8  0x1092880fd  ld::tool::Resolver::doAtom(ld::Atom const&) + 1519
          9  0x10922debf  mach_o::relocatable::File<arm64>::forEachAtom(ld::File::AtomHandler&) const + 57
          10  0x10927ae8e  ld::tool::InputFiles::forEachInitialAtom(ld::File::AtomHandler&, ld::Internal&) + 842
          11  0x10928ba5a  ld::tool::Resolver::resolve() + 44
          12  0x109211f85  main + 334
          13  0x7fff689c3cc9  start + 1
          A linker snapshot was created at:
              /tmp/rust_selector_example-2020-11-13-211415.ld-snapshot
          ld: Assertion failed: (0 && "unsupported reference to selector"), function targetCString, file /Library/Caches/com.apple.xbs/Sources/ld64/ld64-609/src/ld/parsers/macho_relocatable_file.cpp, line 6626.
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
          

error: aborting due to previous error

error: could not compile `rust-selector-example`

To learn more, run the command again with --verbose.

I made rust-selector-example to demonstrate this along with the output files.

Meta

rustc --version --verbose:

rustc 1.48.0 (7eac88abb 2020-11-16)
binary: rustc
commit-hash: 7eac88abb2e57e752f3302f02be5f3ce3d7adfb4
commit-date: 2020-11-16
host: x86_64-apple-darwin
release: 1.48.0
LLVM version: 11.0
Backtrace

Same output as above.

   Compiling rust-selector-example v0.1.0 (/Users/nvzqz/dev/projects/rust/rust-selector-example)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-m64" "-arch" "x86_64" "-L" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.23krzzzlv7ezytc3.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.27yqtl2ylu03xj6q.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.2grqpfl6x3qw6iog.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.3dt6412lsuw6p800.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.3s3n8189tgrmo8kg.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.40m92p34zpuv3v5w.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.49tlrzb126jrkxo6.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.4ao0x1z97rza55sf.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.4yul2o9rg14svn1q.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.9o9lxkvkswp6als.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.eq4nwdbclem533e.rcgu.o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.jnq2xws0k8hqned.rcgu.o" "-o" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps/rust_selector_example.38all9tx0n3ois7v.rcgu.o" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/Users/nvzqz/dev/projects/rust/rust-selector-example/target/debug/deps" "-L" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "-lobjc" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-688c1376a25c049d.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-078f89c2cdb6d46b.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libobject-fec020208bc1ad3c.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libaddr2line-38540dcacc9fd218.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libgimli-6f613179f618c598.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-4d1a5d7118aaeaf2.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-d88fab3b1b9d8356.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-a21754532a052f2f.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-2e9ebc4127641a96.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-82d0f961232a05ca.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-370b1b71f08bac3c.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-816106c1f35f5421.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-20f3a030f1a56a86.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-60333aa00936c5ce.rlib" "/Users/nvzqz/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-116aaeaea873ef94.rlib" "-lSystem" "-lresolv" "-lc" "-lm"
  = note: 0  0x106756078  __assert_rtn + 123
          1  0x106757581  mach_o::relocatable::PointerToCStringSection<x86_64>::targetCString(mach_o::relocatable::Atom<x86_64> const*, ld::IndirectBindingTable const&) const (.cold.2) + 0
          2  0x106634810  mach_o::relocatable::PointerToCStringSection<x86_64>::targetCString(mach_o::relocatable::Atom<x86_64> const*, ld::IndirectBindingTable const&) const + 152
          3  0x106634678  mach_o::relocatable::PointerToCStringSection<x86>::contentHash(mach_o::relocatable::Atom<x86> const*, ld::IndirectBindingTable const&) const + 70
          4  0x106633485  mach_o::relocatable::Atom<arm64>::contentHash(ld::IndirectBindingTable const&) const + 39
          5  0x106687568  std::__1::__hash_iterator<std::__1::__hash_node<std::__1::__hash_value_type<ld::Atom const*, unsigned int>, void*>*> std::__1::__hash_table<std::__1::__hash_value_type<ld::Atom const*, unsigned int>, std::__1::__unordered_map_hasher<ld::Atom const*, std::__1::__hash_value_type<ld::Atom const*, unsigned int>, ld::tool::SymbolTable::ReferencesHashFuncs, true>, std::__1::__unordered_map_equal<ld::Atom const*, std::__1::__hash_value_type<ld::Atom const*, unsigned int>, ld::tool::SymbolTable::ReferencesHashFuncs, true>, std::__1::allocator<std::__1::__hash_value_type<ld::Atom const*, unsigned int> > >::find<ld::Atom const*>(ld::Atom const* const&) + 40
          6  0x106682999  ld::tool::SymbolTable::findSlotForReferences(ld::Atom const*, ld::Atom const**) + 127
          7  0x10668b4d0  ld::tool::Resolver::convertReferencesToIndirect(ld::Atom const&) + 190
          8  0x10668b0fd  ld::tool::Resolver::doAtom(ld::Atom const&) + 1519
          9  0x106630ebf  mach_o::relocatable::File<arm64>::forEachAtom(ld::File::AtomHandler&) const + 57
          10  0x10667de8e  ld::tool::InputFiles::forEachInitialAtom(ld::File::AtomHandler&, ld::Internal&) + 842
          11  0x10668ea5a  ld::tool::Resolver::resolve() + 44
          12  0x106614f85  main + 334
          A linker snapshot was created at:
              /tmp/rust_selector_example-2020-11-13-211329.ld-snapshot
          ld: Assertion failed: (0 && "unsupported reference to selector"), function targetCString, file /Library/Caches/com.apple.xbs/Sources/ld64/ld64-609/src/ld/parsers/macho_relocatable_file.cpp, line 6626.
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
          

error: aborting due to previous error

error: could not compile `rust-selector-example`

To learn more, run the command again with --verbose.

Related issue: nvzqz/fruity#2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.O-macosOperating system: macOS

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions