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

ndk-build always links libstdc++ #105

Closed
DanAlbert opened this issue May 24, 2016 · 18 comments
Closed

ndk-build always links libstdc++ #105

DanAlbert opened this issue May 24, 2016 · 18 comments
Assignees
Labels
Milestone

Comments

@DanAlbert
Copy link
Member

$ readelf -d sources/cxx-stl/llvm-libc++/libs/armeabi-v7a/libc++_shared.so
...
0x00000001 (NEEDED)                     Shared library: [libstdc++.so]
...

This is actually because ndk-build always links libstdc++ since we don't use -nodefaultlibs. We should fix this.

@DanAlbert DanAlbert added this to the r13 milestone May 24, 2016
@DanAlbert DanAlbert self-assigned this May 24, 2016
@DanAlbert DanAlbert modified the milestones: r14, r13 Aug 9, 2016
@DanAlbert DanAlbert removed this from the r14 milestone Jan 4, 2017
miodragdinic pushed a commit to MIPS/external-libcxx that referenced this issue Jan 12, 2017
An alternative implementation would be to use `-nodefaultlibs`, but
that seems a bit more fragile. In the platform we use
`-nodefaultlibs`, but it took us quite a while to get that right.

Bug: android/ndk#105
Change-Id: I417f4afbfd46288d2eeffc85f056a6dc8a3a117d
miodragdinic pushed a commit to MIPS/ndk that referenced this issue Jan 12, 2017
First, enable fatal linker warnings since responsible users will do
that too.

Second, build an executable rather than a shared libray. The linker
only checks certain warnings (such as unavailable shared libraries in
the DT_NEEDED of your dependencies) when building executables.

With these two changes, we would have noticed
android/ndk#105 affecting libc++_shared
at test time rather than via manual inspection.

Change-Id: I0cb06755318064325d235d1d8de6be442fd60ae7
@DanAlbert
Copy link
Member Author

Went to add -nostdlib++ to Clang and found that @nico beat me to it: https://reviews.llvm.org/D35780 (thanks!)

@srhines @pirama-arumuga-nainar: Not sure if you've picked a revision for the next update yet. Could you make sure that patch is included?

@AstralStorm
Copy link

AstralStorm commented Sep 6, 2017

Also affects cmake builds in 16.0.4293906 rc1 and older.

Hacky workaround if you want to patch it in android.toolchain.cmake (replace to ANDROID_*_FLAGS in that case) follows. This workaround doesn't seem to break anything, but might be adding a few too many libs in fact.

Workaround:
if(NOT CMAKE_CXX_FLAGS MATCHES "nostdlib" AND NOT CMAKE_CXX_FLAGS MATCHES "nodefaultlibs")
set(CMAKE_POLICY_DEFAULT_CMP0056 NEW)
string(CONCAT CMAKE_CXX_FLAGS "-nodefaultlibs " "${CMAKE_CXX_FLAGS}")
string(CONCAT CMAKE_SHARED_LINKER_FLAGS "-lc -lm -ldl " "${CMAKE_SHARED_LINKER_FLAGS}")
string(CONCAT CMAKE_EXE_LINKER_FLAGS "-lc -lm -ldl " "${CMAKE_EXE_LINKER_FLAGS}")
endif()

Equivalent could be applied in ndk-build scripts.

@java4ada
Copy link

ETA ?

@DanAlbert
Copy link
Member Author

Whenever we get a clang update. Probably r17.

@DanAlbert
Copy link
Member Author

@wang-bin
Copy link

wang-bin commented Dec 7, 2017

Why a new option -nostdlib++? libstdc++ is the default for clang, but explicitly setting -stdlib=libc++ is enough. I use the option since r13 without any problem.

@DanAlbert
Copy link
Member Author

That is not equivalent. libc++.so and libc++.a in the NDK are linker scripts that link all their dependencies. These exist to facilitate standalone toolchains. It is not possible for the link order to be exactly correct using this method, and you run the risk of hitting #379.

@wang-bin
Copy link

wang-bin commented Dec 7, 2017

That is not equivalent

Yes. -nostdlib++ disables both libc++ and libstdc++

libc++.so and libc++.a in the NDK are linker scripts that link all their dependencies

