Search code examples
rustembeddedavr-gcc

My `avr_hal` rust project fails to link with `libc_alloc` if I trying to use something using `alloc` crate


My problem: when I trying to cargo build my avr_hal project it throws error: linking with avr-gcc failed: exit status: 1.

Full error text (mre):

WARN rustc_codegen_ssa::back::link Linker does not support -no-pie command line option. Retrying without.
error: linking with `avr-gcc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/usr/local/rustup/toolchains/nightly-2023-08-08-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/vscode/vscode-server/bin/linux-x64/74f6148eb9ea00507ec113ec51c489d6ffb4b771/bin/remote-cli:/usr/local/cargo/bin:/usr/local/cargo/bin:/usr/local/cargo/bin:/usr/local/cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/vscode/.local/bin" VSLANG="1033" "avr-gcc" "-mmcu=atmega328p" "/tmp/rustcfZfdOa/symbols.o" "/workspaces/avrgcc_linking_fail_mre/avrgcc-linking-failed-mre/target/avr-atmega328p/debug/deps/avrgcc_linking_failed_mre-4f3e20c93dc596f3.arduino_hal-6541d5c2c9fe98bb.arduino_hal.fa13f7103336eb93-cgu.0.rcgu.o.rcgu.o" "-Wl,--as-needed" "-L" "/workspaces/avrgcc_linking_fail_mre/avrgcc-linking-failed-mre/target/avr-atmega328p/debug/deps" "-L" "/workspaces/avrgcc_linking_fail_mre/avrgcc-linking-failed-mre/target/debug/deps" "-L" "/usr/local/rustup/toolchains/nightly-2023-08-08-x86_64-unknown-linux-gnu/lib/rustlib/avr-atmega328p/lib" "-Wl,-Bstatic" "/workspaces/avrgcc_linking_fail_mre/avrgcc-linking-failed-mre/target/avr-atmega328p/debug/deps/libcompiler_builtins-400b15d51260279e.rlib" "-Wl,-Bdynamic" "-lgcc" "-Wl,-z,noexecstack" "-L" "/usr/local/rustup/toolchains/nightly-2023-08-08-x86_64-unknown-linux-gnu/lib/rustlib/avr-atmega328p/lib" "-o" "/workspaces/avrgcc_linking_fail_mre/avrgcc-linking-failed-mre/target/avr-atmega328p/debug/deps/avrgcc_linking_failed_mre-4f3e20c93dc596f3.elf" "-Wl,--gc-sections"
  = note: /workspaces/avrgcc_linking_fail_mre/avrgcc-linking-failed-mre/target/avr-atmega328p/debug/deps/avrgcc_linking_failed_mre-4f3e20c93dc596f3.arduino_hal-6541d5c2c9fe98bb.arduino_hal.fa13f7103336eb93-cgu.0.rcgu.o.rcgu.o: In function `_$LT$libc_alloc..LibcAlloc$u20$as$u20$core..alloc..global..GlobalAlloc$GT$::alloc::hcb8d6e5a6803e71c':
          /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/libc_alloc-1.0.5/src/lib.rs:35: undefined reference to `memalign'
          /workspaces/avrgcc_linking_fail_mre/avrgcc-linking-failed-mre/target/avr-atmega328p/debug/deps/avrgcc_linking_failed_mre-4f3e20c93dc596f3.arduino_hal-6541d5c2c9fe98bb.arduino_hal.fa13f7103336eb93-cgu.0.rcgu.o.rcgu.o: In function `alloc::raw_vec::finish_grow::h0af711d7dce70706':
          arduino_hal.fa13f7103336eb93-cgu.0:(.text._ZN5alloc7raw_vec11finish_grow17h0af711d7dce70706E+0xb2): undefined reference to `memalign'
          collect2: error: ld returned 1 exit status
          
  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

What causes this and how to fix it? (Is it possible to define global allocator while using avr_hal in general?)

Steps to reproduce (mre)

  1. install cargo-generate - cargo install cargo-generate

  2. run cargo generate --git https://github.com/rahix/avr-hal-template.git --name avrgcc-linking-failed-mre (I am using Uno)

  3. add avrgcc-linking-failed-mre/.cargo/config.toml

[build]
target = "avr-specs/avr-atmega328p.json"

[unstable]
build-std = ["core", "alloc"]
  1. add avrgcc-linking-failed-mre/src/main.rs
#![no_std]
#![no_main]

use libc_alloc::LibcAlloc;
use panic_halt as _;

extern crate alloc;


#[arduino_hal::entry]
fn main() -> ! {
    "123".replace("1", "4");
    loop {}
}

#[global_allocator]
static ALLOCATOR: LibcAlloc = LibcAlloc;
  1. add libc_alloc = "1.0.5" to deps in avrgcc-linking-failed-mre/Cargo.toml

If you don't want to use devcontainers, see avr_hal's quickstart

  1. add .devcontainer/devcontainer.json
{
    "name": "avrgcc_linking_fail_mre_devcontainer",
    "dockerFile": "Dockerfile"
}
  1. add .devcontainer/Dockerfile
FROM mcr.microsoft.com/devcontainers/rust:1-1-bullseye

RUN apt update

RUN apt install -y fish
RUN apt install -y avr-libc gcc-avr pkg-config avrdude libudev-dev build-essential

RUN cargo install cargo-generate
  1. build devcontainer

  2. build in devcontainer cd avrgcc-linking-failed-mre, cargo build | cargo build --release


Solution

  • That's because alloc isn't available on the avr micros, nor is it a good idea to have dynamic allocation on them the linked article lists and expands on the following reasons which also apply to Rust:

    • Non-deterministic allocation at run-time.
    • Memory might not be available.
    • Waste of space.
    • Heap memory fragmentation.
    • Steep execution overhead.
    • User data memory fragmentation.
    • Unspecified amount of memory allocated.
    • "Saving memory" and freeing doesn't make sense.
    • Memory leaks.
    • Dangling pointers.
    • Indeterminate values.
    • Indeterminate types.

    So the non-solution solution is to remove alloc and anything using it from the build-std list and all dependencies.