google / asylo

An open and flexible framework for developing enclave applications
https://asylo.dev
Apache License 2.0
967 stars 126 forks source link

Could not link Rust static lib to enclave code #45

Open tiziano88 opened 4 years ago

tiziano88 commented 4 years ago

I have been experimenting compiling Rust to a static library and linking it against our existing enclave code (will provide detailed steps if anyone is interested), but while I can link the resulting artifacts against standard C++ code, asylo seems picky about it, and I get the following errors in bazel:

bazel-out/k8-fastbuild/bin/rust/oak_runtime/liboak_runtime--866734307.a(divxc3.o): In function `__divxc3':
/rustc/1423bec54cf2db283b614e527cfd602b481485d1/src/llvm-project/compiler-rt/lib/builtins/divxc3.c:23: undefined reference to `fmaxl'
/rustc/1423bec54cf2db283b614e527cfd602b481485d1/src/llvm-project/compiler-rt/lib/builtins/divxc3.c:23: undefined reference to `logbl'
/rustc/1423bec54cf2db283b614e527cfd602b481485d1/src/llvm-project/compiler-rt/lib/builtins/divxc3.c:26: undefined reference to `scalbnl'
/rustc/1423bec54cf2db283b614e527cfd602b481485d1/src/llvm-project/compiler-rt/lib/builtins/divxc3.c:27: undefined reference to `scalbnl'
/rustc/1423bec54cf2db283b614e527cfd602b481485d1/src/llvm-project/compiler-rt/lib/builtins/divxc3.c:31: undefined reference to `scalbnl'
/rustc/1423bec54cf2db283b614e527cfd602b481485d1/src/llvm-project/compiler-rt/lib/builtins/divxc3.c:33: undefined reference to `scalbnl'

For reference, I also came across this https://dev.to/luzero/building-crates-so-they-look-like-c-abi-libraries-1ibn#static-libraries , but it did not help.

tiziano88 commented 4 years ago

cc @daviddrysdale @anghelcovici @michael-kernel-sanders

deeglaze commented 4 years ago

Are you perhaps using libc++ instead of libstdc++? Our libc (newlib) doesn't implement long double functions for architectures where double != long double. libstdc++ seems to fill in the blanks, but libc++ doesn't.

We're experimenting with splicing long double functions from musl into newlib in order to fill in the blanks for libc++, which leads to some weird header #define contortions, but that's not ready to release.

tiziano88 commented 4 years ago

Thanks @deeglaze for the quick and relevant reply! That may well be the case, unfortunately there is no easy way (that I know of) to force the Rust compiler to use one or the other lib. Do you have a prototype version of Asylo implementing the spliced functions you mentioned? I'd be happy to patch things locally and give it a try to see if it solves the issue.

tiziano88 commented 4 years ago

If it helps, the functions seem to be defined in https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/int_math.h .

tiziano88 commented 4 years ago

Blocks https://github.com/project-oak/oak/issues/313

deeglaze commented 4 years ago

int_math.h is referencing libc there, not defining the missing symbols. I don't have anything in a shareable state, sad to say. I spent a few hours trying to make newlib + musl build in the github environment but have hit some snags. I can share that with you to figure out a bit more, but I just don't have the capacity to put more time into it right now. https://github.com/deeglaze/repro/raw/master/newlib_musl.tgz

TheMarshalMole commented 3 years ago

This problem also appears when trying to build a driver for Linux Kernel -- 2021. 😢 Is there any fix for it?

TheMarshalMole commented 3 years ago

UPDATE: it works for Ubuntu 20+, but for Debian 10, no 😭

joelimgu commented 1 year ago

Same issue here