Skip to content

AVR: linker failure - architecture of input file is incompatible #137739

Closed
@tones111

Description

@tones111
Contributor

rustc AVR support recently moved to an avr-none target (#131651). While trying to update the avr-hal crate (Rahix/avr-hal#634) I discovered building for some cpus results in a linker error.

I've created a dependency-free reproducer at https://github.com/tones111/avr_bug

# rust-toolchain.toml
[toolchain]
channel = "nightly-2025-02-26"
components = [ "rust-src" ]
profile = "minimal"
# .cargo/config.toml
[build]
target = "avr-none"

# linking works
#rustflags = ["-C", "target-cpu=atmega328p"]

# avr architecture of input file `/tmp/rustcvmgbHY/symbols.o' is incompatible with avr:51 output
rustflags = ["-C", "target-cpu=atmega1280"]

[unstable]
build-std = ["core"]
# Cargo.toml
[package]
name = "avr-bug"
edition = "2021"

[profile.release]
panic = "abort"
codegen-units = 1
lto = true
opt-level = "s"
// src/main.rs
#![no_std]
#![no_main]

#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
    loop {}
}

#[export_name = "main"]
pub unsafe extern "C" fn __main() {
    loop {}
}

Attempting to build ( cargo build --release) results in a linker failure like

   Compiling avr-bug v0.0.0 (/home/paul/src/avr_bug)
error: linking with `avr-gcc` failed: exit status: 1
  |
  = note:  "avr-gcc" "/tmp/rustcpXMkWs/symbols.o" "<1 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "/home/paul/src/avr_bug/target/avr-none/release/deps/{librustc_std_workspace_core-58b27ec79d4fc03b.rlib,libcore-e5aabaa098bae4b6.rlib,libcompiler_builtins-7eca6f593cadd3a5.rlib}.rlib" "-Wl,-Bdynamic" "-lgcc" "-Wl,-z,noexecstack" "-o" "/home/paul/src/avr_bug/target/avr-none/release/deps/avr_bug-29daed87aec90e75.elf" "-Wl,--gc-sections" "-no-pie" "-mmcu=atmega1280" "-Wl,-O1" "-Wl,--strip-debug" "-nodefaultlibs"
  = note: some arguments are omitted. use `--verbose` to show all linker arguments
  = note: /usr/bin/avr-ld: avr architecture of input file `/tmp/rustcpXMkWs/symbols.o' is incompatible with avr:51 output

Building for atmega328p works as expected. Adding -C default-linker-libraries doesn't change the behavior. Targeting atmega2560 is also problematic.

tool versions
avr-gcc: 14.2.0
avr-libc: 2.2.1
avr-ld: GNU ld (GNU Binutils) 2.43
rustc: nightly-2025-02-26

Activity

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Feb 27, 2025
bjorn3

bjorn3 commented on Feb 27, 2025

@bjorn3
Member

ef_avr_arch(&sess.target.options.cpu)
probably needs to be updated to respect -Ctarget-cpu.

added
A-linkageArea: linking into static, shared libraries and binaries
on Feb 27, 2025
tones111

tones111 commented on Feb 27, 2025

@tones111
ContributorAuthor

I'd like to investigate but I'm having trouble figuring out how to build a version of the compiler with avr-none support (first time trying to build from source). I see AVR listed as an experimental target but when I try to enable it I get an error.

# config.toml
profile = "compiler"
change-id = 137215

[build]
target = ["x86_64-unknown-linux-gnu", "avr-none"]
$ ./x build rustc
...
thread 'main' panicked at src/bootstrap/src/core/sanity.rs:270:17:
No such target exists in the target list,
make sure to correctly specify the location of the JSON specification file for custom targets!
Use BOOTSTRAP_SKIP_TARGET_SANITY=1 to bypass this check.

exporting BOOTSTRAP_SKIP_TARGET_SANITY=1 allows building stage1 and stage2 compilers, but when I try to build for AVR I get

   Compiling avr-bug v0.0.0 (/home/paul/src/avr_bug)
error[E0463]: can't find crate for `core`
  |
  = note: the `avr-none` target may not be installed
  = help: consider adding the standard library to the sysroot with `x build library --target avr-none`

I haven't found any other information on how to enable support for these experimental targets. Any tips to get an avr-none supported rustc are greatly appreciated.

bjorn3

bjorn3 commented on Feb 27, 2025

@bjorn3
Member

I think you will want to omit avr-none when building rustc and then use build-std like you did with the official toolchain builds except this time with your locally built compiler.

added a commit that references this issue on Feb 28, 2025
4c1f51b
added 2 commits that reference this issue on Mar 1, 2025

Rollup merge of rust-lang#137830 - LuigiPiucco:incompatible-isa-fix, …

04ede0c

Rollup merge of rust-lang#137830 - LuigiPiucco:incompatible-isa-fix, …

878f383
tones111

tones111 commented on Mar 1, 2025

@tones111
ContributorAuthor

Thanks @bjorn3 for confirming the workflow. This helped me determine my issue was with cargo. From the rustc dev guide:

Note: the toolchain we've built does not include cargo. In this case, rustup will fall back to using cargo from the installed nightly, beta, or stable toolchain (in that order). If you need to use unstable cargo flags, be sure to run rustup install nightly if you haven't already.

After running rustup install nightly cargo is able to build core for avr-none and I'm able to see my compiler changes. Thanks to @LuigiPiucco for beating me to it. I'll bump my AVR pull requests once the fix hits nightly.

removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Mar 1, 2025

1 remaining item

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @tones111@bjorn3@rustbot@Noratrieb

      Issue actions

        AVR: linker failure - architecture of input file is incompatible · Issue #137739 · rust-lang/rust