Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
tones111 opened this issue Feb 27, 2025 · 4 comments · Fixed by #137830
Closed

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

tones111 opened this issue Feb 27, 2025 · 4 comments · Fixed by #137830
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug.

Comments

@tones111
Copy link
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

@tones111 tones111 added the C-bug Category: This is a bug. label Feb 27, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 27, 2025
@bjorn3
Copy link
Member

bjorn3 commented Feb 27, 2025

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

@bjorn3 bjorn3 added the A-linkage Area: linking into static, shared libraries and binaries label Feb 27, 2025
@tones111
Copy link
Contributor Author

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
Copy link
Member

bjorn3 commented Feb 27, 2025

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.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 1, 2025
…r=workingjubilee

Fix link failure on AVR (incompatible ISA error)

Fixes rust-lang#137739. A reproducer of the issue is present there. I believe the root cause was introducing the avr-none target (which has no CPU by default) while also trying to get the ISA revision from the target spec. This commit uses the `target-cpu` option instead, which is already required to be present for the target.

r? compiler
cc `@Patryk27`
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 1, 2025
…r=workingjubilee

Fix link failure on AVR (incompatible ISA error)

Fixes rust-lang#137739. A reproducer of the issue is present there. I believe the root cause was introducing the avr-none target (which has no CPU by default) while also trying to get the ISA revision from the target spec. This commit uses the `target-cpu` option instead, which is already required to be present for the target.

r? compiler
cc ``@Patryk27``
@tones111
Copy link
Contributor Author

tones111 commented Mar 1, 2025

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.

@Noratrieb Noratrieb removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 1, 2025
LuigiPiucco added a commit to LuigiPiucco/rust that referenced this issue Mar 1, 2025
This is a fix for rust-lang#137739, and makes sure the "generic" CPU is never
used when target_cpu is passed. This is particularly useful for
architectures where knowing the exact CPU is a must, like AVR.
@bors bors closed this as completed in 4c1f51b Mar 2, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 2, 2025
Rollup merge of rust-lang#137830 - LuigiPiucco:incompatible-isa-fix, r=workingjubilee

Fix link failure on AVR (incompatible ISA error)

Fixes rust-lang#137739. A reproducer of the issue is present there. I believe the root cause was introducing the avr-none target (which has no CPU by default) while also trying to get the ISA revision from the target spec. This commit uses the `target-cpu` option instead, which is already required to be present for the target.

r? compiler
cc ``@Patryk27``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants