How to override pthread_self() (link-time issue)
Hello,
I'm implementing my own pthread_*() functions for use by a huge project.
It works (compiles, links and runs with my own functions) for almost all pthread_*() that were redefined except pthread_self(), where the linking fails:
error: Linking globals named 'pthread_self': symbol multiply defined!
I'm not really familiar to emscripten internals, and I struggle trying to overcome this.
The project is quite huge with tons of dependencies, so a compile time -Dpthread_self=my_pthread_self to expose my_pthread_self is not really an option.
Thanks!
If you implement all your own pthreads functions, then it should not link in the pthreads system lib, which is probably what's causing that error. Maybe you forgot to implement one or two of those methods. To debug this, look at debug logging (EMCC_DEBUG=1 in the env), and also add -s VERBOSE=1 to emcc, and then it will log system library linking decision info (e.g. considering libcxx.a is the line where it checks if to link in libc++`).
The original post was indeed misleading. I haven't redefined all of the functions in <pthread.h> (and also <sched.h>), but most of them. Those that are redefined are actually used (that is, they are linked in instead of the libc ones, and do get invoked instead of the default ones), yet the linker fails for pthread_self() with the message reported in the original post.
Here's the exhaustive list of those that I did redefine:
int pthread_attr_init(pthread_attr_t *attr)
int pthread_attr_setaffinity_np(pthread_attr_t *attr, size_t cpusetsize, const cpu_set_t *cpuset)
int pthread_attr_getaffinity_np(const pthread_attr_t *attr, size_t cpusetsize, cpu_set_t *cpuset)
int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
int pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
int pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched)
int pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched)
int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
int pthread_attr_setscope(pthread_attr_t *attr, int scope)
int pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize)
int pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, size_t *stacksize)
int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
int pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
int pthread_getattr_np(pthread_t thread, pthread_attr_t *attr)
int pthread_getattr_default_np(pthread_attr_t *attr)
int pthread_setattr_default_np(pthread_attr_t *attr)
int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param)
int pthread_setschedprio(pthread_t thread, int prio)
int pthread_setconcurrency(int new_level)
int pthread_getconcurrency(void)
int pthread_getcpuclockid(pthread_t id, clockid_t *clock_id)
pthread_t pthread_self(void)
int pthread_equal(pthread_t a, pthread_t b)
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg)
int pthread_detach(pthread_t id)
void pthread_exit(void *value_ptr)
int pthread_cancel(pthread_t id)
int pthread_key_create(pthread_key_t *key, void (*destructor)(void*))
int pthread_key_delete(pthread_key_t key)
int pthread_setspecific(pthread_key_t key, const void *value)
void *pthread_getspecific(pthread_key_t key)
int pthread_join(pthread_t id, void **retval)
void pthread_cleanup_push(void (*routine)(void *), void *arg)
void pthread_cleanup_pop(int execute)
int pthread_setcancelstate(int state, int *oldstate)
int thread_setcanceltype(int type, int *oldtype)
void pthread_testcancel(void)
int sched_yield(void)
I linked with environment EMCC_DEBUG set to 1 and with -s VERBOSE=1, yet the output is 19Mb of text. Here's an excerpt that might be relevant:
...
DEBUG:root:including libcxx.a
DEBUG:root:including libcxxabi.bc
DEBUG:root:including libc.bc
DEBUG:root:including wasm-libc.bc
DEBUG:root:emcc step "calculate system libraries" took 3.07 seconds
DEBUG:root:linking: [<tons of project objects and prebuilt libs>, '/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/tmpHiSdtq/bind_0_1.o', '/Users/vedad/.emscripten_cache/asmjs/dlmalloc_debug.bc', '/Users/vedad/.emscripten_cache/asmjs/libc.bc', '/Users/vedad/.emscripten_cache/asmjs/wasm-libc.bc', '/Users/vedad/.emscripten_cache/asmjs/libcxx.a', '/Users/vedad/.emscripten_cache/asmjs/libcxxabi.bc']
Followed by tons of
DEBUG:root:adding object <project objects>
Including this one that holds the above mentioned redefined pthread_*() functions.
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_zxEkeq_archive_contents/EmscriptenPthread.cpp.o to link
Followed by:
DEBUG:root:adding object /var/folders/ld/lg90tl916h90s068qh2029040000gn/T/tmpHiSdtq/bind_0_1.o to link
DEBUG:root:adding object /Users/vedad/.emscripten_cache/asmjs/dlmalloc_debug.bc to link
DEBUG:root:adding object /Users/vedad/.emscripten_cache/asmjs/libc.bc to link
DEBUG:root:adding object /Users/vedad/.emscripten_cache/asmjs/wasm-libc.bc to link
DEBUG:root:considering archive /Users/vedad/.emscripten_cache/asmjs/libcxx.a
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/algorithm_28f09b36.cpp.o to link
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/chrono_4e7b4271.cpp.o to link
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/condition_variable_de2a3bbf.cpp.o to link
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/exception_d748e044.cpp.o to link
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/hash_66513507.cpp.o to link
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/ios_a50879ca.cpp.o to link
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/iostream_42eabfa4.cpp.o to link
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/locale_31508eac.cpp.o to link
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/memory_446816ce.cpp.o to link
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/mutex_b5c8bf01.cpp.o to link
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/new_b97df972.cpp.o to link
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/random_c5613c3f.cpp.o to link
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/regex_672ea6e5.cpp.o to link
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/stdexcept_8787beb6.cpp.o to link
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/string_067845ed.cpp.o to link
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/system_error_0ef5f8f0.cpp.o to link
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/thread_56db025b.cpp.o to link
DEBUG:root:adding object /private/var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp_ibfiyn_archive_contents/future_ca8f06ba.cpp.o to link
DEBUG:root:done running loop of archive /Users/vedad/.emscripten_cache/asmjs/libcxx.a
DEBUG:root:adding object /Users/vedad/.emscripten_cache/asmjs/libcxxabi.bc to link
DEBUG:root:using response file for llvm-link
DEBUG:root:emcc step "link" took 11.65 seconds
DEBUG:root:saving intermediate processing steps to /var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp
DEBUG:root:(not saving intermediate /var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp/emcc-0-basebc.bc because deferring linking)
DEBUG:root:emcc: LLVM opts: -strip-debug -disable-debug-info-type-map -internalize -internalize-public-api-list=main,__cxa_demangle,malloc,free,__errno_location,fflush,__cxa_can_catch,__cxa_is_pointer_type,malloc,free,malloc,malloc,malloc,free,realloc,malloc,free,malloc,htonl,htons,ntohs,free,htons,malloc,malloc,htons,htonl,htons,ntohs,htonl,htons,ntohs,memalign,free -globaldce -disable-loop-vectorization -disable-slp-vectorization -vectorize-loops=false -vectorize-slp=false -vectorize-slp-aggressive=false [num inputs: 1]
DEBUG:root:emcc step "post-link" took 44.67 seconds
DEBUG:root:LLVM => JS
DEBUG:root:emscript: llvm backend: /Users/vedad/Dropbox/emscripten/emsdk-portable/clang/e1.37.21_64bit/llc /var/folders/ld/lg90tl916h90s068qh2029040000gn/T/tmpHiSdtq/project.bc -march=js -filetype=asm -o /var/folders/ld/lg90tl916h90s068qh2029040000gn/T/emscripten_temp/tmp2x4JzW.4.js -emscripten-stack-size=5242880 -O2 -emscripten-precise-f32 -emscripten-assertions=2 -emscripten-global-base=1024 -enable-emscripten-cpp-exceptions -emscripten-wasm -emscripten-only-wasm
DEBUG:root: emscript: llvm backend took 18.5316390991 seconds
DEBUG:root:emscript: js compiler glue
Does this help?
Meanwhile, I did use the -Dpthread_self=em_pthread_self to overcome the problem.
Thanks,
No lines with pthread in the debug output?
If pthreads isn't linked in, perhaps pthreads_self arrives from another part of libc, I'm not sure.
Here's the exhaustive grep 'pthread' from the EMCC_DEBUG=1 and -s VERBOSE=1 output, with references not containing "pthread" removed from individual lines:
DEBUG:root:undefs: set(['pthread_join', 'pthread_setspecific', 'em_pthread_self', 'pthread_detach', 'pthread_getspecific', 'pthread_key_create', 'pthread_create', ])
DEBUG:root:undefs: set(['pthread_setspecific', 'em_pthread_self', 'pthread_create', ])
DEBUG:root:undefs: set(['pthread_mutex_unlock', 'pthread_condattr_destroy', 'pthread_mutex_init', 'pthread_mutex_lock', 'pthread_cond_wait', 'pthread_cond_signal', 'pthread_mutex_destroy', 'pthread_condattr_init', 'pthread_create', 'pthread_cond_timedwait', 'pthread_sigmask', 'pthread_cond_destroy', 'pthread_condattr_setclock', 'pthread_join', 'pthread_cond_init', ])
DEBUG:root:undefs: set(['pthread_join', 'pthread_setspecific', 'em_pthread_self', 'pthread_detach', 'pthread_getspecific', 'pthread_key_create', 'pthread_create', ])
DEBUG:root:undefs: set(['pthread_setspecific', 'em_pthread_self', 'pthread_create', ])
DEBUG:root:undefs: set(['pthread_mutex_unlock', 'pthread_condattr_destroy', 'pthread_mutex_init', 'pthread_mutex_lock', 'pthread_cond_wait', 'pthread_cond_signal', 'pthread_mutex_destroy', 'pthread_condattr_init', 'pthread_create', 'pthread_cond_timedwait', 'pthread_sigmask', 'pthread_cond_destroy', 'pthread_condattr_setclock', 'pthread_join', 'pthread_cond_init', ])
DEBUG:root:undefs: set(['pthread_join', 'pthread_setspecific', 'em_pthread_self', 'pthread_detach', 'pthread_getspecific', 'pthread_key_create', 'pthread_create', ])
DEBUG:root:undefs: set(['pthread_setspecific', 'em_pthread_self', 'pthread_create', ])
DEBUG:root:undefs: set(['pthread_mutex_unlock', 'pthread_condattr_destroy', 'pthread_mutex_init', 'pthread_mutex_lock', 'pthread_cond_wait', 'pthread_cond_signal', 'pthread_mutex_destroy', 'pthread_condattr_init', 'pthread_create', 'pthread_cond_timedwait', 'pthread_sigmask', 'pthread_cond_destroy', 'pthread_condattr_setclock', 'pthread_join', 'pthread_cond_init', ])
DEBUG:root:undefs: set(['pthread_join', 'pthread_setspecific', 'em_pthread_self', 'pthread_detach', 'pthread_getspecific', 'pthread_key_create', 'pthread_create', ])
DEBUG:root:undefs: set(['pthread_setspecific', 'em_pthread_self', 'pthread_create', ])
DEBUG:root:undefs: set(['pthread_mutex_unlock', 'pthread_condattr_destroy', 'pthread_mutex_init', 'pthread_mutex_lock', 'pthread_cond_wait', 'pthread_cond_signal', 'pthread_mutex_destroy', 'pthread_condattr_init', 'pthread_create', 'pthread_cond_timedwait', 'pthread_sigmask', 'pthread_cond_destroy', 'pthread_condattr_setclock', 'pthread_join', 'pthread_cond_init', ])
adding _pthread_mutex_unlock and deps : function _pthread_mutex_unlock(x) {
adding _pthread_mutex_lock and deps : function _pthread_mutex_lock(x) {
adding _pthread_mutexattr_settype and deps : function _pthread_mutexattr_settype() {}
adding _pthread_cond_wait and deps : function _pthread_cond_wait() { return 0
adding _pthread_cond_signal and deps : function _pthread_cond_signal() { return
adding _pthread_mutexattr_init and deps : function _pthread_mutexattr_init() {}
adding _pthread_mutex_init and deps : function _pthread_mutex_init() {}
adding _pthread_mutexattr_destroy and deps : function _pthread_mutexattr_destroy() {}
adding _pthread_cond_broadcast and deps : function _pthread_cond_broadcast(x) {
adding _pthread_mutex_destroy and deps : function _pthread_mutex_destroy() {}
adding _pthread_cond_destroy and deps : function _pthread_cond_destroy() { retur
Hmm, no pthread_self there. Strange.
Anyhow, if you have a workaround already, maybe we can close this. Or, if you have a testcase, I could investigate that.
I'll try providing a test case by the end of the week. Thanks.
Here it is.
main.c
#include <pthread.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
pthread_key_t key;
int ret;
pthread_create(NULL, NULL, NULL, NULL);
pthread_self();
ret = pthread_key_create(&key, NULL);
printf("pthread_key_create() returned %d, key=%d\n", ret, key);
return 0;
}
my_pthread.c
#include <pthread.h>
#include <stdio.h>
pthread_t pthread_self(void)
{
printf("My pthread_self()\n");
return 0;
}
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg)
{
printf("My pthread_create()\n");
return 0;
}
Build 1:
$ emcc main.c -o main.o
$ emcc my_pthread.c -o my_pthread.o
$ emcc main.o my_pthread.o -o main.js
error: Linking globals named 'pthread_self': symbol multiply defined!
Build 2 (renaming pthread_self):
$ emcc -Dpthread_self=my_pthread_self main.c -o main.o
$ emcc -Dpthread_self=my_pthread_self my_pthread.c -o my_pthread.o
$ emcc main.o my_pthread.o -o main.js
$ node main.js
My pthread_create()
My pthread_self()
pthread_key_create() returned 0, key=1
I can reproduce this, thanks. I think this comes down to pthread_self not being defined as a weak symbol, which is necessary for such replacing. malloc is defined that way, for example.
I don't see libc docs mention it should be replaceable that way, so maybe this is not supposed to work? When trying it using gcc natively, it also fails to link.
What is your motivation btw to re-do the pthread library? Is there a bug/missing feature that you are trying to work around by replacing these?
pthread_self could either come from
- https://github.com/kripken/emscripten/blob/incoming/src/library_pthread.js#L814
- or https://github.com/kripken/emscripten/blob/incoming/system/lib/libc/musl/src/misc/emscripten_pthread.c#L7
depending on whether building with -s USE_PTHREADS=1 or not. It's probably the second one given the error message - you can try forcibly removing that definition to do a local hack (and then issue emcc --clear-cache)
The motivation was to port the existing mobile/desktop code heavily using threads without rewriting most of the code (threads that acted as workers, waiting for jobs in a loop had to be slightly refactored), by scheduling their execution in serial, while remaining nearly 100% pthread spec-compliant. It worked out very well.
I'm not building with -s USE_PTHREADS=1 (see test case above).
Altering emscripten_pthread.c by assigning the weak attribute to pthread_self:
@@ -3,8 +3,9 @@
#include "pthread_impl.h"
#if !__EMSCRIPTEN_PTHREADS__
-static struct pthread __main_pthread;
+__attribute__((weak))
pthread_t pthread_self(void) {
+ static struct pthread __main_pthread;
return &__main_pthread;
}
#endif
solves my issue (no need for the -D hack). Are there any issues that would prevent this from going mainstream?
Also, what is the purpose of the void __emscripten_pthread_data_constructor(void) ?
Thanks,
This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 7 days. Feel free to re-open at any time if this issue is still relevant.