rust - 使用 LD.LLD 链接 Rust 二进制文件

标签 rust llvm rust-cargo

我有一个 Rust 项目,它使用 cargo-xbuild 交叉编译 ARM Cortex-M7 核心的核心、alloc 和 compiler_builtins,使用目标规范(见下文)。

到目前为止,我使用的是 arm-none-eabi-gcc 链接器。这很有效。

由于 ld.lld 链接器足够成熟,我想切换到它。因此我改变了目标规范。但是,我收到奇怪的错误,说链接器风格是 gnu 并且它还传递了 -nostartfiles ,它未被识别为参数。

[thumbv7em-none-eabi-gcc.json]

  "abi-blacklist": [
    "stdcall",
    "fastcall",
    "vectorcall",
    "thiscall",
    "win64",
    "sysv64"
  ],
  "arch": "arm",
  "data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64",
  "emit-debug-gdb-scripts": false,
  "env": "",
  "executables": true,
  "is-builtin": true,
  "linker": "arm-none-eabi-gcc",
  "linker-flavor": "gcc",
  "llvm-target": "thumbv7em-none-eabi",
    "pre-link-args": {
        "gcc" : [
        "-mcpu=cortex-m4", "-mthumb",
        "-nostartfiles", "-nostdlib",
            "-Tlayout.ld", "-ffreestanding"
            ]
    },
  "max-atomic-width": 32,
  "os": "none",
  "panic-strategy": "abort",
  "relocation-model": "static",
  "target-c-int-width": "32",
  "target-endian": "little",
  "target-pointer-width": "32",
  "vendor": ""
}

[thumbv7em-none-eabi-lld.json]

{
"abi-blacklist": [
        "stdcall",
        "fastcall",
        "vectorcall",
        "win64",
        "sysv64"
    ],
    "arch": "arm",
    "cpu" : "cortex-m4",
    "data-layout": "e-m:e-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64",
    "env": "",
    "executables": true,
    "linker-flavor": "ld.lld",
    "llvm-target": "thumbv7em-none-eabi",
    "max-atomic-width": 32,
    "os": "none",
    "panic-strategy": "abort",
    "link-args": {
        "ld.lld" : [
        "-Tlayout.ld"]
    },
    "relocation-model": "static",
    "target-endian": "little",
    "target-pointer-width": "32",
    "target-c-int-width": "32",
    "vendor": ""
    }

[错误]

   Compiling stm32f3 v0.1.0 (file:///home/cylon2p0/stm32f3)                                                                                                                                                                                        
     Running `rustc --crate-name stm32f3 src/main.rs --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=ecb4f772152518d9 -C extra-filename=-ecb4f772152518d9 --out-dir /home/cylon2p0/stm32f3/target/thumbv7em-none-eabi/debug/deps --target /home/cylon2p0/stm32f3/thumbv7em-none-eabi.json -C incremental=/home/cylon2p0/stm32f3/target/thumbv7em-none-eabi/debug/incremental -L dependency=/home/cylon2p0/stm32f3/target/thumbv7em-none-eabi/debug/deps -L dependency=/home/cylon2p0/stm32f3/target/debug/deps -C link-arg=-Tlayout.ld -C link-arg=-nostartfiles --sysroot /home/cylon2p0/stm32f3/target/sysroot`
error: linking with `lld` failed: exit code: 1======>                    ] 2/3: stm32f3
  |
  = note: "lld" "-flavor" "gnu" "-L" "/home/cylon2p0/stm32f3/target/sysroot/lib/rustlib/thumbv7em-none-eabi/lib" "/home/cylon2p0/stm32f3/target/thumbv7em-none-eabi/debug/deps/stm32f3-ecb4f772152518d9.1f6vm61vpmp6og7k.rcgu.o" "-o" "/home/cylon2p0/stm32f3/target/thumbv7em-none-eabi/debug/deps/stm32f3-ecb4f772152518d9" "--gc-sections" "-L" "/home/cylon2p0/stm32f3/target/thumbv7em-none-eabi/debug/deps" "-L" "/home/cylon2p0/stm32f3/target/debug/deps" "-L" "/home/cylon2p0/stm32f3/target/sysroot/lib/rustlib/thumbv7em-none-eabi/lib" "-Bstatic" "/home/cylon2p0/stm32f3/target/sysroot/lib/rustlib/thumbv7em-none-eabi/lib/libcore-08f9d20805b02a18.rlib" "/home/cylon2p0/stm32f3/target/sysroot/lib/rustlib/thumbv7em-none-eabi/lib/libcompiler_builtins-898d35b0b97ca347.rlib" "-Tlayout.ld" "-nostartfiles" "-Bdynamic"
  = note: lld: error: unknown argument: -nostartfiles


error: aborting due to previous error

error: Could not compile `stm32f3`.                                                                                                                                                                                                                

Caused by:
  process didn't exit successfully: `rustc --crate-name stm32f3 src/main.rs --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=ecb4f772152518d9 -C extra-filename=-ecb4f772152518d9 --out-dir /home/cylon2p0/stm32f3/target/thumbv7em-none-eabi/debug/deps --target /home/cylon2p0/stm32f3/thumbv7em-none-eabi.json -C incremental=/home/cylon2p0/stm32f3/target/thumbv7em-none-eabi/debug/incremental -L dependency=/home/cylon2p0/stm32f3/target/thumbv7em-none-eabi/debug/deps -L dependency=/home/cylon2p0/stm32f3/target/debug/deps -C link-arg=-Tlayout.ld -C link-arg=-nostartfiles --sysroot /home/cylon2p0/stm32f3/target/sysroot` (exit code: 101)

编辑: 删除 -nostartfiles 会导致段错误。

