Skip to content

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

Open
@xjnxjnxjn

Description

@xjnxjnxjn

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-inline-assemblyArea: Inline assembly (`asm!(…)`)A-target-featureArea: Enabling/disabling target features like AVX, Neon, etc.C-bugCategory: This is a bug.C-external-bugCategory: issue that is caused by bugs in software beyond our controlE-needs-mcveCall for participation: This issue has a repro, but needs a Minimal Complete and Verifiable ExampleO-AVRTarget: AVR processors (ATtiny, ATmega, etc.)T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions