www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.ldc - Android tls issue

reply Newbie2019 <newbie2019 gmail.com> writes:
_tlsstart/_tlsend in the file druntime/src/rt/sections_android.d 
defined with extern:

extern(C)
{
     /* Symbols created by the compiler/linker and inserted into 
the
      * object file that 'bracket' sections.
      */
     extern __gshared
     {
         void* __start_deh;
         void* __stop_deh;
         version (LDC)
         {
             void* __start___minfo;
             void* __stop___minfo;
         }
         else
         {
             void* __start_minfo;
             void* __stop_minfo;
         }

         version(X86) size_t _end;
         else version(X86_64) size_t _end;
         else version(ARM) size_t __bss_end__;
         else version(AArch64) size_t __bss_end__;
         else static assert( false, "Android architecture not 
supported." );

         int _tlsstart;
         int _tlsend;
     }
}

  I can not find the position of _tlsstart/_tlsend.  I try build 
this example get this error:

extern(C) __gshared extern {
         int _tlsstart;
         int _tlsend;
}
int tls_v1 = 1;
void main(string[] args){
}

/data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: 
/data/data/com.termux/files/usr/bin/../lib/libdruntime-ldc.a(
ections_android.o): in function `_D2rt16sections_android12initSectionsFNbNiZv':
sections_android.d:(.text._D2rt16sections_android12initSectionsFNbNiZv[_D2rt16sections_android12initSec
ionsFNbNiZv]+0x54): undefined reference to `_tlsend'
/data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: 
sections_android.d:(.text._D2rt16sections_android12initSectionsFNbNiZv[_D2rt16sections_android12initSec
ionsFNbNiZv]+0x64): undefined reference to `_tlsend'
/data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: 
/data/data/com.termux/files/usr/bin/../lib/libdruntime-ldc.a(
ections_android.o): in function `__tls_get_addr':
sections_android.d:(.text.__tls_get_addr[__tls_get_addr]+0x50): 
undefined reference to `_tlsstart'
/data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: 
sections_android.d:(.text.__tls_get_addr[__tls_get_addr]+0x58): 
undefined reference to `_tlsstart'
/data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: 
sections_android.d:(.text.__tls_get_addr[__tls_get_addr]+0x6c): 
undefined reference to `_tlsend'
/data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: 
sections_android.d:(.text.__tls_get_addr[__tls_get_addr]+0x70): 
undefined reference to `_tlsend'
clang-8: error: linker command failed with exit code 1 (use -v to 
see invocation)
Error: /data/data/com.termux/files/usr/bin/gcc failed with 
status: 1

Is this a bugs ?  or the _tlsstart/_tlsend is some symbols import 
from android system lib ?
Jun 23 2019
next sibling parent Newbie2019 <newbie2019 gmail.com> writes:
On Sunday, 23 June 2019 at 15:34:40 UTC, Newbie2019 wrote:
 _tlsstart/_tlsend in the file 
 druntime/src/rt/sections_android.d defined with extern:
build ldc2 on Android device get this error: [107/161] Generating bin/ldc2 FAILED: bin/ldc2 cd /data/data/com.termux/files/home/d/tmp && /data/data/com.termux/files/usr/bin/c++ -o /data/data/com.termux/files/home/d/tmp/bin/ldc2 /data/data/com.termux/files/home/d/tmp/obj/ldc2.o /data/data/com.termux/files/home/d/tmp/lib/libldc.a -lLLVM-8 -L/data/data/com.termux/files/usr/lib -L/data/data/com.termux/files/usr/bin/../lib -lphobos2-ldc -ldruntime-ldc -Wl,--gc-sections -ldl -lm /data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: /data/data/com.termux/files/usr/lib/libdruntime-ldc.a(sections_android.o): in function `_D2rt16sections_android12initSectionsFNbNiZv': sections_android.d:(.text._D2rt16sections_android12initSectionsFNbNiZv[_D2rt16sections_android12initSec ionsFNbNiZv]+0x54): undefined reference to `_tlsend' /data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: sections_android.d:(.text._D2rt16sections_android12initSectionsFNbNiZv[_D2rt16sections_android12initSec ionsFNbNiZv]+0x64): undefined reference to `_tlsend' /data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: /data/data/com.termux/files/usr/lib/libdruntime-ldc.a(sections_android.o): in function `__tls_get_addr': sections_android.d:(.text.__tls_get_addr[__tls_get_addr]+0x50): undefined reference to `_tlsstart' /data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: sections_android.d:(.text.__tls_get_addr[__tls_get_addr]+0x58): undefined reference to `_tlsstart' /data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: sections_android.d:(.text.__tls_get_addr[__tls_get_addr]+0x6c): undefined reference to `_tlsend' /data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: sections_android.d:(.text.__tls_get_addr[__tls_get_addr]+0x70): undefined reference to `_tlsend' clang-8: error: linker command failed with exit code 1 (use -v to see invocation) ninja: build stopped: subcommand failed.
Jun 23 2019
prev sibling parent reply kinke <noone nowhere.com> writes:
On Sunday, 23 June 2019 at 15:34:40 UTC, Newbie2019 wrote:
 Is this a bugs ?  or the _tlsstart/_tlsend is some symbols 
 import from android system lib ?
These 2 symbols are inserted by the compiler into the object file containing the D main function, see https://run.dlang.io/is/QDwmHM. What triple are you using? Make sure there's an `-android` at the end.
Jun 23 2019
next sibling parent Newbie2019 <newbie2019 gmail.com> writes:
On Sunday, 23 June 2019 at 17:09:32 UTC, kinke wrote:
 On Sunday, 23 June 2019 at 15:34:40 UTC, Newbie2019 wrote:
 Is this a bugs ?  or the _tlsstart/_tlsend is some symbols 
 import from android system lib ?
These 2 symbols are inserted by the compiler into the object file containing the D main function, see https://run.dlang.io/is/QDwmHM. What triple are you using? Make sure there's an `-android` at the end.
here is my cmake options: D_FLAGS:STRING=-mtriple=aarch64-unknown-linux-android LDC_TARGET_PRESET:STRING=Android-aarch64 TARGET_SYSTEM:STRING=Android But when I run ninjia -8, the D_FLAGS seems not used: /data/data/com.termux/files/usr/bin/ldmd2 -c -wi -O -inline -release -J/data/data/com.termux/files/home/d/ldc/res -I/data/data/com.termux/files/home/d/ldc -I/data/data/com.termux/files/home/d/tmp -version=IN_LLVM -of/data/data/com.termux/files/home/d/tmp/obj/ldc2.o /data/data/com.termux/files/home/d/ldc/dmd/access.d /data/data/com.termux/files/home/d/ldc/dmd/aggregate.d /data/data/com.termux/files/home/d/ldc/dmd/aliasthis.d /data/data/com.termux/files/home/d/ldc/dmd/apply.d /data/data/com.termux/files/home/d/ldc/dmd/argtypes.d /data/data/com.termux/files/home/d/ldc/dmd/argtypes_sysv_x64.d /data/data/com.termux/files/home/d/ldc/dmd/arrayop.d /data/data/com.termux/files/home/d/ldc/dmd/arraytypes.d /data/data/com.termux/files/home/d/ldc/dmd/ast_node.d /data/data/com.termux/files/home/d/ldc/dmd/astcodegen.d /data/data/com.termux/files/home/d/ldc/dmd/attrib.d /data/data/com.termux/files/home/d/ldc/dmd/blockexit.d /data/data/com.termux/files/home/d/ldc/dmd/builtin.d /data/data/com.termux/files/home/d/ldc/dmd/canthrow.d /data/data/com.termux/files/home/d/ldc/dmd/cli.d /data/data/com.termux/files/home/d/ldc/dmd/clone.d /data/data/com.termux/files/home/d/ldc/dmd/compiler.d /data/data/com.termux/files/home/d/ldc/dmd/complex.d /data/data/com.termux/files/home/d/ldc/dmd/cond.d /data/data/com.termux/files/home/d/ldc/dmd/console.d /data/data/com.termux/files/home/d/ldc/dmd/constfold.d /data/data/com.termux/files/home/d/ldc/dmd/cppmangle.d /data/data/com.termux/files/home/d/ldc/dmd/cppmanglewin.d /data/data/com.termux/files/home/d/ldc/dmd/ctfeexpr.d /data/data/com.termux/files/home/d/ldc/dmd/ctorflow.d /data/data/com.termux/files/home/d/ldc/dmd/dcast.d /data/data/com.termux/files/home/d/ldc/dmd/dclass.d /data/data/com.termux/files/home/d/ldc/dmd/declaration.d /data/data/com.termux/files/home/d/ldc/dmd/delegatize.d /data/data/com.termux/files/home/d/ldc/dmd/denum.d /data/data/com.termux/files/home/d/ldc/dmd/dimport.d /data/data/com.termux/files/home/d/ldc/dmd/dinterpret.d /data/data/com.termux/files/home/d/l 32419 u0_a167 0:09 /data/data/com.termux/files/usr/bin/ldc2 -ldmd -c -wi -O -enable-inlining -Hkeep-all-bodies -release -J/data/data/com.termux/files/home/d/ldc/res -I/data/data/com.termux/files/home/d/ldc -I/data/data/com.termux/files/home/d/tmp -d-version=IN_LLVM -of/data/data/com.termux/files/home/d/tmp/obj/ldc2.o /data/data/com.termux/files/home/d/ldc/dmd/access.d /data/data/com.termux/files/home/d/ldc/dmd/aggregate.d /data/data/com.termux/files/home/d/ldc/dmd/aliasthis.d /data/data/com.termux/files/home/d/ldc/dmd/apply.d /data/data/com.termux/files/home/d/ldc/dmd/argtypes.d /data/data/com.termux/files/home/d/ldc/dmd/argtypes_sysv_x64.d /data/data/com.termux/files/home/d/ldc/dmd/arrayop.d /data/data/com.termux/files/home/d/ldc/dmd/arraytypes.d /data/data/com.termux/files/home/d/ldc/dmd/ast_node.d /data/data/com.termux/files/home/d/ldc/dmd/astcodegen.d /data/data/com.termux/files/home/d/ldc/dmd/attrib.d /data/data/com.termux/files/home/d/ldc/dmd/blockexit.d /data/data/com.termux/files/home/d/ldc/dmd/builtin.d /data/data/com.termux/files/home/d/ldc/dmd/canthrow.d /data/data/com.termux/files/home/d/ldc/dmd/cli.d /data/data/com.termux/files/home/d/ldc/dmd/clone.d /data/data/com.termux/files/home/d/ldc/dmd/compiler.d /data/data/com.termux/files/home/d/ldc/dmd/complex.d /data/data/com.termux/files/home/d/ldc/dmd/cond.d /data/data/com.termux/files/home/d/ldc/dmd/console.d /data/data/com.termux/files/home/d/ldc/dmd/constfold.d /data/data/com.termux/files/home/d/ldc/dmd/cppmangle.d /data/data/com.termux/files/home/d/ldc/dmd/cppmanglewin.d /data/data/com.termux/files/home/d/ldc/dmd/ctfeexpr.d /data/data/com.termux/files/home/d/ldc/dmd/ctorflow.d /data/data/com.termux/files/home/d/ldc/dmd/dcast.d /data/data/com.termux/files/home/d/ldc/dmd/dclass.d /data/data/com.termux/files/home/d/ldc/dmd/declaration.d /data/data/com.termux/files/home/d/ldc/dmd/delegatize.d /data/data/com.termux/files/home/d/ldc/dmd/denum.d /data/data/com.termux/files/home/d/ldc/dmd/dimport.d /data/data/com.termux/files/home/d/ldc/dmd/dinterpret.d /d try with example file from termux also get this error: ldc2 -mtriple=aarch64-unknown-linux-android test.d ldc2 -mtriple=aarch64-linux-android test.d /data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: /data/data/com.termux/files/usr/bin/../lib/libdruntime-ldc.a( ections_android.o): in function `_D2rt16sections_android12initSectionsFNbNiZv': sections_android.d:(.text._D2rt16sections_android12initSectionsFNbNiZv[_D2rt16sections_android12initSec ionsFNbNiZv]+0x54): undefined reference to `_tlsend' /data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: sections_android.d:(.text._D2rt16sections_android12initSectionsFNbNiZv[_D2rt16sections_android12initSec ionsFNbNiZv]+0x64): undefined reference to `_tlsend' /data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: /data/data/com.termux/files/usr/bin/../lib/libdruntime-ldc.a( ections_android.o): in function `__tls_get_addr': sections_android.d:(.text.__tls_get_addr[__tls_get_addr]+0x50): undefined reference to `_tlsstart' /data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: sections_android.d:(.text.__tls_get_addr[__tls_get_addr]+0x58): undefined reference to `_tlsstart' /data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: sections_android.d:(.text.__tls_get_addr[__tls_get_addr]+0x6c): undefined reference to `_tlsend' /data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: sections_android.d:(.text.__tls_get_addr[__tls_get_addr]+0x70): undefined reference to `_tlsend' clang-8: error: linker command failed with exit code 1 (use -v to see invocation) Error: /data/data/com.termux/files/usr/bin/gcc failed with status: 1
Jun 23 2019
prev sibling parent reply Newbie2019 <newbie2019 gmail.com> writes:
On Sunday, 23 June 2019 at 17:09:32 UTC, kinke wrote:
 These 2 symbols are inserted by the compiler into the object 
 file containing the D main function, see 
 https://run.dlang.io/is/QDwmHM. What triple are you using? Make 
 sure there's an `-android` at the end.
Then I build normal example with TLS var on android, it work as expect. But when I try build ldc it self from Android, I get this error. (because the source code link with druntime/src/rt/sections_android.d, and there is no symbols of _tlsstart/_tlsend) The cmake force to use ldmd2 and there is no `-mtriple=aarch64-unknown-linux-android` pass to ldmd2 when I run ninja. Then I try cross build from macOS get this error: ldc-build-runtime --ninja --targetPreset=Android-aarch64 --buildDir=/opt/local/droid/droid64 core/sys/posix/aio.d(481): Error: undefined identifier `aiocb` core/sys/posix/aio.d(482): Error: undefined identifier `aiocb` core/sys/posix/aio.d(483): Error: undefined identifier `aiocb` core/sys/posix/aio.d(484): Error: undefined identifier `aiocb` core/sys/posix/aio.d(485): Error: undefined identifier `aiocb` core/sys/posix/aio.d(486): Error: undefined identifier `aiocb` core/sys/posix/aio.d(487): Error: undefined identifier `aiocb` core/sys/posix/aio.d(488): Error: undefined identifier `aiocb` ninja: build stopped: subcommand failed. Error: command failed with status 1 try build example with cross betterC: export CC=/Users/android/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang ldmd2 -L-L/opt/local/droid/droid64/lib -Xcc=--sysroot=/Users/android/Library/Android/sdk/ndk-bundle/platforms/a droid-27/arch-arm64 -Xcc=-target -Xcc=aarch64-none-linux-android -Xcc=-fpie -Xcc=-pie -betterC test_android_tls.d -mtriple=aarch64-unknown-linux-android test_android_tls.d:(.text.main[main]+0x14): undefined reference to `_tlsstart'
Jun 23 2019
parent reply kinke <kinke gmx.net> writes:
On Monday, 24 June 2019 at 03:10:40 UTC, Newbie2019 wrote:
 Then I build normal example with TLS var on android, it work as 
 expect.
Okay, that's what I expected (although your earlier posts seem to contradict this).
 But when I try build ldc it self from Android, I get this error.
Yeah, LDC and LDMD themselves don't have a D main anymore since v1.15, so they don't get these 2 symbols automatically anymore. Our Android guy has left the scene, so we haven't been aware of this.
Jun 24 2019
parent reply Newbie2019 <newbie2019 gmail.com> writes:
On Monday, 24 June 2019 at 09:38:36 UTC, kinke wrote:
 On Monday, 24 June 2019 at 03:10:40 UTC, Newbie2019 wrote:
 Then I build normal example with TLS var on android, it work 
 as expect.
Okay, that's what I expected (although your earlier posts seem to contradict this).
 But when I try build ldc it self from Android, I get this 
 error.
Yeah, LDC and LDMD themselves don't have a D main anymore since v1.15, so they don't get these 2 symbols automatically anymore. Our Android guy has left the scene, so we haven't been aware of this.
Thanks for explain, I will try some work around.
Jun 24 2019
parent reply kinke <noone nowhere.com> writes:
On Monday, 24 June 2019 at 17:29:02 UTC, Newbie2019 wrote:
 I will try some work around.
You could try adding the following to driver/main.d: import ldc.attributes; extern (C) __gshared { section(".tdata") int _tlsstart = 0; section(".tcommon") int _tlsend = 0; }
Jun 24 2019
next sibling parent reply Newbie2019 <newbie2019 gmail.com> writes:
On Monday, 24 June 2019 at 18:31:36 UTC, kinke wrote:
 On Monday, 24 June 2019 at 17:29:02 UTC, Newbie2019 wrote:
 I will try some work around.
You could try adding the following to driver/main.d: import ldc.attributes; extern (C) __gshared { section(".tdata") int _tlsstart = 0; section(".tcommon") int _tlsend = 0; }
This solve the _tlsstart/_tlsend problem, now I get a ldc2 binary for aarch64. when I use the new ldc2 built example, I get this error: ldc2 sieve.d /data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: _tlsstart: TLS definition in sieve.o section .tdata mismatches non-TLS reference in /data/data/com.termux/files/home/d/tmp/lib/libdruntime-ldc.a(sections_android.o) /data/data/com.termux/files/usr/bin/aarch64-linux-android-ld: /data/data/com.termux/files/home/d/tmp/lib/libdruntime-ldc.a: error adding symbols: bad value clang-8: error: linker command failed with exit code 1 (use -v to see invocation) Error: /data/data/com.termux/files/usr/bin/gcc failed with status: 1 ldc2 sieve.d -Xcc=-fuse-ld=lld -Xcc=-L/system/lib64 ld.lld: error: undefined symbol: __bss_end__
 referenced by sections_android.d
               
 sections_android.o:(_D2rt16sections_android12initSectionsFNbNiZv) in archive
/data/data/com.termux/files/home/d/tmp/lib/libdruntime-ldc.a
ld.lld: error: undefined symbol: __bss_end__
 referenced by sections_android.d
               
 sections_android.o:(_D2rt16sections_android12initSectionsFNbNiZv) in archive
/data/data/com.termux/files/home/d/tmp/lib/libdruntime-ldc.a
clang-8: error: linker command failed with exit code 1 (use -v to see invocation) Error: /data/data/com.termux/files/usr/bin/gcc failed with status: 1 Link with lld report there is no symbol __bss_end__, I guess we need patch driver/codegenerator.cpp to fix this error ? I add ` emitSymbolAddrGlobal(ir_->module, "__bss_end__", "_d_execBssEndAddr");` after the `endSymbol` define, but not work. any suggestion to fix this __bss_end__ issue for Android ?
Jun 24 2019
parent reply kinke <noone nowhere.com> writes:
On Monday, 24 June 2019 at 22:24:47 UTC, Newbie2019 wrote:
 any suggestion to fix this __bss_end__ issue for Android ?
Just don't use LLD for now ;); it's not usable for macOS either due to missing magic linker symbols.
 when I use the new ldc2 built example, I get this error
That's the more interesting part. Do you happen not to be using our LLVM fork with custom TLS emulation on Android? [1] It's required for Android. [1] https://github.com/ldc-developers/llvm/releases
Jun 24 2019
parent Newbie2019 <newbie2019 gmail.com> writes:
On Monday, 24 June 2019 at 23:23:30 UTC, kinke wrote:
 That's the more interesting part. Do you happen not to be using 
 our LLVM fork with custom TLS emulation on Android? [1] It's 
 required for Android.

 [1] https://github.com/ldc-developers/llvm/releases
I use termux llvm, I will try build with ldc-llvm.
Jun 24 2019
prev sibling parent Newbie2019 <newbie2019 gmail.com> writes:
On Monday, 24 June 2019 at 18:31:36 UTC, kinke wrote:
 You could try adding the following to driver/main.d:
Correct me if I am wrong. Some how the ldc change the auto generate _tlsstart to TLS, but it referenced as non-TLS in sections_android.o cause the problem ? I change _tlsstart to tls in sections_android.d the ldc can generate binary, but Segmentation fault at _D3std11concurrency10MessageBox3putMFKSQBlQBk7MessageZv () There also a ldc Segmentation: ---------------- pragma(LDC_no_moduleinfo); pragma(LDC_no_typeinfo) void main(){} --------------- ldmd2 dmain.d -c -mtriple=aarch64-linux-android 0 ldc2 0x0000000108425768 llvm::sys::PrintStackTrace(llvm::raw_ostream&) + 40 1 ldc2 0x0000000108424887 llvm::sys::RunSignalHandlers() + 39 2 ldc2 0x0000000108425d78 SignalHandler(int) + 264 3 libsystem_platform.dylib 0x00007fff66d20b5d _sigtramp + 29 4 ldc2 0x0000000108ea819e GCC_except_table13 + 702442 5 ldc2 0x000000010814dabe llvm::Mangler::getNameWithPrefix(llvm::SmallVectorImpl<char>&, llvm::GlobalValue const*, bool) const + 110 6 ldc2 0x0000000107a14496 llvm::TargetMachine::getSymbol(llvm::GlobalValue const*) const + 134 7 ldc2 0x00000001076f4294 llvm::AsmPrinter::EmitGlobalVariable(llvm::GlobalVariable const*) + 180 8 ldc2 0x00000001076f9088 llvm::AsmPrinter::doFinalization(llvm::Module&) + 104 9 ldc2 0x0000000108142e63 llvm::FPPassManager::doFinalization(llvm::Module&) + 51 10 ldc2 0x00000001081432c3 llvm::legacy::PassManagerImpl::run(llvm::Module&) + 1091 11 ldc2 0x00000001067af18b (anonymous namespace)::codegenModule(llvm::TargetMachine&, llvm::Module&, llvm::raw_fd_ostream&, llvm::TargetMachine::CodeGenFileType) + 283 12 ldc2 0x00000001067aeb7c writeModule(llvm::Module*, char const*) + 4092 13 ldc2 0x00000001067a8945 ldc::CodeGenerator::writeAndFreeLLModule(char const*) + 1541 14 ldc2 0x00000001067a8fce ldc::CodeGenerator::emit(Module*) + 782 15 ldc2 0x00000001067c264d codegenModules(Array<Module*>&) + 237 16 ldc2 0x00000001065715dd mars_mainBody(Param&, Array<char const*>&, Array<char const*>&) + 5309 Error: Error executing /opt/local/bin/ldc2: Segmentation fault: 11
Jun 24 2019