2opremio / soroban-go-rust-preflight-poc

PoC of calling Rust from Go to obtain the preflight of a soroban contract
Do What The F*ck You Want To Public License
0 stars 0 forks source link

What to do about static linking in Linux #3

Open 2opremio opened 1 year ago

2opremio commented 1 year ago

Static linking is useful for non-cgo projects.

However, that doesn't really seem to be the case for CGO (at least when using glibc).

On one hand statically linking glibc (Linux): a. may cause problems when distributing the binaries due to its LGPL license b. isn't fully possible due to NSS. Note that when linking statically we get warnings like:

usr/bin/ld: ./lib/preflight/target/x86_64-unknown-linux-gnu/release/libpreflight.a(std-05737cf45bd30456.std.428b0c25-cgu.0.rcgu.o): in function `std::sys::unix::os::home_dir::fallback':
/rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/sys/unix/os.rs:629: warning: Using 'getpwuid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: ./lib/preflight/target/x86_64-unknown-linux-gnu/release/libpreflight.a(std-05737cf45bd30456.std.428b0c25-cgu.0.rcgu.o): in function `<std::sys_common::net::LookupHost as core::convert::TryFrom<(&str,u16)>>::try_from':
/rustc/897e37553bba8b42751c67658967889d11ecd120/library/std/src/sys_common/net.rs:205: warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

See https://github.com/2opremio/rust-plus-golang/actions/runs/3424937249/jobs/5705206558 for the full output and https://stackoverflow.com/questions/2725255/create-statically-linked-binary-that-uses-getaddrinfo for more details on the problem.

If we really want to statically link the binaries for linux, we should probably use musl instead of glibc:

  1. For compiling Rust we can use the aarch64-unknown-linux-musl and x86_64-unknown-linux-musl
  2. For compiling CGO we can use CC=musl-gcc or CC=aarch64-linux-musl-gcc (using https://musl.cc/aarch64-linux-musl-cross.tgz )
2opremio commented 1 year ago

musl Rust compiler targets (which allow linking truly statically) don't seem to allow unwinding Rust panics (See https://github.com/stellar/go/actions/runs/3484488730/jobs/5829099175 , in which you can see the Rust call causing an ABORT signal)