Skip to content

Static items can result in references to unexported symbols #13620

Closed
@bkoropoff

Description

@bkoropoff

Scenario

  • Crate 1 contains a struct with an extern fn field
  • Crate 1 contains a pub static item of that struct, with the field being initialized to a priv extern fn
  • Crate 2 contains a static item of the same struct which is initialized to the static item in crate 1
  • All crates are built as dylibs, and a program is linked against crate 2

Result

Crate 2 ends up referring directly to the priv extern fn in crate 1, which is not in the dynamic symbol table for the crate. The program fails to link.

Code

crate1.rs

pub struct Foo {
    pub foo: extern fn()
}

extern fn the_foo() {}

pub static FOO: Foo = Foo {
    foo: the_foo
};

crate2.rs

extern crate crate1;

pub static FOO2: crate1::Foo = crate1::FOO;

main.rs

extern crate crate2;

fn main() {
    (crate2::FOO2.foo)();
}

Makefile

CRATE1=$(shell rustc --crate-type=dylib --crate-file-name crate1.rs)
CRATE2=$(shell rustc --crate-type=dylib --crate-file-name crate2.rs)

all: main

$(CRATE1): crate1.rs
    rustc --crate-type=dylib $<

$(CRATE2): crate2.rs $(CRATE1)
    rustc --crate-type=dylib -L. $<

main: main.rs $(CRATE2)
    rustc -L. $<

clean:
    rm -f *.so main

.PHONY: clean all

Output

Excerpt of error:

....
note: .../rust-bug/libcrate2-c9f1a15a-0.0.so: error: undefined reference to 'the_foo::h33cb9568e97d6893haa::v0.0'

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binaries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions