1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 #include <pthread.h>
6 #include <dlfcn.h>
7 #include "libcgo.h"
8
9 // Truncated to a different magic value on 32-bit; that's ok.
10 #define magic1 (0x23581321345589ULL)
11
12 // From https://android.googlesource.com/platform/bionic/+/refs/heads/android10-tests-release/libc/private/bionic_asm_tls.h#69.
13 #define TLS_SLOT_APP 2
14
15 // inittls allocates a thread-local storage slot for g.
16 //
17 // It finds the first available slot using pthread_key_create and uses
18 // it as the offset value for runtime.tls_g.
19 static void
20 inittls(void **tlsg, void **tlsbase)
21 {
22 pthread_key_t k;
23 int i, err;
24 void *handle, *get_ver, *off;
25
26 // Check for Android Q where we can use the free TLS_SLOT_APP slot.
27 handle = dlopen("libc.so", RTLD_LAZY);
28 if (handle == NULL) {
29 fatalf("inittls: failed to dlopen main program");
30 return;
31 }
32 // android_get_device_api_level is introduced in Android Q, so its mere presence
33 // is enough.
34 get_ver = dlsym(handle, "android_get_device_api_level");
35 dlclose(handle);
36 if (get_ver != NULL) {
37 off = (void *)(TLS_SLOT_APP*sizeof(void *));
38 // tlsg is initialized to Q's free TLS slot. Verify it while we're here.
39 if (*tlsg != off) {
40 fatalf("tlsg offset wrong, got %ld want %ld\n", *tlsg, off);
41 }
42 return;
43 }
44
45 err = pthread_key_create(&k, nil);
46 if(err != 0) {
47 fatalf("pthread_key_create failed: %d", err);
48 }
49 pthread_setspecific(k, (void*)magic1);
50 // If thread local slots are laid out as we expect, our magic word will
51 // be located at some low offset from tlsbase. However, just in case something went
52 // wrong, the search is limited to sensible offsets. PTHREAD_KEYS_MAX was the
53 // original limit, but issue 19472 made a higher limit necessary.
54 for (i=0; i<384; i++) {
55 if (*(tlsbase+i) == (void*)magic1) {
56 *tlsg = (void*)(i*sizeof(void *));
57 pthread_setspecific(k, 0);
58 return;
59 }
60 }
61 fatalf("inittls: could not find pthread key");
62 }
63
64 void (*x_cgo_inittls)(void **tlsg, void **tlsbase) = inittls;
65
View as plain text