Repository navigation
GNU linker warns “corrupt .drectve” on staticlib binary generated with Rust 1.70 on Windows #112368
Description
It seems, as of version 16, LLVM generates a binary containing incompatible directive with GNU linker. I believe this is the cause:
commit: llvm/llvm-project@c5b3de6
Since Rust 1.70 upgraded LLVM to version 16, we started to see a lot of warnings by GNU linker when I link against staticlibs compiled by Rust. The warnings are like this:
Warning: .drectve `-exclude-symbols:_ZN7testpkg11hello_world17ha68eef8a416aa303E ' unrecognized
Warning: corrupt .drectve at end of def file
It seems I can just ignore this warning (while the concern described in the LLVM's commit can be a real problem in future), but still I feel this is something that should be fixed if possible. I know, from LLVM's viewpoint, it's reasonable to assume the linker is lld and embed the directives that only lld can understand. But, on the other hand, from Rust's viewpoint, is it intentional that the "GNU" target produces a binary that is incompatible with "GNU" linker? I'm honestly not sure about the answer. I'm yet to figure out what the "GNU" in Rust's GNU toolchain/target actually means.
Code
A minimal reproducible code: https://github.com/yutannihilation/rust170_gnu_warning
# compile Rust code to create a staticlib
cargo build --target=x86_64-pc-windows-gnu --lib --release
# compile the C code
gcc -c main.c -o main.o
# link to the staticlib
gcc -o testpkgrust170_gnu_warning.dll main.o -L./target/x86_64-pc-windows-gnu/release -lrust170_gnu_warning -lws2_32 -ladvapi32 -luserenv -lbcrypt -lntdll In case of Rust 1.69, we don't see any warnings. However, if it's compiled using Rust 1.70, GNU linker warns:
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
(This can be reproducible on GHA: result)
Version it worked on
It most recently worked on: Rust 1.69
Version with regression
rustc --version --verbose:
rustc 1.70.0 (90c541806 2023-05-31)
binary: rustc
commit-hash: 90c541806f23a127002de5b4038be731ba1458ca
commit-date: 2023-05-31
host: x86_64-pc-windows-msvc
release: 1.70.0
LLVM version: 16.0.2
Backtrace
References
- the original context: Fix compile error on Windows with Rust 1.70 extendr/rextendr#285 (comment)
- post on the Rust user forum before reporting here: https://users.rust-lang.org/t/corrupt-drectve-warning-on-x86-64-pc-windows-gnu-target-with-rust-1-70/94912
Activity
erikdesjardins commentedon Jun 8, 2023
Looks like support for the
-exclude-symbols.drectvewas added to GNU ld in this commit, and then added to lld in these two commits.Those changes were released in Binutils 2.40 (the latest) and LLVM 15, both fairly new versions. GNU ld < 2.40 will emit a warning if it sees this directive, and lld < 15 will fail to link.
yutannihilation commentedon Jun 8, 2023
Thank you so much! It really helps.
In our particular use case, we use a toolchain called Rtools, which is provided for compiling R packages, and, fortunately, the latest version of it uses binutils 2.40.
Actually, I confirmed I don't see these warnings after I updated the toolchain to the latest version (I didn't notice there's a newer version). So, this might be a less serious problem than I thought at first. I'm not sure if it's fine in general to require such a new version of binutils, though.
Jules-Bertholet commentedon Jun 10, 2023
@rustbot label -regression-untriaged +regression-from-stable-to-stable
saurik commentedon Jun 13, 2023
FWIW, I'm running into this issue with lld 14 and I'm definitely not a good position to trivially upgrade to lld 15+ (though I guess if I stick with 1.69.0 for another year or so the maintainers of my toolchain packages will manage to release one based on lld 15).
hydra commentedon Jun 15, 2023
I'm using the GNU LD linker for two reasons:
arm-none-eabi-ld.I too started seeing the warnings detailed above after upgrading the nightly version of rust I'm using.
Example warnings from gnu ld 2.34. (arm-none-eabi) and gnu ld 2.39.
At the time of writing the latest official GNU Arm Tools is 12.2.MPACBTI-Rel1 https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads which give you this version:
which is 2.40.
However, I still see all the warnings, as follows:
It's thus not correct to say that "GNU < 2.40" (see #112368 (comment)) will emit warnings, as
2.40.20230307certainly still emits them.I guess I'll have to wait for a new GNU Arm Tools release or build windows binaries somehow, neither of which are appealing. 😄
workingjubilee commentedon Jun 23, 2023
When you use a
-gnutarget, that is effectively meant to be, for most intents and purposes, "compatible with glibc". I have not really heard Rust team members discuss them in other terms. Obviously, there are some caveats here about dynamic loaders, but that isn't necessarily the same as the static linker.mati865 commentedon Jun 24, 2023
It's harmless warning. Previously binaries would contain unnecessary symbols and this newly introduced feature gets rid of them. Like already said here it's supported by GNU toolchain (since Binutils 2.40)
Most importantly artifacts produced by Rust and GNU toolchain (GCC+Binutils+mingw-w64) are compatible with each other. Linking and the resulting binaries still work but a warning is shown when Binutils are too old to support feature used by LLVM. Surely this is not ideal (maybe there is a way to silence this warning from
ld.bfd?) but I don't believe they are incompatible.2.40 does contain the required changes, either you have some environment issue or their version is not 2.40.
Using 2.40 Binutils from MSYS2:
When speaking of windows-gnu target it's never about glibc but a GNU toolchain so GCC and Binutils (or compatible one like LLVM) coupled with MinGW.
yutannihilation commentedon Jun 24, 2023
Thanks for the detailed explaination!
I now agree with you.
Since my problem is almost solved, I'm fine to close this issue. The only two reasons I keep this open are (1) I don't have enough knowledge to judge if this is a negligible issue (2) those who suffer from this warning might find this easier. Should I close this issue?
Regarding #112368 (comment), I also confirmed 2.40 contains the required change. So, it seems it's a problem on Arm's GCC toolchain's side, not on Rust's side.
27 remaining items
Zxilly commentedon Jul 29, 2024
@mati865 Thanks, but I didn't have any fixes in mind, so I simply removed the gnu target. This issue came up after the 1.70 release, but I can't reproduce it locally.
mati865 commentedon Jul 29, 2024
You probably have a different Binutils version locally and it doesn't crash.
Rust upgrade is not very relevant, Rust output has changed a bit in a way that causes this crash but a similar crash could also happen when you modify your code in a specific way.
Zxilly commentedon Jul 29, 2024
For binutils I have 2.42 locally, and https://github.com/actions/runner-images/blob/main/images/windows/Windows2022-Readme.md shows the CI have the binutils 2.39. I'll continue to delve into this when I can.
hsn10 commentedon Jul 29, 2024
rust won't pick locally installed binutils automatically because auto detection code there can detect only one special distribution and that distribution ships outdated binutils.
niXman/mingw-builds-binaries#68
mati865 commentedon Jul 29, 2024
Rust will pick any linker from the PATH that is named
<arch>-w64-mingw32-gccso it's compatible with most, if not all distributions.Auto merge of rust-lang#119229 - mati865:update-mingw-toolchain, r=<try>
Auto merge of rust-lang#119229 - mati865:update-mingw-toolchain, r=<try>
Rollup merge of rust-lang#119229 - mati865:update-mingw-toolchain, r=…
Rollup merge of rust-lang#119229 - mati865:update-mingw-toolchain, r=…
Rollup merge of rust-lang#119229 - mati865:update-mingw-toolchain, r=…
Rollup merge of rust-lang#119229 - mati865:update-mingw-toolchain, r=…
Rollup merge of rust-lang#119229 - mati865:update-mingw-toolchain, r=…
Unrolled build for rust-lang#119229
Rollup merge of #119229 - mati865:update-mingw-toolchain, r=jieyouxu,…