I knew it. In fact I wondered why not removing the full path of libc++.so in linker flags. -stdlib=libc++ will replace -lstdc++ by -lc++, and because of libc++.so is a script, libc++_shared.so and some other libraries are linked. I got exactly the same binary without the full path libc++.so. But I can't remove it without modifying android.toolchain.cmake.

These exist to facilitate standalone toolchains

I use cmake

It is not possible for the link order to be exactly correct using this method, and you run the risk of hitting #379

Not sure. No problem found yet

@thughes
Copy link

thughes commented Apr 3, 2018

@DanAlbert Why does nostdlib++ not apply when using STLport?
https://android.googlesource.com/platform/ndk.git/+/master/build/cmake/android.toolchain.cmake#332

I don't know much about STLport (and am not using it for my ndk build), but it's not entirely clear to me why it should be special:
https://developer.android.com/ndk/guides/cpp-support.html#stlport
https://developer.android.com/ndk/guides/cpp-support.html#one_stl_per_app

I'm trying to make sure cmake (without using the toolchain file) has the right behavior:
https://gitlab.kitware.com/cmake/cmake/merge_requests/1919
https://gitlab.kitware.com/cmake/cmake/issues/17863

kwrobot pushed a commit to Kitware/CMake that referenced this issue Apr 4, 2018
The chosen STL libraries are already linked explicitly so we shouldn't
let the compiler add its implicit `-lstdc++` (the default) when invoking
the linker.

Fixes: #17863
NDK-Issue: android/ndk#105
Inspired-by: Tom Hughes <tomtheengineer@gmail.com>
miodragdinic pushed a commit to MIPS/ndk that referenced this issue Apr 17, 2018
This prevents Clang from automatically linking libstdc++ when we
don't want it to. It needs to for the system STL and for stlport
(which gets new/delete from the system STL), but for gnustl and
libc++ we don't want this linked.

Test: ./checkbuild.py && ./run_tests.py
Bug: android/ndk#105
Change-Id: I2c5d6d6212f6b9f3552934100f2ed970fd8340ab
@DanAlbert
Copy link
Member Author

Sorry, missed this update.

Bionic's libstdc++ is really more akin to libc++about or libsupc++ (though not complete). It contains an implementation of new and delete, and not much else. Stlport does not include these, so it gets new/delete from bionic's libstdc++.

Not that this is very relevant now. There's not much reason to use stlport starting with r16 and it doesn't exist at all in master.

@cybertk
Copy link

cybertk commented Jun 5, 2018

The issue still exists in NDK r17 while building with ndk-build, is there any fix plan or workaround?

@cybertk
Copy link

cybertk commented Jun 5, 2018

NDK r16b works fine with same project configuration

APP_PLATFORM := android-16
APP_ABI := arm64-v8a
APP_STL := c++_shared
APP_CPPFLAGS += -std=c++11 -fexceptions

@DanAlbert
Copy link
Member Author

No it doesn't?

// foo/jni/foo.cpp
int main(int argc, char** argv) {
  return 0;
}
# foo/jni/Android.mk
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := foo
LOCAL_SRC_FILES := foo.cpp
include $(BUILD_EXECUTABLE)
# foo/jni/Application.mk
APP_STL := c++_shared
APP_CPPFLAGS := -std=c++11 -fexceptions
$ android-ndk-r17/ndk-build -C foo APP_ABI=arm64-v8a V=1 APP_LDFLAGS=-v -B
Android NDK: APP_PLATFORM not set. Defaulting to minimum supported version android-14.
make: Entering directory `/work/src/ndk/ndk/foo'
rm -f ./libs/arm64-v8a/* ./libs/armeabi-v7a/* ./libs/x86/* ./libs/x86_64/*
rm -f ./libs/arm64-v8a/gdbserver ./libs/armeabi-v7a/gdbserver ./libs/x86/gdbserver ./libs/x86_64/gdbserver
rm -f ./libs/arm64-v8a/gdb.setup ./libs/armeabi-v7a/gdb.setup ./libs/x86/gdb.setup ./libs/x86_64/gdb.setup
[arm64-v8a] Compile++      : foo <= foo.cpp
/work/src/android-ndk-r17/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -MMD -MP -MF ./obj/local/arm64-v8a/objs/foo/foo.o.d -gcc-toolchain /work/src/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64 -target aarch64-none-linux-android -ffunction-sections -funwind-tables -fstack-protector-strong -fpic -Wno-invalid-command-line-argument -Wno-unused-command-line-argument -no-canonical-prefixes
 -g -fno-exceptions -fno-rtti -O2 -DNDEBUG  -I/work/src/android-ndk-r17/sources/cxx-stl/llvm-libc++/include -I/work/src/android-ndk-r17/sources/cxx-stl/llvm-libc++/../llvm-libc++abi/include -Ijni -std=c++11  -std=c++11 -fexceptions  -DANDROID  -D__ANDROID_API__=21 -Wa,--noexecstack -Wformat -Werror=format-security -fpie   --sysroot /work/src/android-ndk-r17/sysroot -isystem /work/src/android-ndk-r17/sysroot/usr/include/aarch64-linux-android -c  jni/foo.cpp -o ./obj/local/arm64-v8a/objs/foo/foo.o
[arm64-v8a] Prebuilt       : libc++_shared.so <= <NDK>/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/
cp -f /work/src/android-ndk-r17/sources/cxx-stl/llvm-libc++/libs/arm64-v8a/libc++_shared.so obj/local/arm64-v8a/libc++_shared.so
[arm64-v8a] Executable     : foo
/work/src/android-ndk-r17/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -Wl,--gc-sections -Wl,-z,nocopyreloc --sysroot=/work/src/android-ndk-r17/platforms/android-21/arch-arm64 -Wl,-rpath-link=/work/src/android-ndk-r17/platforms/android-21/arch-arm64/usr/lib -Wl,-rpath-link=./obj/local/arm64-v8a ./obj/local/arm64-v8a/objs/foo/foo.o -lgcc -Wl,--exclude-libs,libgcc.a -latomic -Wl,--exclude-libs,libatomic.a ./obj/local/arm64-v8a/libc++_shared.so  -gcc-toolchain /work/src/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64 -target aarch64-none-linux-android -no-canonical-prefixes   -Wl,--build-id -nostdlib++ -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--warn-shared-textrel -Wl,--fatal-warnings -fpie -pie -v  -lc -lm -o ./obj/local/arm64-v8a/foo
Android (4691093 based on r316199) clang version 6.0.2 (https://android.googlesource.com/toolchain/clang 183abd29fc496f55536e7d904e0abae47888fc7f) (https://android.googlesource.com/toolchain/llvm 34361f192e41ed6e4e8f9aca80a4ea7e9856f327) (based on LLVM 6.0.2svn)
Target: aarch64-none-linux-android
Thread model: posix
InstalledDir: /work/src/android-ndk-r17/toolchains/llvm/prebuilt/linux-x86_64/bin
Found candidate GCC installation: /work/src/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x
Selected GCC installation: /work/src/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/work/src/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld" --sysroot=/work/src/android-ndk-r17/platforms/android-21/arch-arm64 -pie --fix-cortex-a53-843419 --eh-frame-hdr -m aarch64linux -dynamic-linker /system/bin/linker64 -o ./obj/local/arm64-v8a/foo /work/src/android-ndk-r17/platforms/android-21/arch-arm64/usr/lib/crtbegin_dynamic.o -L/work/src/android-ndk-r17/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/6.0.2/lib/linux/aarch64 -L/work/src/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x -L/work/src/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/lib/../lib64 -L/work/src/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/lib -L/work/src/android-ndk-r17/platforms/android-21/arch-arm64/usr/lib --gc-sections -z nocopyreloc -rpath-link=/work/src/android-ndk-r17/platforms/android-21/arch-arm64/usr/lib -rpath-link=./obj/local/arm64-v8a ./obj/local/arm64-v8a/objs/foo/foo.o -lgcc --exclude-libs libgcc.a -latomic --exclude-libs libatomic.a ./obj/local/arm64-v8a/libc++_shared.so --build-id --no-undefined -z noexecstack -z relro -z now --warn-shared-textrel --fatal-warnings -lc -lm -lm -lgcc -lgcc -ldl -lc -lgcc -lgcc -ldl /work/src/android-ndk-r17/platforms/android-21/arch-arm64/usr/lib/crtend_android.o
[arm64-v8a] Install        : foo => libs/arm64-v8a/foo
install -p ./obj/local/arm64-v8a/foo ./libs/arm64-v8a/foo
/work/src/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-strip --strip-unneeded  ./libs/arm64-v8a/foo
[arm64-v8a] Install        : libc++_shared.so => libs/arm64-v8a/libc++_shared.so
install -p ./obj/local/arm64-v8a/libc++_shared.so ./libs/arm64-v8a/libc++_shared.so
/work/src/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-strip --strip-unneeded  ./libs/arm64-v8a/libc++_shared.so
make: Leaving directory `/work/src/ndk/ndk/foo'
$ readelf -dW foo/libs/arm64-v8a/foo | grep stdc
$

