Problem description
Clang++ happily compiles the following (stored in m.cc)
#include<iostream>
int main() {
std::cout << 101 << "\n";
return 101;
}
But the generated program segfaults instantly.
Steps to reproduce
clang++ m.cc
./a.out
Expected behavior
Program runs without crash, printing 101 and returning the exit code 101.
Additional information
Post output of command termux-info.
termux-info
Updatable packages:
All packages up to date
Subscribed repositories:
https://dl.bintray.com/grimler/science-packages-21 science/stable
https://dl.bintray.com/grimler/game-packages-21 games/stable
https://termux.net stable/main
https://termux.net stable/main
System information:
Linux localhost 3.18.66-perf-g27418207c1b5-04571-gda413b97f3a4 #1 SMP PREEMPT Fri Sep 7 14:36:50 2018 aarch64 Android
Termux-packages arch:
aarch64
Android version:
8.0.0
Device manufacturer:
Sony
Device model:
F8331
Stack trace from gdb
Program received signal SIGSEGV, Segmentation fault.
0x0000007fb7ce9f54 in ?? ()
from /data/data/com.termux/files/usr/lib/libc++_shared.so
(gdb) bt
#0 0x0000007fb7ce9f54 in ?? ()
from /data/data/com.termux/files/usr/lib/libc++_shared.so
#1 0x0000007fb7ced258 in ?? ()
from /data/data/com.termux/files/usr/lib/libc++_shared.so
#2 0x0000007fb7ced2b8 in ?? ()
from /data/data/com.termux/files/usr/lib/libc++_shared.so
#3 0x0000007fb7ce81d8 in ?? ()
from /data/data/com.termux/files/usr/lib/libc++_shared.so
#4 0x0000007fb7ccd8fc in ?? ()
from /data/data/com.termux/files/usr/lib/libc++_shared.so
#5 0x0000007fb7ec3490 in __dl__ZL10call_arrayIPFviPPcS1_EEvPKcPT_mbS5_ () from /system/bin/linker64
#6 0x0000007fb7ec36c0 in __dl__ZN6soinfo17call_constructorsEv ()
from /system/bin/linker64
#7 0x0000007fb7ec35b8 in __dl__ZN6soinfo17call_constructorsEv ()
from /system/bin/linker64
#8 0x0000007fb7ebf6c8 in __dl___linker_init ()
from /system/bin/linker64
#9 0x0000007fb7ec5a04 in __dl__start ()
from /system/bin/linker64
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
(gdb)
Can't reproduce crash.
[~/clang-test]:$ cat m.cc
#include<iostream>
int main() {
std::cout << 101 << "\n";
return 101;
}
[~/clang-test]:$ clang++ m.cc
[~/clang-test]:$ ./a.out
101
[~/clang-test]:$ echo $?
101
[~/clang-test]:$ uname -a
Linux localhost 3.18.91-15877854-QB23332289 #1 SMP PREEMPT Mon Apr 22 12:37:03 KST 2019 aarch64 Android
Might happen only on android-5 branch. @cypher1 you can switch to the android-7 repo by running termux-upgrade-repo
@Grimler91 I was able currently to test only Android 5 ARM (not AArch64), still works like in https://github.com/termux/termux-packages/issues/3990#issuecomment-506690079.
@xeffyr, ah, great that your tested!
Confirmed on aarch64/android-5.
Seems doesn't happen on android-7 repos and android-5 arm.
I'll try to rebuild clang and see if that solves problem.
I'll try to rebuild clang and see if that solves problem.
Just rebuilding doesn't help...
Thanks that (termux-upgrade-repo) fixed it for me. Also reminded me that I've installed a ridiculously large set of packages.
Thank you so much
I'm having the same problem. However, running termux-upgrade-repo didn't help:
<zsh> u0_a141 ~ $ cat test.cpp
#include <iostream>
int main() {
std::cout << 101 << "\n";
return 101;
}
<zsh> u0_a141 ~ $ clang++ test.cpp
<zsh> u0_a141 ~ $ ./a.out
zsh: segmentation fault ./a.out
<zsh> u0_a141 ~ $ uname -a
Linux localhost 4.9.111 #1 SMP PREEMPT Fri Apr 26 02:50:26 CST 2019 aarch64 Android
<zsh> u0_a141 ~ $ termux-info
Updatable packages:
All packages up to date
Subscribed repositories:
https://dl.bintray.com/xeffyr/x11-packages-21 x11/main
https://dl.bintray.com/grimler/science-packages-21 science/stable
https://dl.bintray.com/grimler/termux-root-packages-21 root/stable
https://dl.bintray.com/grimler/game-packages-21 games/stable
https://termux.net stable/main
https://termux.net stable/main
System information:
Linux localhost 4.9.111 #1 SMP PREEMPT Fri Apr 26 02:50:26 CST 2019 aarch64 Android
Termux-packages arch:
aarch64
Android version:
9
Device manufacturer:
HUAWEI
Device model:
JSN-L21
<zsh> u0_a141 ~ $
@kodo-pp You are still subscribed to the android-5 repos so you did something wrong when you ran termux-upgrade-repo.
Just found that issue was in libc++_shared.so - it become broken after stripping and applying termux-elf-cleaner.
Posting here original AArch64 libc++ binary copied from NDK:
libc++_shared.so.gz
gunzip it and place to $PREFIX/lib and recompile your program.
Update:
To make libc++_shared.so broken, stripping should be done after executing termux-elf-cleaner. In fact, this is what actually build-package.sh does for libraries from NDK:
Executes termux-elf-cleaner when setting up cross-toolchain: termux_step_setup_toolchain.sh:153.
Strips binaries when preparing content for *.deb file: termux_step_massage.sh:34.
Why does clang/LLVM not segfault immediately then? They should use it too
Why does clang/LLVM not segfault immediately then?
Why it should ? It is linked against libc++ from NDK, not a "cleaned" one from Termux. All symbols present, so it is linked as it should and works.
do you mean it is statically linked against it?
No.
Stripped libc++_shared.so is not properly linked for whatever reason. But it has nothing to do with NDK bundle - it is fine.
I can confirm that your solution with replacing libc++_shared.so works
Absolutely all compiled programs output a Segmentation fault. After I replaced my libc ++ _ shared.so with yours - everything began to work fine.
The fix is now also available in the android-5 repository - update to the latest libc++ package version with pkg up!
This is still not fixed, why would you close this?
Everything required for fixing is done (get libc++ package from https://cirrus-ci.com/task/5750830328709120 for testing).
It just looks like libc++ package was rebuilt with termux-elf-cleaner v1.6 or less. Ask @fornwall to rebuild libc++ again in clean environment.
Just found that issue was in libc++_shared.so - it become broken after stripping and applying termux-elf-cleaner.
Posting here original AArch64 libc++ binary copied from NDK:
libc++_shared.so.gz
gunzip it and place to$PREFIX/liband recompile your program.Update:
To make libc++_shared.so broken, stripping should be done after executingtermux-elf-cleaner. In fact, this is what actuallybuild-package.shdoes for libraries from NDK:
- Executes
termux-elf-cleanerwhen setting up cross-toolchain: termux_step_setup_toolchain.sh:153.- Strips binaries when preparing content for *.deb file: termux_step_massage.sh:34.
The Segmentation fault still on my device:
termux-infoUpdatable packages:
All packages up to date
Subscribed repositories:
https://dl.bintray.com/grimler/science-packages-21 science/stable
https://dl.bintray.com/grimler/termux-root-packages-21 root/stable
https://termux.net stable/main
https://termux.net stable/main
System information:
Linux localhost 3.10.49-perf-g6675d52-50155-g4c9bddc #1 SMP PREEMPT Tue Apr 17 02:17:55 CST 2018 aarch64 Android
Termux-packages arch:
aarch64
Android version:
5.1.1
pkg list-installed
clang/stable,now 8.0.1-2 aarch64 [installed,automatic]
cmake/stable,now 3.15.3 aarch64 [installed]
libc++/stable,now 20-1 aarch64 [installed]
termux-elf-cleaner/stable,now 1.7 aarch64 [installed]
md5sum
58c74e3c9a0902fd4e0f49d354fd4347 /data/data/com.termux/files/home/usr/lib/libc++_shared.so
f76cf68da665cded3628abf23eef15bd /data/data/com.termux/files/usr/bin/aarch64-linux-android-ld
clang++ -v -O3 -std=c++11 -stdlib=libc++ -g -Wall -o helloworld helloworld.cpp
clang version 8.0.1 (tags/RELEASE_801/final)
Target: aarch64-unknown-linux-android
Thread model: posix
InstalledDir: /data/data/com.termux/files/usr/bin
"/data/data/com.termux/files/usr/bin/clang-8" -cc1 -triple aarch64-unknown-linux-android -emit-obj -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mthread-model posix -mdisable-fp-elim -masm-verbose -mconstructor-aliases -fuse-init-array -target-cpu generic -target-feature +neon -target-abi aapcs -mllvm -aarch64-fix-cortex-a53-835769=1 -fallow-half-arguments-and-returns -dwarf-column-info -debug-info-kind=limited -dwarf-version=4 -debugger-tuning=gdb -v -resource-dir /data/data/com.termux/files/usr/lib/clang/8.0.1 -internal-isystem /data/data/com.termux/files/usr/bin/../include/c++/v1 -internal-isystem /data/data/com.termux/files/usr/include/aarch64-linux-android -internal-isystem /usr/local/include -internal-isystem /data/data/com.termux/files/usr/lib/clang/8.0.1/include -internal-externc-isystem /data/data/com.termux/files/usr/include -O3 -Wall -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /data/data/com.termux/files/home/trojan-gfw/tests -ferror-limit 19 -fmessage-length 0 -fno-signed-char -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -vectorize-loops -vectorize-slp -o /data/data/com.termux/files/usr/tmp/main-6f6fd6.o -x c++ main.cpp
clang -cc1 version 8.0.1 based upon LLVM 8.0.1 default target aarch64-linux-android
ignoring nonexistent directory "/usr/local/include"include "..." search starts here:
include <...> search starts here:
/data/data/com.termux/files/usr/bin/../include/c++/v1
/data/data/com.termux/files/usr/include/aarch64-linux-android
/data/data/com.termux/files/usr/lib/clang/8.0.1/include
/data/data/com.termux/files/usr/include
End of search list.
"/data/data/com.termux/files/usr/bin/aarch64-linux-android-ld" -pie -EL --fix-cortex-a53-843419 -z now -z relro -z max-page-size=4096 --hash-style=both --enable-new-dtags --eh-frame-hdr -m aarch64linux -dynamic-linker /system/bin/linker64 -o hello /data/data/com.termux/files/usr/lib/crtbegin_dynamic.o -L/data/data/com.termux/files/usr/lib -L/data/data/com.termux/files/usr/bin/../lib /data/data/com.termux/files/usr/tmp/main-6f6fd6.o -lc++_shared -lgcc -ldl -lm -lc -lgcc -ldl /data/data/com.termux/files/usr/lib/crtend_android.o
@peter-tank Try pkg upgrade and check it again.
@xeffyr Thanks, I managed to fixed that by using binutils-gold to the final linking, and -LLVM_USE_LLD=ON for skip shared lib refrences double checking by cmake(undefined refrence to xxx@GLIBC) before pkg upgrade
After the pkg upgrade, only have libc++_20-2 installed, and the upper GOLD solution still needed for C program make. BUT cmake fails on undefined refrence to xxx@GLIBC in libc++_shared.so (linker command failed on initial c++ compiler test, on normal aarch64_linux_android_ld) @ CMakeTestCXXCompiler.cmake Moulde NONE help with -LLVM_USE_LLD=ON this time on compiling C++ codes by cmake. And manually compile C++ codes by adding -fuse-ld=gold is a must now.
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold")-DCMAKE_CXX_COMPILER_FORCED:BOOL=ON give it a go to skip CXX compiler test and force ld.gold for cmake.It seems issue#148 becomes more relevent now.
PASSED
openssl-1.1.1d.so: linking, strace_logSIGFAULT
openssl-1.1.1d.so and boost_1.70.0.a(the same on boost_1.69.0-2.so): linking, strace_logopenssl-1.1.1d.a and boost_1.70.0.a: linking, strace_logYou are hitting https://github.com/termux/termux-packages/issues/4287. Maybe side effect of fixing libc++. Happens only on android-5 branch.
Most helpful comment
I can confirm that your solution with replacing libc++_shared.so works