Skip to content

Wrong calling convention on using ffi of extern mod #9055

Closed
@klutzy

Description

@klutzy

Here's an example on win32.

a.rs:

#[crate_type = "lib"];

pub use std::libc::*;
pub use std::libc::types::os::arch::extra::*;

extern "stdcall" {
    fn MessageBoxW(hWnd: HANDLE, lpText: LPCWSTR, lpCaption: LPCWSTR, uType: c_uint) -> c_int;
}

Build a.rs as shared library.

b.rs:

extern mod a;
use std::ptr;

#[fixed_stack_segment]
fn main() {
    unsafe { a::MessageBoxW(ptr::mut_null(), ptr::null(), ptr::null(), 0 as a::c_uint); }
}

Building b.rs I got linking failure:

note: b.o:fake:(.text+0x86): undefined reference to `MessageBoxW'

LLVM IR has some interesting point:

; Function Attrs: noinline uwtable
define void @_ZN4main16_bd1837541cdc5544v0.0E({ i32, %tydesc*, i8*, i8*, i8 }*) #4 {
  ...
  %14 = call x86_stdcallcc i32 @MessageBoxW(%"enum.std::libc::types::common::c95::c_void[#1]"* %10, i16* %11, i16* %12, i32 %13)
  ...
}
...
declare i32 @MessageBoxW(%"enum.std::libc::types::common::c95::c_void[#1]"*, i16*, i16*, i32)

so rustc generates correct call, but wrong declare (no callconv).

Frankly I don't even know if it's ok to use foreign fn from extern module. I'm also confused that there is no need to add pub at MessageBoxW declaration.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-FFIArea: Foreign function interface (FFI)O-windowsOperating system: Windows

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions