How to write type-parametric `naked_asm!`

⚓ Rust    📅 2025-08-06    👤 surdeus    👁️ 5      

surdeus

I have the following code:

#[unsafe(naked)]
extern "C" fn asm_trampoline() {
    naked_asm!(
        "
            tail trampoline
        "
    )
}

#[no_mangle]
extern "C" fn trampoline() { ... }

I need to make both functions parametric, but the “obvious” Rust style doesn’t work:

#[unsafe(naked)]
extern "C" fn asm_trampoline<T>() {
    naked_asm!(
        "
            tail trampoline::<T>
        "
    )
}

#[no_mangle]
extern "C" fn trampoline<T>() { ... }

Using ::<T> syntax just gets passed to the assembler, which of course doesn’t recognize it. Unfortunately, since this is a naked function, you can’t use a local variable either:

#[unsafe(naked)]
extern "C" fn asm_trampoline<T>() {
    let trampoline = trampoline::<T>;
    naked_asm!(
        "
            tail {trampoline}
        ",
        trampoline = in(reg) trampoline,
    )
}

#[no_mangle]
extern "C" fn trampoline<T>() { ... }

naked_asm! doesn’t accept variable arguments, and naked functions can’t have any code besides a single naked_asm! block.

Any idea how I can accomplish this?

3 posts - 2 participants

Read full topic

🏷️ Rust_feed