Undefined external symbols from compiler_builtins #124042
Description
I have a project where I'm experimenting with introducing Rust code into an existing codebase by compiling it into a static library for a custom target and linking it using armips. This process no longer works on the latest nightly because armips complains about undefined external symbols such as _rnvntcse1eqnti5u6g_17compiler_builtins4math5atan2.
A minimal example is at https://github.com/fenhl/rust-builtins-min which can be tested by running python build.py after installing armips (scoop install extras/armips on Windows, see https://github.com/Kingcom/armips for binaries and build instructions for other platforms).
I ran cargo-bisect-rustc on the original project where I ran into this (not the minimal example, hence the different script in the command line) and it points to #123719 as the commit that introduced the regression.
cargo-bisect-rustc output
searched nightlies: from nightly-2022-06-21 to nightly-2024-04-16
regressed nightly: nightly-2024-04-15
searched commit range: 0bf471f...0d8b334
regressed commit: 29b1207
bisected with cargo-bisect-rustc v0.6.8
Host triple: x86_64-pc-windows-msvc
Reproduce with:
cargo bisect-rustc --start=2022-06-21 -c rust-src --script python -- .\cargo-bisect-rustc-test.py
Activity
Noratrieb commentedon Apr 17, 2024
rust-lang/compiler-builtins@0.1.108...0.1.109
I suspect it's rust-lang/compiler-builtins#577
Noratrieb commentedon Apr 17, 2024
@Amjad50 @Amanieu
Amjad50 commentedon Apr 17, 2024
Thanks for letting me know.
The changes in
0.1.109do not work as expected, I'm working on a fix here rust-lang/compiler-builtins#594, I'll test it with this project.Remove `weak-intrinsics` attribute
Apply `weak` attributes to all* intrinsics
Amjad50 commentedon Apr 29, 2024
So after looking around and trying to fix the issue, the main issue is from
armips, it doesn't handleweaksymbols correctlyAdded a quick small fix here, Kingcom/armips#242, but not sure if the maintainer is around anymore
Apply `weak` attributes to all intrinsics
Apply `weak` attributes to all intrinsics
Apply `weak` attributes to all intrinsics
Amjad50 commentedon May 1, 2024
So, the PR in
armipsis fixed, and now it can handleweaksymbols.Though, there appear to be another issue.
When running armips with the library from latest nightly, it gives an error that
memsetis not found.Looking at the
llvm-irforcompiler_builtins, it seems the functioncompiler_builtins::math::libm::rem_pio2_large::rem_pio2_largecallswhich is converted into
memsetfunction call for some reason.There is a way to fix this, which is to add
-Zbuild-std-features=compiler-builtins-memflag which would add the memory builtin functions.I'm not sure if that is the correct behavior of llvm or not. since the target in the example has
"os" = "none".This is a static library, so maybe its assuming
memsetwill be available when linking any executable?bjorn3 commentedon May 1, 2024
memcpy, memmove and memset are expected to be present by LLVM. They are either provided by libc (if any) or compiler-builtins (on no_std targets) Compiler-builtins has a hard coded set of no_std targets. For any other you have to explicitly use
-Zbuild-std-features=compiler-builtins-mem.https://github.com/rust-lang/compiler-builtins/blob/2cbde5b9538e2398716510b16b1b2c3d60d6b649/build.rs#L26-L36
Amjad50 commentedon May 1, 2024
Thanks @bjorn3, so I guess this is expected behavior.
I think then this ticket can be closed, the fix is to use the newer
armipsbuild.fenhl commentedon May 1, 2024
Can confirm this is fixed by using the new armips and adding
-Zbuild-std-features=compiler-builtins-memto the cargo command line. Thanks everyone for the help!Apply `weak` attributes to all intrinsics
Apply `weak` attributes to all intrinsics
Apply `weak` attributes to all intrinsics
Apply `weak` attributes to all intrinsics