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

cargo doesn't transiently pass undefined symbols #112446

Closed
DrewRidley opened this issue Jun 9, 2023 · 3 comments
Closed

cargo doesn't transiently pass undefined symbols #112446

DrewRidley opened this issue Jun 9, 2023 · 3 comments
Labels
A-linkage Area: linking into static, shared libraries and binaries T-cargo Relevant to the cargo team, which will review and decide on the PR/issue.

Comments

@DrewRidley
Copy link

DrewRidley commented Jun 9, 2023

Hey all,

I am compiling a fairly complex library from C# with NativeAOT into a statically linked binary. NativeAOT requires that at link time, a specific argument is included _NativeAOT_StaticInitialization. If I write a FFI crate to interop with this library, and add:
println!("cargo:rustc-link-args=-Wl,-u,_NativeAOT_StaticInitialization");
It seems to work at first glance. I wrote some unit tests to verify this behavior, and the unit tests pass. The undefined symbol is correctly included in the final binary and the NativeAOT code runs as expected.

Unfortunately, this behavior is not consistent when the same exact unit test is copied into a dependent crate. Adding the original FFI crate as a dependency seems to cause a segmentation fault which is due to the undefined symbol no longer existing in the binary.

I am not entirely sure how I can help, if it would be of assistance I can provide the specific statically linked library in question for reproduction? I suspect what happens is that when rust compiles the internal unit test, it directly links against the static lib into a binary without an intermediate rlib.

I was hoping someone could point me in the right direction towards a specific flag I can use here, or another tracking issue for a similar issue?

Any help or guidance on this matter would be much appreciated.
Thanks

@DrewRidley DrewRidley changed the title Rustc doesn't transiently pass linker "-u" Rustc doesn't transiently pass undefined symbols Jun 9, 2023
@workingjubilee
Copy link
Member

I thought the Cargo command is rustc-link-arg, not rustc-link-args? Do both work? That's interesting.

Nonetheless, your problem is not exactly a rustc one, it is a cargo one: rustc is not responsible for how cargo drives it, and while it does invoke a compiler for performing linkage, build.rs is a cargo feature, as is essentially everything concerning the multiple-crate ecosystem. But I believe the hesitation in passing things along is deliberate:

Based on scanning that thread, it seems the intent is that you should define a way for dependents to deliberately opt-in, rather than allowing you to muck up the linkage of other crates at will.

@workingjubilee workingjubilee changed the title Rustc doesn't transiently pass undefined symbols cargo doesn't transiently pass undefined symbols Jun 9, 2023
@Noratrieb Noratrieb added A-linkage Area: linking into static, shared libraries and binaries T-cargo Relevant to the cargo team, which will review and decide on the PR/issue. labels Jun 9, 2023
@DrewRidley
Copy link
Author

Thanks for getting back to me. This is definitely no fault of rust and its a rather peculiar situation, whereby microsoft decided to mandate that consumers of their static libraries produced through NativeAOT have a forced, undefined symbol to initialize statically.

For some reason, println!("cargo:rustc-link-args=-Wl,-u,_NativeAOT_StaticInitialization"); works for the local unit test in the FFI crate, but when copied over to the dependent library like described, it still falls flag. Removing the the s and making it link-arg seemed to fix the issue.

Not really sure why there would be any different between the two only in the dependent crate?
Regardless I now know the solution. Thank you for your help

@workingjubilee
Copy link
Member

Mildly vexing but not too surprising! Closing this as there is effectively "nothing to do" here, if a further request is desired a new issue can be opened at rust-lang/cargo's issue tracker.

@workingjubilee workingjubilee closed this as not planned Won't fix, can't repro, duplicate, stale Jun 9, 2023
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 T-cargo Relevant to the cargo team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants