Rust inline asm expression call function pointer #38988

Closed
cptroot opened this Issue Jan 11, 2017 · 2 comments

Projects

None yet

3 participants

@cptroot
cptroot commented Jan 11, 2017

What is the correct way to use inline assembly in rust to call a function pointer?

I tried the following

#![feature(asm)]

fn main() {
    println!("Hello, world!");

    unsafe {
        asm!("\
            callq $0
            " : : "i"(other_function as extern "C" fn())
        );
    }
}

extern fn other_function() {
    println!("do stuff");
}

based off of Phil Opperman's blog, but this is giving me the following error:

error: <inline asm>:1:8: error: invalid operand for instruction
        callq $_ZN7testing14other_function17h88b4b876c9c8afc5E
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  --> src/main.rs:7:9
   |
7  |           asm!("\
   |  _________^ starting here...
8  | |             callq $0
9  | |             " : : "i"(other_function as extern "C" fn())
10 | |         );
   | |__________^ ...ending here

error: aborting due to previous error

What's the issue here? What is the correct way to generate a call instruction to a function pointer using inline assembly?

@nagisa
Contributor
nagisa commented Jan 11, 2017
fn main() {
    println!("Hello, world!");

    unsafe {
        asm!("\
            call $0
            " : : "i"(other_function as extern "C" fn())::"intel"
        );
    }
}

;)

@steveklabnik
Contributor

@cptroot questsions are best asked on http://users.rust-lang.org/ or Stack Overflow, we try to keep the issue tracker for bugs only. Seems like @nagisa was kind enough to give you an answer anyway 😄 But, since this isn't a bug, I'm going to close it, if you have futher questions, please ask in one of those places, thanks 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment