Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build fully statically linked executable for Android not supported #10765

Closed
leleliu008 opened this issue Jun 17, 2022 · 6 comments
Closed

build fully statically linked executable for Android not supported #10765

leleliu008 opened this issue Jun 17, 2022 · 6 comments
Labels
C-bug Category: bug

Comments

@leleliu008
Copy link

Problem

I want to build a fully statically linked executable for Android.

I respectively tried following settings:

export RUSTFLAGS="$RUSTFLAGS -C target-feature=+crt-static"
export RUSTFLAGS="$RUSTFLAGS -Clink-arg=-static"

both are not work. the final executable is always dynamically-linked.

Steps

build https://github.com/sharkdp/bat.git for Android

Possible Solution(s)

No response

Notes

No response

Version

cargo 1.59.0 (49d8809dc 2022-02-10)
release: 1.59.0
commit-hash: 49d8809dc2d3e6e0d5ec634fcf26d8e2aab67130
commit-date: 2022-02-10
host: x86_64-unknown-linux-gnu
libgit2: 1.3.0 (sys:0.13.23 vendored)
libcurl: 7.80.0-DEV (sys:0.4.51+curl-7.80.0 vendored ssl:OpenSSL/1.1.1l)
os: Ubuntu 22.04 (jammy) [64-bit]
@leleliu008 leleliu008 added the C-bug Category: bug label Jun 17, 2022
@weihanglo
Copy link
Member

Seems like on Android crt-static is ignored.

If you feel hard to tell whether it is a bug from Cargo, I'd recommend asking on the user forum. Community there is often more helpful for general usage questions. I am going to close this for now. We can reopen it at anytime if there are things Cargo can improve on. Thanks for the report!

@weihanglo weihanglo closed this as not planned Won't fix, can't repro, duplicate, stale Jun 18, 2022
@petrochenkov
Copy link
Contributor

This is an issue (or rather lack of a feature) with rustc and libc, not with cargo.
rust-lang/rust#65447 (comment) has instructions that were used for enabling +crt-static for glibc, but they are more or less applicable to bionic libc on Android too.

It would be really great to implement this, it's been in my work queue for years, just never with a high enough priority.

@leleliu008
Copy link
Author

It would be really great to implement this

Yes, if this feature is implemented, we can link the lastest libc.a to the executable, then the executable can be run on older devices.

@petrochenkov
Copy link
Contributor

@leleliu008
You can try implementing it yourself, it's shouldn't be a complex task, see rust-lang/libc#1899 and rust-lang/rust#77386 for examples.

@leleliu008
Copy link
Author

@petrochenkov Thanks for the hint.

@leleliu008
Copy link
Author

@petrochenkov

I find a easy way to build fully statically linked executable for Android without changing any code.

ANDROID_API_LEVEL=31
ANDROID_NDK_ROOT="/home/leleliu008/.ndk-pkg/android-ndk-r23b"
ANDROID_HOST_TAG=$(uname | tr A-Z a-z)-x86_64
ANDROID_LIBRARY_DIR="$ANDROID_NDK_ROOT//toolchains/llvm/prebuilt/$ANDROID_HOST_TAG/sysroot/usr/lib/aarch64-linux-android/$ANDROID_API_LEVEL"

export RUSTFLAGS="-Clink-arg=-L$PWD -Clink-arg=-static -Clink-arg=-ffunction-sections -Clink-arg=-fdata-sections -Clink-arg=-Wl,--gc-sections -Clink-arg=-Wl,--no-dynamic-linker"

cat > libc.a <<EOF
INPUT($ANDROID_LIBRARY_DIR/libc.a)
EOF

cat > libm.a <<EOF
INPUT($ANDROID_LIBRARY_DIR/libm.a)
EOF

cat > libz.a <<EOF
INPUT($ANDROID_LIBRARY_DIR/libz.a)
EOF

cat > libdl.a <<EOF
INPUT($ANDROID_LIBRARY_DIR/libdl.a)
EOF

I successfully build the fully statically linked executable in this way.

Screenshot from 2022-06-20 00-03-22

the only fly in the ointment being the fact that the dynamic section is in the final statically linked executable.
Screenshot from 2022-06-20 00-04-26

Although this way has a slight flaw but it doesn't affect the usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

3 participants