You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

编译Rust自定义目标时遭遇链接器错误,重装Rust无效最终通过修复binutils解决

编译Rust自定义目标时遭遇链接器错误,重装Rust无效最终通过修复binutils解决

问题场景

我最近跟着教程尝试用Rust编写操作系统,使用自定义的x86_64-blog_os.json目标文件编译项目时,突然触发了链接器错误,具体报错内容如下:

error: linking with 'cc' failed: exit status: 1

|

= note: LC_ALL="C" PATH= ......

..... = note: /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/Scrt1.o: unrecognized relocation (0x2a) in section '.text'

/usr/bin/ld: final link failed: Bad value

collect2: error: ld returned 1 exit status

error: could not compile 'compiler_builtins' (build script) due to previous error

一开始我误以为是第一次安装Rust时中途中断安装脚本导致的问题,于是卸载并重装了Rust,但问题完全没有改善。后来我还发现,不管编译任何Rust项目都会出现这个错误,更新Rust nightly工具链也没能解决问题。

相关配置信息

我的自定义目标文件x86_64-blog_os.json内容:

{
"llvm-target": "x86_64-unknown-none",
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
"arch": "x86_64",
"target-endian": "little",
"target-pointer-width": "64",
"target-c-int-width": "32",
"os": "none",
"executables": true,
"linker-flavor": "ld.lld",
"linker": "rust-lld",
"panic-strategy": "abort",
"disable-redzone": true,
"features": "-mmx,-sse,+soft-float"
}

.cargo/config.toml配置:

[unstable]
build-std = ["core", "compiler_builtins"]

Cargo.toml配置:

[package]
name = "blog_os"
version = "0.1.0"
edition = "2018"

[dependencies]

使用的工具链:nightly-x86_64-unknown-linux-gnu (default),rustc版本1.72.0-nightly

最终解决方法

经过排查,发现问题根源在系统的binutils工具包上。只需要执行以下命令卸载并重装该工具包,就能彻底解决链接器错误:

sudo apt purge binutils
sudo apt install binutils

备注:内容来源于stack exchange,提问作者Mach

火山引擎 最新活动