英文:
Rust debug build linking fails whereas release build passes and on macos both passes
问题
新手学习Rust,在Ubuntu上尝试构建一个调试版本的二进制文件,但是遇到以下链接错误:
使用`cc`链接失败:退出状态:1
|
= 注意:LC_ALL="C" PATH="/home/my_home/.rustup/toolchains/nightly-2023-05-31-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/my_home/my_project-2-dev/bin:/home/my_home/.nvm/versions/node/v16.19.1/bin:/home/my_home/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin" VSLANG="1033" "cc" "-m64" "/tmp/rustcsg0duh/symbols.o" "/home/my_home/my_project-2-dev/my_service/target/debug/deps/my_project_my_service_abc-8aad4b2b83ae80f6.10vvqzkopsquoi13.rcgu.o" "/home/my_home/my_project-2-dev/my_service/target/debug/deps/my_project_my_service_abc-8aad4b2b83ae80f6.11rgc8keoer74gis.rcgu.o" "/home/my_home/my_project-2-dev/my_service/target/debug/deps/my_project_my_service_abc-8aad4b2b83ae80f6.12a9dvufwkt6aflp.rcgu.o" "/home/my_home/my_project-2-dev/my_service/target/debug/deps/my_project_my_service_abc-8aad4b2b83ae80f6.14sl35u4afv70bhq.rcgu.o" "/home/my_home/my_project-2-dev/my_service/target/debug/deps/my_project_my_service_abc-8aad4b2b83ae80f6.15avg65pxqdjpcwx.rcgu.o"
= 注意:/home/my_home/.rustup/toolchains/nightly-2023-05-31-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-a8a9ba95b1228749.rlib(std-a8a9ba95b1228749.std.97c55777bef24d10-cgu.0.rcgu.o):(.debug_info+0x12):重定位截断以适应:R_X86_64_32 对 `.debug_str' 的重定位
/home/my_home/.rustup/toolchains/nightly-2023-05-31-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-a8a9ba95b1228749.rlib(std-a8a9ba95b1228749.std.97c55777bef24d10-cgu.0.rcgu.o):(.debug_info+0x73):附加重定位溢出省略
collect2: error: ld 返回 1 退出状态
我的Ubuntu版本如下:
cc --version
cc (Ubuntu 11.3.0-1ubuntu1~22.04.1) 11.3.0
ld --version
GNU ld (GNU Binutils for Ubuntu) 2.38
而在macOS上,调试版本和发布版本都能通过构建:
cc --version
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: arm64-apple-darwin22.5.0
我怀疑问题出在Ubuntu上的链接器上,不确定如何进行故障排除,请帮忙,非常感谢!
英文:
New to rust, trying to build a debug version for a binary in ubuntu which fails with below linking error:
linking with `cc` failed: exit status: 1
|
= note: LC_ALL="C" PATH="/home/my_home/.rustup/toolchains/nightly-2023-05-31-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/my_home/my_project-2-dev/bin:/home/my_home/.nvm/versions/node/v16.19.1/bin:/home/my_home/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin" VSLANG="1033" "cc" "-m64" "/tmp/rustcsg0duh/symbols.o" "/home/my_home/my_project-2-dev/my_service/target/debug/deps/my_project_my_service_abc-8aad4b2b83ae80f6.10vvqzkopsquoi13.rcgu.o" "/home/my_home/my_project-2-dev/my_service/target/debug/deps/my_project_my_service_abc-8aad4b2b83ae80f6.11rgc8keoer74gis.rcgu.o" "/home/my_home/my_project-2-dev/my_service/target/debug/deps/my_project_my_service_abc-8aad4b2b83ae80f6.12a9dvufwkt6aflp.rcgu.o" "/home/my_home/my_project-2-dev/my_service/target/debug/deps/my_project_my_service_abc-8aad4b2b83ae80f6.14sl35u4afv70bhq.rcgu.o" "/home/my_home/my_project-2-dev/my_service/target/debug/deps/my_project_my_service_abc-8aad4b2b83ae80f6.15avg65pxqdjpcwx.rcgu.o"
= note: /home/my_home/.rustup/toolchains/nightly-2023-05-31-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-a8a9ba95b1228749.rlib(std-a8a9ba95b1228749.std.97c55777bef24d10-cgu.0.rcgu.o):(.debug_info+0x12): relocation truncated to fit: R_X86_64_32 against `.debug_str'
/home/my_home/.rustup/toolchains/nightly-2023-05-31-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-a8a9ba95b1228749.rlib(std-a8a9ba95b1228749.std.97c55777bef24d10-cgu.0.rcgu.o):(.debug_info+0x73): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status
my versions on ubuntu are:
cc --version
cc (Ubuntu 11.3.0-1ubuntu1~22.04.1) 11.3.0
ld --version
GNU ld (GNU Binutils for Ubuntu) 2.38
whereas on macos both the debug and the release build both passes.
cc --version
Apple clang version 14.0.3 (clang-1403.0.22.14.1)
Target: arm64-apple-darwin22.5.0
My suspicion is the issue is with the linker on ubuntu, not sure how to troubleshoot, any help is highly appreciated?
答案1
得分: 2
默认情况下,在x86-64架构上,编译器会为重定位(relocation)保留32位,重定位是一个用于查找对象的地址。然而,在你的情况下,对象距离超过4 GiB,因此无法使用32位的重定位。这篇文章很好地解释了这个问题。
你在Linux的调试模式下看到这个问题而在发布模式下没有看到的原因可能是因为在发布模式下,(a)你没有调试信息,这在二进制文件中占用空间,以及(b)通常代码更小,因为它经过了更多的优化。你在macOS上没有看到这个问题的原因是因为你使用的是不同的处理器类型,而arm64允许不同大小的重定位(例如,33位的重定位,可能足够大以避免触发这个问题)。
你可以通过在编译Rust代码时使用-C code-model=large
来解决这个问题。可以通过设置RUSTFLAGS
环境变量来实现。这告诉编译器你的二进制文件非常大,需要在编译时为大型重定位保留空间,这应该可以解决这个问题。
英文:
By default, on x86-64, the compiler reserves 32 bits for a relocation, which is an address that can be used to find an object. However, in your case, the object is more than 4 GiB away, so a 32-bit relocation can't be used. This article explains the problem really well.
The likely reason you see this on Linux in debug mode and not release mode is because in release mode, (a) you don't have debugging information, which takes up space in the binary, and (b) typically the code is smaller because it's more optimized. The reason you don't see this on macOS is because you're using a different processor type, and arm64 allows different sizes of relocations (for example, 33-bit relocations, which might be large enough to not trigger the problem).
You can solve this problem by using -C code-model=large
when compiling your Rust code. That can be done with the RUSTFLAGS
environment variable. That tells the compiler that your binary is very large and you need it to reserve space for a large relocation when compiling, which should make this problem go away.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论