簡體   English   中英

鏈接時如何始終包含 Rust 依賴項?

[英]How do I always include a Rust dependency when linking?

我正在嘗試使用 Fortanix SGX 框架在飛地中運行 libdvdcss,但 linker 存在問題。

我圍繞 libdvdcss 創建了一個簡單的 FFI 包裝器,在 Linux 上正常執行它時工作正常(沒有 SGX 並且全局安裝了 libdvdcss)。 當我使用入門頁面上指定的目標x86_64-fortanix-unknown-sgx運行它時它不起作用,因為 linker 抱怨許多丟失的符號,特別是關於malloc等。

據我了解,問題在於 SGX 中沒有 libc,因此我需要手動包含rs-libc ,它基本上是在 SGX 中使用的 libc。 rs-libc包包含 C、ASM 以及一些 Rust 代碼(主要用於malloc )。 因此我的 Cargo.toml:

[dependencies]
rs-libc = "0.2.3"

我發現這還不夠,我必須提供以下 build.rs 來手動告訴 linker 也將libc.ars-libc板條箱鏈接起來。

pub fn main() {
    println!("cargo:rustc-link-search=/home/me/dev/libdvdcss/.libs");  // Will be changed later
    println!("cargo:rustc-link-lib=static=dvdcss");
    println!("cargo:rustc-link-lib=static=c");
}

這修復了一些strlen等未找到的錯誤,但不適用於mallocfree 我認為問題在於它們不是從 C 源文件編譯的,而是在alloc.rs中編譯的。 我還看到這個源文件被編譯到target/x86_64-fortanix-unknown-sgx/debug/deps/librs_libc-04111db022dd517f.rlib中的 Rust 庫並包含以下符號:

rs_libc-04111db022dd517f.rs_libc.53f0fd72-cgu.5.rcgu.o:
0000000000000000 T calloc
0000000000000000 T free
0000000000000000 W __llvm_lvi_thunk_r11
0000000000000000 T malloc
0000000000000000 T realloc
0000000000000000 V __rustc_debug_gdb_scripts_section__
                 U _ZN3std3sys3sgx5alloc81_$LT$impl$u20$core..alloc..global..GlobalAlloc$u20$for$u20$std..alloc..System$GT$12alloc_zeroed17ha722ea369ab2dac9E
                 U _ZN3std3sys3sgx5alloc81_$LT$impl$u20$core..alloc..global..GlobalAlloc$u20$for$u20$std..alloc..System$GT$5alloc17h561d825df0f2dfa8E
                 U _ZN3std3sys3sgx5alloc81_$LT$impl$u20$core..alloc..global..GlobalAlloc$u20$for$u20$std..alloc..System$GT$7dealloc17hbbda0b3d0a1f9d67E
                 U _ZN3std3sys3sgx5alloc81_$LT$impl$u20$core..alloc..global..GlobalAlloc$u20$for$u20$std..alloc..System$GT$7realloc17hf76d62802e46847bE
                 U _ZN4core3ptr4read17hc3470c4a404ff082E
                 U _ZN4core3ptr5write17ha0bb4d6cbfed535eE
                 U _ZN4core5alloc6layout6Layout25from_size_align_unchecked17h074422ae6f6459f7E
                 U _ZN4core9panicking5panic17hde9db97a7382bd28E

不幸的是,這個文件似乎沒有包含在最終的 linker 命令中。 我認為這是因為在我的 Rust 應用程序中沒有找到這個庫的用法,因此它被優化了。

有沒有辦法包含它?

添加一個extern crate rs_libc; 到你的箱子的根應該做的伎倆。 必須將使用extern crate聲明的依賴項提供給 linker。 在 rust 2018 及更高版本中,當且僅當您使用該 crate 的一個或多個符號(rust 2015 要求顯式添加extern crate )時,編譯器才會將適當的聲明隱式添加到您的 crate。 有關詳細信息,請參閱rust 參考中的部分。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM