Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upLinking dylib fails in some cases, "undefined reference to `rust_begin_unwind" #18807
Comments
This comment has been minimized.
This comment has been minimized.
|
Duplicating all rlibs on the command line makes the error go away. Which means this is a link ordering issue between Rust rlibs. |
This comment has been minimized.
This comment has been minimized.
|
I believe that this is because the program here isn't actually using any symbols from the standard library. We do actually do a topological sort of the libs on the command line. The linker command line looks like:
However, this program doesn't actually use any symbols from the standard library's rlib, so the linker actually discards The best way to solve this... I'm not entirely sure! I've thought in the past that each object generated by rustc needs a symbol which can be referenced by objects to guarantee the linker doesn't strip any of them, which would definitely help here but is slightly heavy-handed... |
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton, if we topologically sort them, shouldn't -lstd have come after -lcore? I thought we simply piled all upstream libraries onto linker command line. Can you point me to the code that does topo-sorting? |
This comment has been minimized.
This comment has been minimized.
|
My current understanding of linkers leads me to believe that we need a topological sorting with the outermost dependencies at the far left and their own dependencies to the right. For example, if I have a crate c1 that links to c2 which links to c3, we have two options:
|
This comment has been minimized.
This comment has been minimized.
|
Oops, posted too soon! Anyway, let's assume that c1 calls c2 functions, but does not call any c3 functions. We do know, however, that c2 calls c3 functions. This means that in the first example, there are outstanding undefined references when In the second case, when the linker looks at The current topo-sort is here: rust/src/librustc/metadata/cstore.rs Lines 156 to 184 in 351f7af |
This comment has been minimized.
This comment has been minimized.
|
I think linker works ever simpler than that: it goes through the libraries on the command line left-to-right, and at each step tries to resolve currently outstanding symbols. It never examines libraries to the left of the current one. |
This comment has been minimized.
This comment has been minimized.
|
@alexcrichton, I wonder if we have circular dependencies in this case. Does anything in std depend on core? |
This comment has been minimized.
This comment has been minimized.
|
You can't actually encode a circular dependency in Rust itself, but std/core do have a circular relationship where std depends heavily on core for all its symbols and core depends on the "weak lang items" coming from std (aka rust_begin_unwind). That's the source of the problem here sadly :( |
This comment has been minimized.
This comment has been minimized.
|
If it's a one-off, I suppose it doesn't make sense to build the logic for dealing with circular dependencies into |
This comment has been minimized.
This comment has been minimized.
|
Yeah I'm ok with adding a linker flag for specifically symbol for now. |
This comment has been minimized.
This comment has been minimized.
|
No luck, |
This comment has been minimized.
This comment has been minimized.
|
@roeyskatt, just make sure that you call |
This comment has been minimized.
This comment has been minimized.
|
@vadimcn hehe, thanks :) |
steveklabnik
added
A-linkage
O-windows
labels
Jan 27, 2015
This comment has been minimized.
This comment has been minimized.
|
I have the same issue when not building with Cargo, without rlibs, on handmade_no_std. Wait, nevermind, I'm just being dumb. |
alexcrichton
referenced
this issue
in alexcrichton/rust
Apr 24, 2015
alexcrichton
referenced this issue
May 13, 2015
Closed
Linking error on Windows when recreating a slice from raw parts #25369
This comment has been minimized.
This comment has been minimized.
domgetter
commented
May 14, 2015
|
Workaround is to add println!(""); in a spare function you never use. This somehow forces the compiler to link things correctly, as far as I can tell. #[no_mangle]
pub extern "C" fn mandel(x: f32, y: f32, dwell: i32) -> i32 {
//your code here
}
#[allow(dead_code)]
fn spare() { println!(""); } //adding this (doesn't have to be named "spare") makes the compilation work.
// you don't even need to add this function signature where you're using these functions. |
alexchandel
referenced this issue
May 29, 2015
Closed
Linking a dylib/DLL is broken on windows #25891
DanielKeep
added a commit
to DanielKeep/rust-ffi-omnibus
that referenced
this issue
Jun 28, 2015
Kingsquee
referenced this issue
Jul 2, 2015
Closed
Linking issues using Cargo-built libraries from non-cargo projects. #1777
This comment has been minimized.
This comment has been minimized.
|
I'm having the same issue on Linux with cargo with a setup like the following: Library crate #![no_std]
pub fn do_panic() -> ! {
panic!("test")
}Bin crate: #![no_main]
extern crate corepanic;
#[no_mangle]
pub extern fn main(_argc: i32, _argv: *const *const u8) -> i32 {
corepanic::do_panic()
}@domgetter's workaround works in this case too |
This comment has been minimized.
This comment has been minimized.
Michael-F-Bryan
commented
Oct 22, 2016
|
I'm currently playing around with calling Rust code from a bunch of different languages and was having the same problem (Ubuntu Linux). The workaround @domgetter mentioned of using something from the std library to make sure it's linked in works. I had to Without the $ rustc --version
rustc 1.14.0-nightly (16eeeac78 2016-10-18)
$ rustc prime.rs -O --crate-type cdylib
$ nm libprime.rs
00000000000183b0 T is_primeAnd with the $ nm libprime.rs
000000000004c150 T is_prime
00000000000872c0 T __rust_allocate
...
00000000000811f0 T __rust_start_panic
000000000004c1d0 T spareAre there any special flags you can pass into rustc/cargo in order to make sure it links in the necessary things from std? |
This comment has been minimized.
This comment has been minimized.
|
This indeed is no longer a Windows-specific issue. I believe that the introduction of @jethrogb and @Michael-F-Bryan, could you help me confirm my suspicion? Were you using @alexcrichton, would you mind updating the labels to reflect this change? |
alexcrichton
removed
the
O-windows
label
Nov 6, 2016
This comment has been minimized.
This comment has been minimized.
|
Certainly! |
philippkeller
pushed a commit
to philippkeller/apue-rust
that referenced
this issue
Dec 21, 2016
This comment has been minimized.
This comment has been minimized.
philippkeller
commented
Dec 21, 2016
|
@shepmaster: yes I'm seeing that on Linux as well now:
Linux version: I started with the second example in of no stdlib but needed to remove |
alexcrichton
referenced this issue
Apr 29, 2017
Closed
`rust_begin_unwind` required for panic=abort #38281
urschrei
added a commit
to urschrei/polylabel-rs
that referenced
this issue
Apr 29, 2017
Mark-Simulacrum
changed the title
Linking dylib fails in some cases on windows, "undefined reference to `rust_begin_unwind"
Linking dylib fails in some cases, "undefined reference to `rust_begin_unwind"
May 18, 2017
This comment has been minimized.
This comment has been minimized.
|
Confirmed; I hit this today when building a minimal cdylib library intended for calling from C. The |
roeyskatt commentedNov 9, 2014
Here's a minimal code example that reproduces the error:
And part of the error:
Version:
This also happens on the x86_64 nightly