Skip to content

asm! doesn't accept offset syntax, but gcc does #79874

Open
@joshtriplett

Description

@joshtriplett

In Intel-syntax assembly, to refer to the address of a symbol rather than the memory pointed to by that symbol, you have to write offset symbol rather than just symbol. For instance, mov rax, 1f is equivalent to mov rax, qword ptr 1f and moves the memory pointed to by 1f into rax, while mov rax, offset 1f moves the address of 1f into rax.

GCC and GAS accept this syntax. However, clang and Rust do not.

(The examples below use position-dependent addressing for simplicity. Production code should use position-independent addressing.)

/tmp$ cat offset.c 
#include <stdio.h>

int main() {
    unsigned value = 0;
    __asm__ __volatile__(
        "mov %0, 1\n"
        "mov rax, offset 3f\n"
        "jmp rax\n"
        "mov %0, 2\n"
        "3:"
        : "=r" (value)
        :
        : "rax"
    );
    printf("%u\n", value);
    return 0;
}

/tmp$ gcc -no-pie -masm=intel -O3 offset.c -o offset
/tmp$ ./offset 
1
/tmp$ clang-12 -no-pie -masm=intel -O3 offset.c -o offset
offset.c:7:10: error: unexpected token in argument list
        "mov rax, offset 3f\n"
         ^
<inline asm>:2:17: note: instantiated into assembly here
mov rax, offset 3f
                ^
1 error generated.
(1) /tmp$ cat offset.rs 
#![feature(asm)]

fn main() {
    let mut value: u64;
    unsafe {
        asm!(
          "mov {value}, 1",
          "mov rax, offset 3f",
          "jmp rax",
          "mov {value}, 2",
          "3:",
          value = out(reg) value,
          out("rax") _,
        );
    }
    dbg!(value);
}
/tmp$ rustc +nightly offset.rs 
error: unexpected token!
 --> offset.rs:8:12
  |
8 |           "mov rax, offset 3f",
  |            ^
  |
note: instantiated into assembly here
 --> <inline asm>:3:17
  |
3 | mov rax, offset 3f
  |                 ^

error: aborting due to previous error

(1) /tmp$ rustc +nightly --version
rustc 1.50.0-nightly (1c389ffef 2020-11-24)

Metadata

Metadata

Assignees

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-inline-assemblyArea: Inline assembly (`asm!(…)`)C-bugCategory: This is a bug.F-asm`#![feature(asm)]` (not `llvm_asm`)O-x86_64Target: x86-64 processors (like x86_64-*) (also known as amd64 and x64)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