"lld" "-flavor" "ld.lld" "-L" "/home/cylon2p0/stm32f3/target/sysroot/lib/rustlib/thumbv7em-none-eabi/lib" "/home/cylon2p0/stm32f3/target/thumbv7em-none-eabi/debug/deps/stm32f3-ecb4f772152518d9.1f6vm61vpmp6og7k.rcgu.o" "-o" "/home/cylon2p0/stm32f3/target/thumbv7em-none-eabi/debug/deps/stm32f3-ecb4f772152518d9" "--gc-sections" "-L" "/home/cylon2p0/stm32f3/target/thumbv7em-none-eabi/debug/deps" "-L" "/home/cylon2p0/stm32f3/target/debug/deps" "-L" "/home/cylon2p0/stm32f3/target/sysroot/lib/rustlib/thumbv7em-none-eabi/lib" "-Bstatic" "/home/cylon2p0/stm32f3/target/sysroot/lib/rustlib/thumbv7em-none-eabi/lib/libcore-08f9d20805b02a18.rlib" "/home/cylon2p0/stm32f3/target/sysroot/lib/rustlib/thumbv7em-none-eabi/lib/libcompiler_builtins-898d35b0b97ca347.rlib" "-Tlayout.ld" "-Bdynamic"
LLVMSymbolizer: error reading file: No such file or directory
#0 0x00007f61706919cb llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/usr/bin/../lib/libLLVM-6.0.so+0x8759cb)
#1 0x00007f617068f886 llvm::sys::RunSignalHandlers() (/usr/bin/../lib/libLLVM-6.0.so+0x873886)
#2 0x00007f617068f9d3 (/usr/bin/../lib/libLLVM-6.0.so+0x8739d3)
#3 0x00007f616ea2e8f0 __restore_rt (/usr/bin/../lib/libc.so.6+0x368f0)
#4 0x00007f616f6c280e lld::elf::ARMExidxSentinelSection::empty() const (/usr/bin/../lib/liblldELF.so.6+0x14e80e)
#5 0x00007f616f707bd6 (/usr/bin/../lib/liblldELF.so.6+0x193bd6)
#6 0x00007f616f725be5 (/usr/bin/../lib/liblldELF.so.6+0x1b1be5)
#7 0x00007f616f7286f0 void lld::elf::writeResult<llvm::object::ELFType<(llvm::support::endianness)1, false> >() (/usr/bin/../lib/liblldELF.so.6+0x1b46f0)
#8 0x00007f616f613180 void lld::elf::LinkerDriver::link<llvm::object::ELFType<(llvm::support::endianness)1, false> >(llvm::opt::InputArgList&) (/usr/bin/../lib/liblldELF.so.6+0x9f180)
#9 0x00007f616f5d2155 lld::elf::LinkerDriver::main(llvm::ArrayRef<char const*>, bool) (/usr/bin/../lib/liblldELF.so.6+0x5e155)
#10 0x00007f616f616598 lld::elf::link(llvm::ArrayRef<char const*>, bool, llvm::raw_ostream&) (/usr/bin/../lib/liblldELF.so.6+0xa2598)
#11 0x000055c3a5b673a6 (lld+0x13a6)
#12 0x00007f616ea1b06b __libc_start_main (/usr/bin/../lib/libc.so.6+0x2306b)
#13 0x000055c3a5b67c0a (lld+0x1c0a)
Stack dump:
0.      Program arguments: lld -flavor ld.lld -L /home/cylon2p0/stm32f3/target/sysroot/lib/rustlib/thumbv7em-none-eabi/lib /home/cylon2p0/stm32f3/target/thumbv7em-none-eabi/debug/deps/stm32f3-ecb4f772152518d9.1f6vm61vpmp6og7k.rcgu.o -o /home/cylon2p0/stm32f3/target/thumbv7em-none-eabi/debug/deps/stm32f3-ecb4f772152518d9 --gc-sections -L /home/cylon2p0/stm32f3/target/thumbv7em-none-eabi/debug/deps -L /home/cylon2p0/stm32f3/target/debug/deps -L /home/cylon2p0/stm32f3/target/sysroot/lib/rustlib/thumbv7em-none-eabi/lib -Bstatic /home/cylon2p0/stm32f3/target/sysroot/lib/rustlib/thumbv7em-none-eabi/lib/libcore-08f9d20805b02a18.rlib /home/cylon2p0/stm32f3/target/sysroot/lib/rustlib/thumbv7em-none-eabi/lib/libcompiler_builtins-898d35b0b97ca347.rlib -Tlayout.ld -Bdynamic 
Segmentation fault (core dumped)

最佳答案

所以我发现了问题 - 它在链接描述文件中。我为 FLASH 定义了从 0x08000000 开始的内存区域,然后在 .text 中我试图将向量表定位到 FLASH 的开头,所以我添加了语句 。 = 0。 LLD 由于此声明而崩溃,而 GCC 则没有。

关于rust - 使用 LD.LLD 链接 Rust 二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51223564/

相关文章:

linq - C# Linq "join"在 Rust 中的等价物是什么?

rust - 如何使用工具链为 Linux 上的所有用户安装 rustup 和 cargo?

rust - 在句子中寻找单词

c++ - 使用 llvm::Linker 以编程方式查找未解析的外部对象

string - 在 Rust 中匹配 Option 静态字符串文字

objective-c - 对象由 "0 key/value pairs"表示,而不是 LLVM 中的内存地址

llvm - 如何在 LLVM 中创建一个 ConstantInt?

rust - 如何返回使用局部声明变量的结构实例

generics - 需要澄清关于 `Box` 、 `Vec` 和其他集合的(协)方差的 Rust Nomicon 部分

type-conversion - 在 Rust 中创建类型之间的自定义转换的标准方法是什么?