微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

锈:构建包含外部C库的静态链接二进制文件

如何解决锈:构建包含外部C库的静态链接二进制文件

我正在尝试使用MUSL静态构建我的rust应用程序。

我的应用程序使用sqlcipher。这意味着静态构建的可执行文件必须包含openssl和sqlcipher C库。

我正在使用https://github.com/emk/rust-musl-builder,所以我写了一个Dockerfile,以他们的dockerfile开头,该文件已经提供了MUSL环境,其中包括静态构建的启用musl的openssl。

因此,“我所需要的”只是先构建sqlcipher,然后构建我的rust应用程序。不幸的是,这对我来说非常复杂。

这是我当前的docker文件

FROM ekidd/rust-musl-builder

# sqlcipher requirements
ENV TZ=Europe/Ljubljana
RUN sudo sh -c "ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone"
RUN sudo apt update
RUN sudo apt install tcl -y

# sqlcipher
RUN VERS=4.4.1 && \
    cd /home/rust/libs && \
    curl -LO https://github.com/sqlcipher/sqlcipher/archive/v$VERS.tar.gz && \
    tar xzf v$VERS.tar.gz && cd sqlcipher-$VERS && \
    CC=musl-gcc ./configure  --host=x86_64-pc-linux-gnu --target=x86_64-linux-musl --prefix=/usr/local/musl --disable-tcl --disable-shared --with-crypto-lib=none --enable-static=yes --enable-tempstore=yes CFLAGS="-DsqlITE_HAS_CODEC -DsqlCIPHER_CRYPTO_OPENSSL -I/usr/include/x86_64-linux-musl -I/usr/local/musl/include -I/usr/local/musl/include/openssl" LDFLAGS=" /usr/local/musl/lib/libcrypto.a" && \
    make && sudo make install && \
    cd .. && rm -rf v$VERS.tar.gz sqlcipher-$VERS

# bring in my rust source
ADD --chown=rust:rust ./ .

# build my rust code
ENV RUSTFLAGS='-L/usr/local/musl/lib  -L/usr/lib/x86_64-linux-musl  -L/lib/x86_64-linux-musl -C linker=musl-gcc -Clink-arg=/usr/local/musl/lib/libcrypto.a -Clink-arg=/usr/local/musl/lib/libsqlcipher.a -C link-arg=/lib/ld-musl-x86_64.so.1 -Clink-arg=/usr/lib/x86_64-linux-musl/libc.a -Ctarget-feature=-crt-static -Clink-arg=-no-pie -C target-feature=+crt-static'
ENV PKG_CONfig_ALLOW_CROSS=1
ENV PKG_CONfig_ALL_STATIC=true
ENV OPENSSL_STATIC=true
ENV LIBZ_SYS_STATIC=1
CMD cargo build --target x86_64-unkNown-linux-musl --release --bin projectpad-cli && cp /home/rust/src/target/x86_64-unkNown-linux-musl/release/projectpad-cli /host

(我的锈源在https://github.com/emmanueltouzery/projectpad2

这完全成功并生成了二进制文件,但是它不是静态链接的:

ldd projectpad-cli 
    /lib/ld-musl-x86_64.so.1 => /lib64/ld-linux-x86-64.so.2 (0x00007f56d6749000)
    linux-vdso.so.1 (0x00007fff301f7000)

file projectpad-cli
     projectpad-cli: ELF 64-bit LSB executable,x86-64,version 1 (SYSV),dynamically linked,interpreter /lib/ld-musl-x86_64.so.1,with debug_info,not stripped

尝试从/lib/ld-musl-x86_64.so.1。中的malloc_usable_size()运行此二进制文件崩溃。

我相信是因为我的系统没有MUSL链接程序,所以它使用了glibc / gcc一个,这会引起问题。

我在想,如果我能够产生一个真正的静态二进制文件,而该二进制文件没有对链接器的引用,那么这可能会起作用。

知道我在做什么错吗?我尝试了一些builder.rs printlns,但目前还没有。

解决方法

好,我找到了...

我很确定问题是-C link-arg=/lib/ld-musl-x86_64.so.1标志。我认为这或多或少强制了一个动态可执行文件。删除它可以解决问题:-)

# -*- mode: dockerfile -*-
#
# An example Dockerfile showing how to add new static C libraries using
# musl-gcc.

FROM ekidd/rust-musl-builder

# https://rtfm.co.ua/en/docker-configure-tzdata-and-timezone-during-build/
ENV TZ=Europe/Ljubljana
RUN sudo sh -c "ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone"

RUN sudo apt update

RUN sudo apt install tcl -y

# Build a static copy of sqlcipher.
# https://github.com/sqlcipher/sqlcipher/issues/132#issuecomment-122908672 
# also related https://discuss.zetetic.net/t/cross-compile-sqlicipher-for-arm/2104/4
# https://github.com/sqlcipher/sqlcipher/issues/276
# https://github.com/rust-lang/rust/issues/40049
RUN VERS=4.4.1 && \
    cd /home/rust/libs && \
    curl -LO https://github.com/sqlcipher/sqlcipher/archive/v$VERS.tar.gz && \
    tar xzf v$VERS.tar.gz && cd sqlcipher-$VERS && \
    CC=musl-gcc ./configure  --host=x86_64-pc-linux-gnu --target=x86_64-linux-musl --prefix=/usr/local/musl --disable-tcl --disable-shared --with-crypto-lib=none --enable-static=yes --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC -DSQLCIPHER_CRYPTO_OPENSSL -I/usr/include/x86_64-linux-musl -I/usr/local/musl/include -I/usr/local/musl/include/openssl" LDFLAGS=" /usr/local/musl/lib/libcrypto.a" && \
    make && sudo make install && \
    cd .. && rm -rf v$VERS.tar.gz sqlcipher-$VERS

ADD --chown=rust:rust ./ .

# https://stackoverflow.com/questions/40695010/how-to-compile-a-static-musl-binary-of-a-rust-project-with-native-dependencies
# https://github.com/rust-lang/rust/issues/54243

ENV RUSTFLAGS='-L/usr/local/musl/lib  -L/usr/lib/x86_64-linux-musl  -L/lib/x86_64-linux-musl -C linker=musl-gcc -Clink-arg=/usr/local/musl/lib/libcrypto.a -Clink-arg=/usr/local/musl/lib/libsqlcipher.a -Clink-arg=/usr/lib/x86_64-linux-musl/libc.a'
ENV PKG_CONFIG_ALLOW_CROSS=1
ENV PKG_CONFIG_ALL_STATIC=true
ENV OPENSSL_STATIC=true
ENV LIBZ_SYS_STATIC=1
CMD cargo build --target x86_64-unknown-linux-musl --release --bin projectpad-cli && cp /home/rust/src/target/x86_64-unknown-linux-musl/release/projectpad-cli /host

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。