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

时间:2018-07-07 13:12:27

标签: rust llvm rust-cargo

我有一个Rust项目,该项目使用cargo-xbuild使用目标规范交叉编译ARM Cortex-M7内核的内核,alloc和builder_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)

1 个答案:

答案 0 :(得分:0)

所以我发现了问题-它在链接脚本中。我为FLASH定义了从0x08000000开始的内存区域,然后在.text中试图将向量表定位到FLASH的开头,因此添加了语句. = 0。 LLD因该语句而崩溃,GCC则不然。