rust-lang / rust-lang/rust

Compile Error on AVR atmega328p with global_asm! in lib.rs

Open
#134,758 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-inline-assembly A-LLVM A-target-feature C-bug C-external-bug E-needs-mcve O-AVR T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

This is my first issue, so I'm not sure if this is the right place to report or if it is reported already.

Also formatting this issue is new to me ...

I'm on Rust with AVR atmega328p, using

nightly-2024-11-12-aarch64-unknown-linux-gnu (default)
rustc 1.84.0-nightly (81eef2d36 2024-11-11)

I use "global_asm!" macro which compiles Ok in a bin main.rs and gives a compile error in a lib lib.rs.

The error is

error: <inline asm>:2:5: instruction requires a CPU feature not currently enabled
    x:
    jmp x
   ^

The source in both, main.rs and lib.rs is

//
global_asm!(r#"
   x:
   jmp x
"#);

To test, I have two crates, one to test as --bin with main.rs, one to test as --lib with lib.rs

Both crates have .cargo/config.toml

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

[target.avr-atmega328p]
rustflags = [
    #
    "-Clink-arg=-nostartfiles",
]

[unstable]
build-std = ["core"]

Both have Config.toml

[package]
name = "..."
edition = "2021"

[dependencies]

[profile.release]
panic = "abort"
lto = true

The main.rs in the bin test crate :

//!
#![no_std]
#![no_main]
//
#![allow(unused_imports)]
//
#![feature(asm_experimental_arch)]

use core::arch::{ asm, global_asm };

//
global_asm!(r#"
   x:
    jmp x
"#);

///
use core::panic::PanicInfo;
#[panic_handler]
fn panic(_panic: &PanicInfo<'_>) -> ! {
    loop {
    }
}

... compiles and links fine !

The lib.rs in the lib test crate :

//!
#![no_std]
//
#![allow(unused_imports)]
//
#![feature(asm_experimental_arch)]

use core::arch::{ asm, global_asm };

//
global_asm!(r#"
   x:
   jmp x
"#);

... and gives compile error

error: <inline asm>:2:5: instruction requires a CPU feature not currently enabled
    jmp x
    ^

Command for both crates is cargo build --release

./home/rust/rust-repos/rustc/rust/src/llvm-project/llvm/lib/Object/ModuleSymbolTable.cpp

I've tracked it down to llvm :

llvm-project/llvm/lib/Object/ModuleSymbolTable.cpp
static void initializeRecordStreamer(...) 
{
...
  std::unique_ptr<MCSubtargetInfo> STI(
      T->createMCSubtargetInfo(TT.str(), "", ""));
...
}

The ("","") forces the feature map to go to the lowest for AVR and does not honor, that "atmega328p" features should be used.

Ich I patch it to

  std::unique_ptr<MCSubtargetInfo> STI(
      T->createMCSubtargetInfo(TT.str(), "atmega328p", ""));

it compiles fine :) But this is not a Solution

So here I stuck ...

br
xjn

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce both AVR crates with cargo build --release, comparing global_asm! in main.rs and lib.rs. Start with llvm-project/llvm/lib/Object/ModuleSymbolTable.cpp and the createMCSubtargetInfo call; trace how the atmega328p target features reach the library case. Done means the library builds without hardcoding the feature and the bin behavior remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, embedded-iot
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.