问题描述
我试图在这个小程序中使用__thread没有运气。任何想法如果这个TLS是支持ndk 10c clang 3.4 / 3.5?同样的程序使用ndk gcc 4.8 / 4.9和原生clang / gcc编译器编译良好。
I am trying to use __thread in this small program without luck. Any idea if this TLS is supported in ndk 10c clang 3.4/3.5? The same program compiles fine with ndk gcc 4.8/4.9 and native clang/gcc compilers.
line -
__thread int counter;
int main () { counter=20; return 0; }
[armeabi] Compile++ thumb: test <= test.cpp
/Users/padlar/android/android-ndk-r10c/toolchains/llvm-3.5/prebuilt/darwin-x86/bin/clang++ -MMD -MP -MF ./obj/local/armeabi/objs/test/test.o.d -gcc-toolchain /Users/padlar/android/android-ndk-r10c/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86 -fpic -ffunction-sections -funwind-tables -fstack-protector -Wno-invalid-command-line-argument -Wno-unused-command-line-argument -no-canonical-prefixes -fno-integrated-as -target armv5te-none-linux-androideabi -march=armv5te -mtune=xscale -msoft-float -fno-exceptions -fno-rtti -mthumb -Os -g -DNDEBUG -fomit-frame-pointer -fno-strict-aliasing -I/Users/padlar/android/android-ndk-r10c/sources/cxx-stl/stlport/stlport -I/Users/padlar/android/android-ndk-r10c/sources/cxx-stl//gabi++/include -Ijni -DANDROID -Wa,--noexecstack -Wformat -Werror=format-security -fPIE -frtti -I/Users/padlar/android/android-ndk-r10c/platforms/android-19/arch-arm/usr/include -c jni/test.cpp -o ./obj/local/armeabi/objs/test/test.o
[armeabi] Executable : test
/Users/padlar/android/android-ndk-r10c/toolchains/llvm-3.5/prebuilt/darwin-x86/bin/clang++ -Wl,--gc-sections -Wl,-z,nocopyreloc --sysroot=/Users/padlar/android/android-ndk-r10c/platforms/android-19/arch-arm -Wl,-rpath-link=/Users/padlar/android/android-ndk-r10c/platforms/android-19/arch-arm/usr/lib -Wl,-rpath-link=./obj/local/armeabi ./obj/local/armeabi/objs/test/test.o /Users/padlar/android/android-ndk-r10c/sources/cxx-stl/stlport/libs/armeabi/thumb/libstlport_static.a -lgcc -gcc-toolchain /Users/padlar/android/android-ndk-r10c/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86 -no-canonical-prefixes -target armv5te-none-linux-androideabi -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -fPIE -pie -L/Users/padlar/android/android-ndk-r10c/platforms/android-19/arch-arm/usr/lib -llog -lc -lm -o ./obj/local/armeabi/test
jni/test.cpp:2: error: undefined reference to '__aeabi_read_tp'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [obj/local/armeabi/test] Error 1
ndk-which objdump
S obj / local / armeabi / objs / test / test.o
ndk-which objdump
-S obj/local/armeabi/objs/test/test.o
obj/local/armeabi/objs/test/test.o: file format elf32-littlearm
Disassembly of section .text.main:
00000000 <main>:
__thread int counter;
int main () { counter=20; return 0; }
0: b580 push {r7, lr}
2: 4904 ldr r1, [pc, #16] ; (14 <main+0x14>)
4: f7ff fffe bl 0 <__aeabi_read_tp>
8: 1840 adds r0, r0, r1
a: 2114 movs r1, #20
c: 6001 str r1, [r0, #0]
e: 2000 movs r0, #0
10: bd80 pop {r7, pc}
12: 46c0 nop ; (mov r8, r8)
14: 00000000 .word 0x00000000
b $ b
gcc4.6反对同一程序
gcc4.6 disass of the same program
ndk-which objdump
-S obj / local / armeabi / objs / test / test.o
ndk-which objdump
-S obj/local/armeabi/objs/test/test.o
obj/local/armeabi/objs/test/test.o: file format elf32-littlearm
Disassembly of section .text.startup.main:
00000000 <main>:
__thread int counter;
int main () { counter=20; return 0; }
0: b508 push {r3, lr}
2: 4804 ldr r0, [pc, #16] ; (14 <main+0x14>)
4: 4478 add r0, pc
6: f7ff fffe bl 0 <__emutls_get_address>
a: 2314 movs r3, #20
c: 6003 str r3, [r0, #0]
e: 2000 movs r0, #0
10: bd08 pop {r3, pc}
12: 46c0 nop ; (mov r8, r8)
14: 0000000c .word 0x0000000c
推荐答案
编辑:从NDK r12开始,Clang支持模拟TLS。虽然上面的技术上仍然是真的(设备上的链接器不支持这一点),编译器可以用pthread调用代替内置TLS支持。
Starting with NDK r12, Clang has support for emulated TLS. While the above is still technically true (the linker on the device does not support this), the compiler can replace builtin TLS support with pthread calls on your behalf.
Android仍然只支持具有简单析构函数的对象的 thread_local
。请参阅有关。这是我们应该能够在不久的将来支持,但是(无论何时该bug中提到的补丁加上一个NDK版本)。
NB: Android still only supports thread_local
for objects with trivial destructors. See discussion on https://github.com/android-ndk/ndk/issues/156#issuecomment-231878690. This is something we should be able to support in the somewhat near future, however (whenever the patch mentioned in that bug lands plus one NDK release).
这篇关于没有运气编译__thread使用ndk clang 3.4 / 3.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!