Skip to content

Commit f18c31a

Browse files
committed
runtime,runtime/cgo: set up TLS storage for Android Q without cgo
Android Q frees a static TLS slot for us to use. Use the offset of that slot as the default for our TLS offset. As a result, runtime/cgo is no more a requirement for Android Q and newer. Updates golang#31343 Updates golang#29674 Change-Id: I759049b2e2865bd3d4fdc05a8cfc6db8b0da1f5d Reviewed-on: https://go-review.googlesource.com/c/go/+/170955 TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Cherry Zhang <[email protected]>
1 parent 973c031 commit f18c31a

5 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/runtime/asm_386.s

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,5 +1564,8 @@ TEXT runtime·panicExtendSlice3CU(SB),NOSPLIT,$0-12
15641564
JMP runtime·goPanicExtendSlice3CU(SB)
15651565

15661566
#ifdef GOOS_android
1567+
// Use the free TLS_SLOT_APP slot #2 on Android Q.
1568+
// Earlier androids are set up in gcc_android.c.
1569+
DATA runtime·tls_g+0(SB)/4, $8
15671570
GLOBL runtime·tls_g+0(SB), NOPTR, $4
15681571
#endif

src/runtime/asm_amd64.s

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,5 +1713,8 @@ TEXT runtime·panicSlice3CU(SB),NOSPLIT,$0-16
17131713
JMP runtime·goPanicSlice3CU(SB)
17141714

17151715
#ifdef GOOS_android
1716+
// Use the free TLS_SLOT_APP slot #2 on Android Q.
1717+
// Earlier androids are set up in gcc_android.c.
1718+
DATA runtime·tls_g+0(SB)/8, $16
17161719
GLOBL runtime·tls_g+0(SB), NOPTR, $8
17171720
#endif

src/runtime/cgo/gcc_android.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ inittls(void **tlsg, void **tlsbase)
4747
{
4848
pthread_key_t k;
4949
int i, err;
50-
void *handle, *get_ver;
50+
void *handle, *get_ver, *off;
5151

5252
// Check for Android Q where we can use the free TLS_SLOT_APP slot.
5353
handle = dlopen("libc.so", RTLD_LAZY);
@@ -60,7 +60,11 @@ inittls(void **tlsg, void **tlsbase)
6060
get_ver = dlsym(handle, "android_get_device_api_level");
6161
dlclose(handle);
6262
if (get_ver != NULL) {
63-
*tlsg = (void *)(TLS_SLOT_APP*sizeof(void *));
63+
off = (void *)(TLS_SLOT_APP*sizeof(void *));
64+
// tlsg is initialized to Q's free TLS slot. Verify it while we're here.
65+
if (*tlsg != off) {
66+
fatalf("tlsg offset wrong, got %ld want %ld\n", *tlsg, off);
67+
}
6468
return;
6569
}
6670

src/runtime/tls_arm.s

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ TEXT setg_gcc<>(SB),NOSPLIT,$0
103103
B runtime·save_g(SB)
104104

105105
#ifdef TLSG_IS_VARIABLE
106+
#ifdef GOOS_android
107+
// Use the free TLS_SLOT_APP slot #2 on Android Q.
108+
// Earlier androids are set up in gcc_android.c.
109+
DATA runtime·tls_g+0(SB)/4, $8
110+
#endif
106111
GLOBL runtime·tls_g+0(SB), NOPTR, $4
107112
#else
108113
GLOBL runtime·tls_g+0(SB), TLSBSS, $4

src/runtime/tls_arm64.s

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ nocgo:
4343
RET
4444

4545
#ifdef TLSG_IS_VARIABLE
46+
#ifdef GOOS_android
47+
// Use the free TLS_SLOT_APP slot #2 on Android Q.
48+
// Earlier androids are set up in gcc_android.c.
49+
DATA runtime·tls_g+0(SB)/8, $16
50+
#endif
4651
GLOBL runtime·tls_g+0(SB), NOPTR, $8
4752
#else
4853
GLOBL runtime·tls_g+0(SB), TLSBSS, $8

0 commit comments

Comments
 (0)