Thread overview
Trouble with Android and arsd.jni
Sep 10
burt
Sep 10
kinke
Sep 10
burt
Sep 10
kinke
September 10
Hello,

I'm trying to upgrade and improve an Android project I was working on a while ago. For this reason, I decided to upgrade my compiler to the newest LDC (v1.23.0).

I am using the arsd.jni library for the JNI headers and for initializing the runtime. However, I am getting linker errors, telling me that _tlsend, _tlsstart and __bss_end__ are missing.

So I tried to fix these by adding empty declarations for them myself (as I saw in another post):

```d
extern(C) __gshared
{
    @section(".tdata")
    int _tlsstart = 0;
    @section(".tcommon")
    int _tlsend = 0;
}
```

However, when doing this, it causes my app to crash, specifically on jni.d:1033 when calling `Runtime.initialize()`. The stack trace shows there is an assertion error in rt.sections_android in the foreach body of `getStaticTLSRange`.

I also tried adding an empty `void main() {}` instead, with the same result.

PR #2991 in dlang/druntime seems to have changed the way the TLS is emulated on Android or something. I have no clue what is going on and how to fix these errors, so if someone could help that would be much appreciated.

Thank you.
September 10
On Thursday, 10 September 2020 at 11:16:55 UTC, burt wrote:
> However, I am getting linker errors, telling me that _tlsend, _tlsstart and __bss_end__ are missing.

Perhaps you happen to use some stale artifacts? These magic symbols aren't used anymore in druntime since LDC v1.21, and not defined by the compiler anymore. You also don't need the dummy main() anymore. The object file containing the undefined references should shed some light on what's still referencing them.
September 10
On Thursday, 10 September 2020 at 11:58:51 UTC, kinke wrote:
> On Thursday, 10 September 2020 at 11:16:55 UTC, burt wrote:
>> However, I am getting linker errors, telling me that _tlsend, _tlsstart and __bss_end__ are missing.
>
> Perhaps you happen to use some stale artifacts? These magic symbols aren't used anymore in druntime since LDC v1.21, and not defined by the compiler anymore. You also don't need the dummy main() anymore. The object file containing the undefined references should shed some light on what's still referencing them.

I'm not sure if this was the cause, but I believe I was using old libdruntime-ldc.a and libphobos2-ldc.a files which where downloaded from before v1.21. So that issue is fixed.

However, the app is still crashing when I load it, and there appears to be an issue in Runtime.initialize(), which is called from JNI_OnLoad(), which is defined in arsd.jni. The debugger tells me that it was calling `getStaticTLSRange`, which calls `safeAssert` in the `__foreachbody`, which fails and eventually aborts.
September 10
On Thursday, 10 September 2020 at 13:14:00 UTC, burt wrote:
> However, the app is still crashing when I load it, and there appears to be an issue in Runtime.initialize(), which is called from JNI_OnLoad(), which is defined in arsd.jni. The debugger tells me that it was calling `getStaticTLSRange`, which calls `safeAssert` in the `__foreachbody`, which fails and eventually aborts.

That safeAssert would print a useful message, but I guess you can obtain the msg/reason via debugging as well. - This is most likely due to *not* using the required bfd linker (which is used by default when linking via LDC and otherwise selectable via `-linker=bfd`). If you're linking manually via clang, try `-fuse-ld=bfd`.