Skip to content

Instantly share code, notes, and snippets.

@depressed-pho
Last active April 14, 2024 04:13
Show Gist options
  • Save depressed-pho/b6894fdaef94a1b9aa5459b1a2f65590 to your computer and use it in GitHub Desktop.
Save depressed-pho/b6894fdaef94a1b9aa5459b1a2f65590 to your computer and use it in GitHub Desktop.
I thought there was an issue in TLS handling but it doesn't reproduce with this code...
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
typedef int (*get_foo_t)();
int main() {
void* lib = dlopen("./libtls.so", RTLD_LAZY);
if (!lib) {
printf("%s", dlerror());
exit(1);
}
get_foo_t get_foo = dlsym(lib, "get_foo");
if (!get_foo) {
printf("%s", dlerror());
exit(1);
}
printf("foo = %d\n", get_foo());
return 0;
}
.PHONY: run
run: dlopen-tls-test libtls.so
./dlopen-tls-test
.PHONY: clean
clean:
rm -f dlopen-tls-test *.so
dlopen-tls-test: main.c
cc -o $@ main.c
libtls.so: tls.c
cc -shared -fPIC -o $@ tls.c
% make
cc -o dlopen-tls-test main.c
cc -shared -fPIC -o libtls.so tls.c
./dlopen-tls-test
foo = -1
static __thread int foo = -1;
int get_foo() {
return foo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment