Skip to content

Extern mod'd shared objects do not respect visibility of structs (linker errors) #11287

Closed
@wilkie

Description

@wilkie

The following library, innocent enough, contains an allowed visibility conundrum:

// foo.rs
// Compiled with rustc --lib foo.rs
#[link(name = "foo", vers = "1.0", package_id = "foo")];

struct Foo { // ** should be public, but isn't, by human error ** //
  a: i8
}

impl Foo {
  pub fn bar(&self) {
  }
}

The following driver:

// bug.rs
// Compiled by rustc bug.rs -L.
extern mod foo;

fn main() {
  let f = foo::Foo { a: 0 };
  f.bar();
}

The compiler will believe Foo is public, and attempt to link the function bar (visible to the compiler, but not to the linker) causing the following linking error:

error: linking with `cc` failed: exit code: 1
note: cc arguments: '-m64' '-L/usr/local/lib/rustc/x86_64-unknown-linux-gnu/lib' '-o' 'bug' 'bug.o' '-Wl,--as-needed' '-L.' '-L/home/wilkie/rust-bug/.rust' '-L/home/wilkie/rust-bug' '-L/usr/local/lib/rustc/x86_64-unknown-linux-gnu/lib' '-lstd-6425b930ca146ae9-0.9-pre' '-L/usr/local/lib/rustc/x86_64-unknown-linux-gnu/lib' '-lrustuv-a13edc95d75df17-0.9-pre' '-L.' '-lfoo-167086a786d07119-1.0' '-lpthread' '-lrt' '-ldl' '-lm' '-lpthread' '-lstdc++' '-lmorestack' '-Wl,-rpath,$ORIGIN/../../../usr/local/lib/rustc/x86_64-unknown-linux-gnu/lib' '-Wl,-rpath,$ORIGIN/.' '-Wl,-rpath,/usr/local/lib/rustc/x86_64-unknown-linux-gnu/lib' '-Wl,-rpath,/home/wilkie/rust-bug'
note: bug.o: In function `main::h7f81f851c09f62ah::v0.0':
bug.rc:(.text+0x3d): undefined reference to `Foo::bar::hbf8f903627fe5f220fad::v1.0'
collect2: error: ld returned 1 exit status

error: aborting due to previous error

If I readelf -Ws libfoo-167086a786d07119-1.0.so, that exact symbol does exist with the correct hash.

If I put 'pub' in front of the struct definition, it works as expected.

I'm compiling a new version from master and will report later, but right now my rust --version is:

rustc 0.9-pre (3cc86d8 2013-12-05 12:32:12 -0800)
host: x86_64-unknown-linux-gnu

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions