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 error on Windows when recreating a slice from raw parts #25369

Closed
pcwiek opened this issue May 13, 2015 · 5 comments
Closed

Linking error on Windows when recreating a slice from raw parts #25369

pcwiek opened this issue May 13, 2015 · 5 comments
Labels
A-linkage Area: linking into static, shared libraries and binaries O-windows Operating system: Windows

Comments

@pcwiek
Copy link

pcwiek commented May 13, 2015

Background

I wanted to expose a Rust library that would expose C-compatible API & ABI and would be used from C-like platform. What I need to do first is populate an array of structs.

Example

use std::slice;

#[repr(C)]
pub struct Item {
    num: i32,
    acc: i32,
}

#[no_mangle]
pub extern fn fill(arr: *mut Item, arr_size: i32) {
    let arr = unsafe { slice::from_raw_parts_mut(arr, arr_size as usize) };

    let mut sum = 0;

    for (i, item) in arr.iter_mut().enumerate() {
        item.num = (i + 1) as i32;
        item.acc = {sum += item.num; sum};
    }
}

Now when I try to compile that using latest Rust 1.0.0-beta5, I get linking errors:

error: linking with gcc failed: exit code: 1 note: "gcc" "-Wl,--enable-long-section-names" "-fno-use-linker-plugin" "-Wl,--nxcompat" "-Wl,--large-address-aware" "-shared-libgcc" "-L" "C:\Rust\bin\rustlib\i686-pc-windows-gnu\lib" "-o" "C:\Projects\Personal\ALR\target\debug\ALR.dll" "C:\Projects\Personal\ALR\target\debug\ALR.o" "C:\Projects\Personal\ALR\target\debug\ALR.metadata.o" "C:\Rust\bin\rustlib\i686-pc-windows-gnu\lib\libstd-4e7c5e5c.rlib" "C:\Rust\bin\rustlib\i686-pc-windows-gnu\lib\libcollections-4e7c5e5c.rlib" "C:\Rust\bin\rustlib\i686-pc-windows-gnu\lib\libunicode-4e7c5e5c.rlib" "C:\Rust\bin\rustlib\i686-pc-windows-gnu\lib\librand-4e7c5e5c.rlib" "C:\Rust\bin\rustlib\i686-pc-windows-gnu\lib\liballoc-4e7c5e5c.rlib" "C:\Rust\bin\rustlib\i686-pc-windows-gnu\lib\liblibc-4e7c5e5c.rlib" "C:\Rust\bin\rustlib\i686-pc-windows-gnu\lib\libcore-4e7c5e5c.rlib" "-L" "C:\Projects\Personal\ALR\target\debug" "-L" "C:\Projects\Personal\ALR\target\debug\deps" "-L" "C:\Rust\bin\rustlib\i686-pc-windows-gnu\lib" "-L" "C:\Projects\Personal\ALR\src.rust\bin\i686-pc-windows-gnu" "-L" "C:\Projects\Personal\ALR\src\bin\i686-pc-windows-gnu" "-Wl,--whole-archive" "-Wl,-Bstatic" "-Wl,--no-whole-archive" "-Wl,-Bdynamic" "-lws2_32" "-luserenv" "-shared" "-lcompiler-rt"
note:
C:\Rust\bin\rustlib\i686-pc-windows-gnu\lib\libcore-4e7c5e5c.rlib(core-4e7c5e5c.o):(.text+0x3ab4): undefined reference to rust_begin_unwind'
C:\Rust\bin\rustlib\i686-pc-windows-gnu\lib\libcore-4e7c5e5c.rlib(core-4e7c5e5c.o):(.eh_frame+0x5c7f): undefined reference to rust_eh_personality'
ld: C:\Rust\bin\rustlib\i686-pc-windows-gnu\lib\libcore-4e7c5e5c.rlib(core-4e7c5e5c.o): bad reloc address 0x5c7f in section `.eh_frame'

If I add empty println somewhere in the loop:

for (i, item) in arr.iter_mut().enumerate() {
    println!("");    
    item.num = (i + 1) as i32;
    item.acc = {sum += item.num; sum};
}

Linking actually works.

Unfortunately, when using the DLL in the third party platform, I'm getting stack overflow exception in the library (?!). That only happens when working with arrays though (iterating over a slice created from raw array, for example), passing a singular C struct by reference works just fine.

@barosl
Copy link
Contributor

barosl commented May 13, 2015

Nice to see an issue related to Windows be filed! Just to be complete, could you tell us what steps are needed to reproduce the bug? Also the contents of Cargo.toml.

@pcwiek
Copy link
Author

pcwiek commented May 13, 2015

Sure!

  1. Install x86 Rust for Windows (.exe)
  2. cargo new ALR
  3. Paste the code from the example example into lib.rs
  4. cargo build
  5. Wade through the crash dump :)

Cargo.toml contents:

[package]
name = "ALR"
version = "0.1.0"
authors = ["pcwiek <mail@redacted-i-dont-like-bots.com>"]

[lib]
name = "ALR"
crate-type = ["dylib"]

Cargo version

C:\Projects\Personal\ALR>cargo -V
cargo 0.2.0-nightly (efb482d 2015-04-30) (built 2015-04-30)

Rust version

C:\Projects\Personal\ALR>rustc -V
rustc 1.0.0-beta.5 (7b4ef47 2015-05-11) (built 2015-05-11)

@steveklabnik steveklabnik added A-linkage Area: linking into static, shared libraries and binaries O-windows Operating system: Windows labels May 13, 2015
@alexcrichton
Copy link
Member

I've often seen this problem arise when the target architecture for gcc/rustc don't match, are you sure that you're matching a 32/64bit rustc with the same bit-ness of gcc? Alternatively, are you running the compiler outside of MinGW (in which case the answer to the first question is yes!)

@pcwiek
Copy link
Author

pcwiek commented May 13, 2015

@alexcrichton Well, I just installed x86 Rust package, mostly because I need x86 DLL and I didn't want to fiddle with different target architecture when using x64 Windows package etc. I don't have any other MinGW/GCC installation, so I assumed that whatever came bundled with Rust was a-OK.

I use cargo to build/test the project, although I'm not exactly sure if that answers your second question :)

Also I'm not sure why linking actually passes after adding println!("") to the loop...

@alexcrichton
Copy link
Member

Ah I think I see what's going now, I believe this is a dupe of #18807, so closing in favor of that. Thanks for the report though!

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 O-windows Operating system: Windows
Projects
None yet
Development

No branches or pull requests

4 participants