You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
libc++ is linked improperly when using cmake or standalone toolchains #379
Oh, that bullet point is way out of date. That was a KI in r11 or something.
libc++ is in much better shape than it was back then, but it's still not quite to the point that I'm comfortable recommending it. For a timeline on that, see our roadmap.
On a project we noticed strange crashes after exceptions. I assumed this to be the cause. I think I could make a small proof of concept for it.
Please do. I can't fix bugs that don't get reported.
When jansson is loaded, it crashes. When second-lib is loaded, it doesn't. For some reason, the externalproject_add library gets linked with a lot of gnu functions, while the other doesn't.
nm libjansson.so | grep 'gnu'
00008038 T __gnu_Unwind_Backtrace
00000000 w __gnu_Unwind_Find_exidx
00007f36 T __gnu_Unwind_ForcedUnwind
00007ed4 T __gnu_Unwind_RaiseException
00008570 T __gnu_Unwind_Restore_VFP
00008580 T __gnu_Unwind_Restore_VFP_D
00008590 T __gnu_Unwind_Restore_VFP_D_16_to_31
00008628 T __gnu_Unwind_Restore_WMMXC
000085a0 T __gnu_Unwind_Restore_WMMXD
00007f4a T __gnu_Unwind_Resume
00007f8c T __gnu_Unwind_Resume_or_Rethrow
00008578 T __gnu_Unwind_Save_VFP
00008588 T __gnu_Unwind_Save_VFP_D
00008598 T __gnu_Unwind_Save_VFP_D_16_to_31
0000863c T __gnu_Unwind_Save_WMMXC
000085e4 T __gnu_Unwind_Save_WMMXD
00007dd2 t __gnu_unwind_24bit.isra.1
0000874a T __gnu_unwind_execute
000089c0 T __gnu_unwind_frame
00007ca6 t __gnu_unwind_get_pr_addr
000080b2 t __gnu_unwind_pr_common
I'm assuming I'm doing something wrong, causing this. But I don't know what.
ndk-build gets this right. All static libraries must precede libgcc, which must preceded all shared libraries. I think I can at least fix cmake by just not using the linker script, but I'm not sure what to do about standalone toolchains...
I'm having the same issue, however the provided patch doesn't help in my case.
My setup is as follows: A fat library called scankit.so depends on many other c++ libraries (opencv, tesseract, zxing, ...) which are all built with gradle, cmake and the NDK r15, similar to the project above. I'm using the SDK-bundled cmake version 3.6.4111459. The fat lib is than packaged intro an aar and used in the app project through gradle.
Now when an exception is thrown in one of these libraries and caught in the scankit, I get the same SIGABRT as above.
I tried both c++_static and c++_shared versions, the only thing that changed was the place where it happened. With static, an exception thrown from lib a was crashing and with shared, this exception went through fine but an exception thrown in lib b now leads to a crash instead.
I tried the readelf stuff mentioned here #289 and got rid of the undefined Unwinder stuff, but that didn't fix it.
Is there anything I'm missing here or any advice what I should check to narrow it down? The linker command looks like this:
BTW, can anybody explain me what's the difference between libc++.so and libc++_shared.so in this case? Because my log states that I'm using libc++.so and the other logs show libc++_shared.so is being linked instead.
I'm using NDK r15c (15.2.4203891) and cmake 3.6.4111459 (I'm experiencing the very same results on r12b and r14b and the previous version of cmake). I've checked cmake scripts as well as build.ninja file. It seems that -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libunwind.a is used as expected.
However, I've got the issue similar to one described here. When I statically link to libc++, I've got the following crash:
Please, note that for some reason crash with dynamically inked libc++ happens when the exception is thrown, but call stack shows _Unwind_Resume only.
Note: it seems that Unwind* functions were linked from liblog.so instead of being statically linked from libunwind.a. Following the ndk-build's linking order we re-sorted all libraries in CMakeList.txt and manually added libc++ between static and dynamic libraries. Now we've got only __gnu_Unwind_Find_exidx being unresolved. Not quite sure what piece of code/library introduced it. However, we don't see crashes because of exceptions anymore.
We ran into the same issue here. We use a custom script to call clang instead of using NDK build or Andoid Studio or anything else. Sometime between setting up our build system and the latest NDK, the NDK build added a unwind.a (I think NDK r15?) to it linking process. We added that and did some reordering of the args we pass into the linker (based on the verbose output from the NDK build args) and we were able to resolve our issue.
ndk-build is unaffected by this bug as of r15, even if the build includes broken prebuilt libraries.
CMake should be unaffected as of r16, assuming there are no broken prebuilts.
Standalone toolchains and third-party-build systems are partially fixed as of r19. See explanation below. Compatibility with broken prebuilts it up to the build system.
Since the issue with broken prebuilts can only be fixed by the build system, the NDK cannot do any more to solve that problem. ndk-build does not have this problem.
NDK r19 turns libgcc.a into a linker script to ensure that libunwind.a is linked before libgcc.a, which clears up most of the issues. This cannot make the symbols hidden, so it may not behave properly in the presence of broken dependencies, but the toolchain itself produces correct output.
The remaining work here is to lift -Wl,--exclude-libs,libgcc.a into the Clang driver for Android targets (realistically it's probably a good change to make for all targets when using a static libgcc, but idk if that will get traction). I've forked that in to #823
Anything beyond that is something that each build system will have to deal with individually.
Activity
janisozaur commentedon May 2, 2017
Or a list of known cases that generate invalid results?
DanAlbert commentedon May 2, 2017
Do you have a bug report? We have no open bugs about exception handling in libc++ that I see.
marijnvdwerf commentedon May 2, 2017
It is said so on https://developer.android.com/ndk/guides/cpp-support.html#ic (scroll up a bit)
On a project we noticed strange crashes after exceptions. I assumed this to be the cause. I think I could make a small proof of concept for it.
DanAlbert commentedon May 2, 2017
Oh, that bullet point is way out of date. That was a KI in r11 or something.
libc++ is in much better shape than it was back then, but it's still not quite to the point that I'm comfortable recommending it. For a timeline on that, see our roadmap.
Please do. I can't fix bugs that don't get reported.
DanAlbert commentedon May 2, 2017
(doc is fixed now, btw, sorry for the confusion)
marijnvdwerf commentedon May 3, 2017
I went a bit deeper into this, now I know that
libc++isn't the source of the issue. Got a case here: https://gist.github.com/marijnvdwerf/5ae2d0405dc4ce766d7ab3d08522a308.When
janssonis loaded, it crashes. Whensecond-libis loaded, it doesn't. For some reason, theexternalproject_addlibrary gets linked with a lot of gnu functions, while the other doesn't.I'm assuming I'm doing something wrong, causing this. But I don't know what.
DanAlbert commentedon May 3, 2017
This is probably the problem. Could you share how
second-libis built?marijnvdwerf commentedon May 3, 2017
DanAlbert commentedon May 3, 2017
Hmm. We need to rethink how we link STLs in cmake, and also in standalone toolchains.
Looking at the
-voutput for the link command fornative-lib:In order:
-lgcc -ldl -lc -lgcc -ldlIf we look at the output for roughly the same project in ndk-build:
ndk-build gets this right. All static libraries must precede libgcc, which must preceded all shared libraries. I think I can at least fix cmake by just not using the linker script, but I'm not sure what to do about standalone toolchains...
[-][question] libc++ exception support[/-][+]libc++ is linked improperly when using cmake or standalone toolchains[/+]10 remaining items
Jaykob commentedon Jul 14, 2017
I'm having the same issue, however the provided patch doesn't help in my case.
My setup is as follows: A fat library called scankit.so depends on many other c++ libraries (opencv, tesseract, zxing, ...) which are all built with gradle, cmake and the NDK r15, similar to the project above. I'm using the SDK-bundled cmake version 3.6.4111459. The fat lib is than packaged intro an aar and used in the app project through gradle.
Now when an exception is thrown in one of these libraries and caught in the scankit, I get the same SIGABRT as above.
I tried both c++_static and c++_shared versions, the only thing that changed was the place where it happened. With static, an exception thrown from lib a was crashing and with shared, this exception went through fine but an exception thrown in lib b now leads to a crash instead.
I tried the readelf stuff mentioned here #289 and got rid of the undefined Unwinder stuff, but that didn't fix it.
Is there anything I'm missing here or any advice what I should check to narrow it down? The linker command looks like this:
Jaykob commentedon Jul 18, 2017
BTW, can anybody explain me what's the difference between libc++.so and libc++_shared.so in this case? Because my log states that I'm using libc++.so and the other logs show libc++_shared.so is being linked instead.
DanAlbert commentedon Jul 18, 2017
#379 (comment)
libc++_shared.so is the real library, libc++.so is a linker script that makes it less of a hassle to use standalone toolchains.
Attach arm issues with C++ exception unwinding
kaa-awem commentedon Sep 11, 2017
Hi Dan.
I'm using NDK r15c (15.2.4203891) and cmake 3.6.4111459 (I'm experiencing the very same results on r12b and r14b and the previous version of cmake). I've checked cmake scripts as well as build.ninja file. It seems that
-Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libunwind.ais used as expected.However, I've got the issue similar to one described here. When I statically link to libc++, I've got the following crash:
when I dynamically link to libc++, I've got a crash here:
Please, note that for some reason crash with dynamically inked libc++ happens when the exception is thrown, but call stack shows
_Unwind_Resumeonly.Note: it seems that Unwind* functions were linked from liblog.so instead of being statically linked from libunwind.a. Following the ndk-build's linking order we re-sorted all libraries in CMakeList.txt and manually added libc++ between static and dynamic libraries. Now we've got only __gnu_Unwind_Find_exidx being unresolved. Not quite sure what piece of code/library introduced it. However, we don't see crashes because of exceptions anymore.
KevinWrightCatDaddy commentedon Sep 22, 2017
We ran into the same issue here. We use a custom script to call clang instead of using NDK build or Andoid Studio or anything else. Sometime between setting up our build system and the latest NDK, the NDK build added a unwind.a (I think NDK r15?) to it linking process. We added that and did some reordering of the args we pass into the linker (based on the verbose output from the NDK build args) and we were able to resolve our issue.
Hide libgcc in all binaries.
Don't manually link libgcc in CMake.
DanAlbert commentedon Oct 17, 2018
A breakdown of the current status here:
Since the issue with broken prebuilts can only be fixed by the build system, the NDK cannot do any more to solve that problem. ndk-build does not have this problem.
NDK r19 turns libgcc.a into a linker script to ensure that libunwind.a is linked before libgcc.a, which clears up most of the issues. This cannot make the symbols hidden, so it may not behave properly in the presence of broken dependencies, but the toolchain itself produces correct output.
The remaining work here is to lift
-Wl,--exclude-libs,libgcc.ainto the Clang driver for Android targets (realistically it's probably a good change to make for all targets when using a static libgcc, but idk if that will get traction). I've forked that in to #823Anything beyond that is something that each build system will have to deal with individually.
DanAlbert commentedon Oct 17, 2018
I should also mention that the correct build process is documented here: https://android.googlesource.com/platform/ndk/+/master/docs/BuildSystemMaintainers.md#Unwinding
addressing android/ndk#379
forgot one standard version reference while addressing android/ndk#379
Fix support for arm32 for C++ exceptions support