rustffiabi

Rust public abi for foreign language ffi


I'm writing a language and want it to be binary compatible with rust, c++ and some other languages. The problem is that I couldn't find information on rusts abi (name mangling, struct layouts, calling conventions, etc.).

I heard somewhere rust doesn't have a stable public abi (yet). But I couldn't find a source for that statement. I want to strictly avoid the c abi if possible.


Solution

  • Rust indeed does not have a stable ABI, so you cannot be compatible with it.

    Sources:

    https://doc.rust-lang.org/stable/reference/type-layout.html#the-rust-representation, about type layout:

    The Rust Representation

    ...

    There are no other guarantees of data layout made by this representation.

    https://github.com/rust-lang/rfcs/issues/600#issuecomment-526033825

    There are no current plans to introduce anything resembling a wholesale stable ABI either for some new repr(v1) or repr(Rust) and there is active opposition to this within the language team. As such, I'm going to close this super broad wishlist issue.

    Name mangling actually is defined, although not for the old, currently used by default version, but for v0 (can be used with -C symbol-mangling-version=v0). See here about it.

    There is an effort for "stable, but more high-level than the C ABI" ABI, currently called crabi, with other languages possibly integrating it as well. However, as of now it isn't even implemented experimentally even in Rust, so there is a long road ahead even if this experiment will succeed.