%P1 is and integer that is passed as argument for asm construction.
You can get this value by creating small test program - include "pthread_internals.h", and print out offsetof(struct _pthread, tsd[0]) value. It may be different for 32-bit and 64-bit code. But most likely nobody anymore cares about 32-bit code on OSX.
In result code should look like:
| void *tmp;
asm("mov %%gs::0xAA, %0" : "=r"(tmp))
return (u32)tmp;
|
Where 0xAA is that value in hex that you get from offsetof.
This way there won't be need for any platform specific includes (pthread).
Although if you take a look at different source of pthread then this value seems to be 0. Not sure what is correct value. Stepping into pthread_self assembly in debugger would be best option to figure out.
Here is pthread_self -
https://opensource.apple.com/sour...ibpthread-105.40.1/src/internal.h
| // Internal references to pthread_self() use TSD slot 0 directly.
inline static pthread_t __attribute__((__pure__))
_pthread_self_direct(void)
{
return _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_SELF);
}
#define pthread_self() _pthread_self_direct()
|
Here is _pthread_getspecific_direct -
https://opensource.apple.com/sour...ad-105.40.1/private/tsd_private.h
| __header_always_inline void *
_pthread_getspecific_direct(unsigned long slot)
{
return _os_tsd_get_direct(slot);
}
|
Here is _PTHREAD_TSD_SLOT_PTHREAD_SELF -
https://opensource.apple.com/sour...ad-105.40.1/private/tsd_private.h
| #define _PTHREAD_TSD_SLOT_PTHREAD_SELF __TSD_THREAD_SELF
|
Here is __TSD_THREAD_SELF and _os_tsd_get_direct -
https://opensource.apple.com/sour...xnu-2422.1.72/libsyscall/os/tsd.h
| _os_tsd_get_direct(unsigned long slot)
{
void *ret;
#if defined(__i386__) || defined(__x86_64__)
__asm__("mov %%gs:%1, %0" : "=r" (ret) : "m" (*(void **)(slot * sizeof(void *))));
#endif
return ret;
}
|
So if I understand it correctly this should work:
| void *tmp;
asm("mov %%gs::0x0, %0" : "=r"(tmp))
return (u32)tmp;
|