@cybertk
Copy link

cybertk commented Jun 6, 2018

Here is my error with r17,

[arm64-v8a] Executable     : foo
/root/android-ndk-r17/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -Wl,--gc-sections -Wl,-z,nocopyreloc --sysroot=/root/android-ndk-r17/platforms/android-21/arch-arm64 -Wl,-rpath-link=/root/android-ndk-r17/platforms/android-21/arch-arm64/usr/lib -Wl,-rpath-link=./obj/local/arm64-v8a ./obj/local/arm64-v8a/objs/foo/main.o -lgcc -Wl,--exclude-libs,libgcc.a -latomic -Wl,--exclude-libs,libatomic.a ./obj/local/arm64-v8a/libA.so ./obj/local/arm64-v8a/libB.so ./obj/local/arm64-v8a/libC.so ./obj/local/arm64-v8a/libc++_shared.so  -gcc-toolchain /root/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64 -target aarch64-none-linux-android -no-canonical-prefixes   -Wl,--build-id -nostdlib++ -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--warn-shared-textrel -Wl,--fatal-warnings -fpie -pie   -lc -lm -o ./obj/local/arm64-v8a/foo
/root/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld: ./obj/local/arm64-v8a/foo: hidden symbol `_Unwind_Resume' in /root/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x/libgcc.a(unwind-dw2.o) is referenced by DSO
/root/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld: final link failed: Bad value
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [obj/local/arm64-v8a/foo] Error 1

@DanAlbert
Copy link
Member Author

That doesn't seem to have anything to do with this bug. You'll need to file a new bug with a test case.

@pengzhendong
Copy link

Here is my error with r17,

[arm64-v8a] Executable     : foo
/root/android-ndk-r17/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -Wl,--gc-sections -Wl,-z,nocopyreloc --sysroot=/root/android-ndk-r17/platforms/android-21/arch-arm64 -Wl,-rpath-link=/root/android-ndk-r17/platforms/android-21/arch-arm64/usr/lib -Wl,-rpath-link=./obj/local/arm64-v8a ./obj/local/arm64-v8a/objs/foo/main.o -lgcc -Wl,--exclude-libs,libgcc.a -latomic -Wl,--exclude-libs,libatomic.a ./obj/local/arm64-v8a/libA.so ./obj/local/arm64-v8a/libB.so ./obj/local/arm64-v8a/libC.so ./obj/local/arm64-v8a/libc++_shared.so  -gcc-toolchain /root/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64 -target aarch64-none-linux-android -no-canonical-prefixes   -Wl,--build-id -nostdlib++ -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--warn-shared-textrel -Wl,--fatal-warnings -fpie -pie   -lc -lm -o ./obj/local/arm64-v8a/foo
/root/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld: ./obj/local/arm64-v8a/foo: hidden symbol `_Unwind_Resume' in /root/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x/libgcc.a(unwind-dw2.o) is referenced by DSO
/root/android-ndk-r17/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld: final link failed: Bad value
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [obj/local/arm64-v8a/foo] Error 1

Same error, have you fixed that?

@enh-google
Copy link
Collaborator

r17 is no longer supported. please try again with a current NDK, and file a new bug if you still have trouble with that.

@android android locked as resolved and limited conversation to collaborators Jun 3, 2021
@DanAlbert
Copy link
Member Author

Please file new bugs if you have new bugs. As I responded to the original commenter for that error, it has nothing to do with the topic of this thread. We want to help, but we can't fix r17 and we can't fix bugs we can't see, so we need a bug filed with a repro case to do so.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants