Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking Rust binary using LD.LLD

I have Rust project that uses cargo-xbuild to cross-compile the core, alloc and compiler_builtins for ARM Cortex-M7 core using target specification (see below).

Up until now I used arm-none-eabi-gcc linker. This works out well.

As the ld.lld linker is mature enough I want to switch to it. Therefore I change the target specification. However, I get strange errors saying the linker-flavor is gnu and it also passes -nostartfiles which is not recognized as a parameter.

[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": ""
    }

[errors]

   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)

EDIT: Removing -nostartfiles causes Segmentation fault.

"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)
like image 639
phodina Avatar asked Jun 24 '26 19:06

phodina


1 Answers

So I found the issue - it was in the linker script. I defined memory region for FLASH to start at 0x08000000 and then in the .text I tried to locate the vector table to the beginning of FLASH so I added statement . = 0. The LLD crashes due to this statement, GCC doesn't.

like image 105
phodina Avatar answered Jun 28 '26 00:06

phodina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!