rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.8k stars 12.66k forks source link

global_asm! errors are inconsistent with target features #113221

Open eigenform opened 1 year ago

eigenform commented 1 year ago
$ rustc --version --verbose
rustc 1.71.0-nightly (2f2c438dc 2023-05-08)
binary: rustc
commit-hash: 2f2c438dce75d8cc532c3baa849eeddc0901802c
commit-date: 2023-05-08
host: x86_64-unknown-linux-gnu
release: 1.71.0-nightly
LLVM version: 16.0.2

global_asm! prints errors about missing target features even when they are enabled. I think this is the same behavior as

50477.

In this case, rustc emits errors but doesn't abort and produces the correct, expected output file.

/// foo.rs
core::arch::global_asm!("
.section .text
.global my_func
my_func:
    sb
    ret
");

#[no_mangle]
#[inline(never)]
fn main() {
    //unsafe { core::arch::asm!("sb"); }
}
$ rustc foo.rs --target aarch64-unknown-linux-gnu --crate-type lib -C target-feature=+sb
error: instruction requires: sb
    sb
    ^
error: instruction requires: sb
    sb
    ^
$ aarch64-linux-gnu-objdump -C -d libfoo.rlib 
In archive libfoo.rlib:

lib.rmeta:     file format elf64-littleaarch64

foo.foo.bbcbb88c88589229-cgu.0.rcgu.o:     file format elf64-littleaarch64

Disassembly of section .text:

0000000000000000 <my_func>:
   0:   d50330ff    sb
   4:   d65f03c0    ret

Disassembly of section .text.main:

0000000000000000 <main>:
   0:   d65f03c0    ret

For comparison, here's the same situation with asm! instead:

[no_mangle]

[inline(never)]

fn main() { unsafe { core::arch::asm!("sb"); } }

$ rustc foo.rs --target aarch64-unknown-linux-gnu --crate-type lib error: instruction requires: sb --> foo.rs:34:27 34 core::arch::asm!("sb"); ^

note: instantiated into assembly here --> :1:2 | 1 | sb | ^

error: aborting due to previous error

$ rustc foo.rs --target aarch64-unknown-linux-gnu --crate-type lib -C target-feature=+sb $ aarch64-linux-gnu-objdump -C -d libfoo.rlib In archive libfoo.rlib:

lib.rmeta: file format elf64-littleaarch64

foo.foo.bbcbb88c88589229-cgu.0.rcgu.o: file format elf64-littleaarch64

Disassembly of section .text.main:

0000000000000000

: 0: d50330ff sb 4: d65f03c0 ret

Jules-Bertholet commented 1 year ago

@rustbot label C-bug A-inline-assembly