Skip to content

Commit c542681

Browse files
authored
dylink: Add test for relocations within the TLS data (#18641)
Depends on https://reviews.llvm.org/D143020
1 parent 6ac4b2a commit c542681

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

src/library_pthread.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ var LibraryPThread = {
231231
// Called by worker.js each time a thread is started.
232232
threadInitTLS: function() {
233233
#if PTHREADS_DEBUG
234-
dbg('threadInitTLS.');
234+
dbg('threadInitTLS');
235235
#endif
236236
// Call thread init functions (these are the _emscripten_tls_init for each
237237
// module loaded.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <assert.h>
2+
#include <pthread.h>
3+
#include <stdio.h>
4+
5+
int foo = 3;
6+
// This should generate a relocation in data section
7+
static int* _Atomic foo_addr = &foo;
8+
// This should generate a reloction in the TLS data section
9+
static _Thread_local int *tls_foo_addr = &foo;
10+
11+
void run_test() {
12+
printf("&foo : %p\n", &foo);
13+
printf("&foo_addr : %p\n", &foo_addr);
14+
printf("&tls_foo_addr: %p\n", &tls_foo_addr);
15+
// The values stored foo_addr and tls_foo_addr should both be
16+
// the address of foo
17+
printf("foo_add=%p tls_foo_addr=%p [delta=%ld]\n", foo_addr, tls_foo_addr, foo_addr - tls_foo_addr);
18+
assert(foo_addr == &foo);
19+
assert(tls_foo_addr == &foo);
20+
}
21+
22+
void* thread_main(void* arg) {
23+
run_test();
24+
return NULL;
25+
}
26+
27+
int main() {
28+
run_test();
29+
30+
pthread_t t;
31+
pthread_create(&t, NULL, thread_main, NULL);
32+
pthread_join(t, NULL);
33+
34+
printf("done\n");
35+
return 0;
36+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
done

test/test_core.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2849,6 +2849,13 @@ def test_pthread_proxy_to_pthread(self):
28492849
self.set_setting('EXIT_RUNTIME')
28502850
self.do_run_in_out_file_test(test_file('pthread/test_pthread_proxy_to_pthread.c'))
28512851

2852+
@node_pthreads
2853+
@needs_dylink
2854+
def test_pthread_tls_dylink(self):
2855+
self.set_setting('MAIN_MODULE', 2)
2856+
self.emcc_args.append('-Wno-experimental')
2857+
self.do_run_in_out_file_test('pthread/test_pthread_tls_dylink.c')
2858+
28522859
def test_tcgetattr(self):
28532860
self.do_runf(test_file('termios/test_tcgetattr.c'), 'success')
28542861

0 commit comments

Comments
 (0)