0

I am new to StackOverflow, but i've asked on countless programming discords and filed dozens of Github issues and am still totally lost as to what the problem is.

https://github.com/DrewRidley/bepuvy-sys

I am compiling this repository with the C# compiler "NativeAOT". This is supposed to generate either a static or dynamic library which can be interoperated with other libraries without requiring the .NET runtime on the target platform.

Unfortunately, every time I write some FFI bindings to the library generated, I get some very strange behavior. Calling the exported APIS from a C++ program works 100% reliably, but in rust, this is not so.

In rust, I consistently get the FFI crate to compile (bepuvy_sys), but any dependency crates always have linker errors with missing symbols.

  = note: Undefined symbols for architecture arm64:
            "_RhGetModuleFromEEType", referenced from:
                _S_P_CoreLib_Internal_Runtime_Augments_RuntimeAugments__GetModuleFromTypeHandle in libbepuvy_sys-c614911535d78543.rlib(Bepuvy.o)
            "_RhpMemoryBarrier", referenced from:
                _S_P_CoreLib_System_Threading_Interlocked__MemoryBarrier in libbepuvy_sys-c614911535d78543.rlib(Bepuvy.o)
          ld: symbol(s) not found for architecture arm64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)
          
                                                                     e-darwin/lib/libminiz_oxide-36d0e1a60d5b6b8c.rlib" "/Users/drewridley/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/aarch64-

These symbols are from the NativeAOT runtime itself, and in some way shape or form, rust must be stripping or removing them once they reach a binary that is being compiled.

I am 100% of the time, reliably able to invoke the exported functions from a basic C program, but have never been able to get rust to work. As highlighted above, I have reason to believe that rust is doing something with the linker that messes up the symbols and causes UB

Any help would be much appreciated!

Drew
  • 1
  • 1
  • Weird. Judging from the symbol names, the missing symbol is somewhere in this property get method https://github.com/dotnet/runtime/blob/8042facb423e8a1151282409e70e380273674926/src/coreclr/nativeaot/Common/src/Internal/Runtime/MethodTable.cs#L1173 ? – Jeremy Lakeman Jun 14 '23 at 02:54
  • Is that with the static library or with the dynamic library? – Jmb Jun 14 '23 at 06:36
  • Also can you show us a minimal example of a program that tries to call a function from the C# library and fails (both the `main.rs` and `Cargo.toml`, along with the `build.rs` if you have one). – Jmb Jun 14 '23 at 06:39

0 Answers0