gnzlbg / ctest

Automatic testing of FFI bindings for Rust

Home Page:https://docs.rs/ctest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Don't use #[link_name] for the C symbol

gandro opened this issue · comments

In the libc crate, NetBSD, OS X and Windows all use #[link_name] to sometimes use alternative linker symbols for functions like stat.

As far as I can tell, on OS X and NetBSD the system header files do a similar technique: The C symbol is distinct from the linker symbol. In the case of NetBSD for example, there is no C symbol named __stat50, the C source code keeps using the original stat C symbol. Only for the assembly output the linker symbol __stat50 is used (coming from an asm directive in the C header file).

On Windows however, C source code needs to use the C symbol _stat directly (which is also used for the linker symbol).

Currently, ctest uses the linker symbol as the C symbol for the generated C code. This is needed for Windows, since there the C symbol is actually the symbol used in #[link_name]. But it breaks on NetBSD, as there the #[link_name] linker symbols are undefined in the C compiler.

I'm not sure how to fix this. It seems that there already is an rule excluding symbols containing $ for OS X (like stat$INODE64). We could add a rule to exclude symbols starting with __ to work around this issue for NetBSD, but it would just be an additional hack.

For a second I was wondering how this worked at all due to the usage of $, but I see you ran across that exception before I did!

I'm actually surprised this logic worked for as long as it did, I think it's 100% right that link_name shouldn't be used at all in the C file by default, Windows is the exception here where it wants to use that.