1*f14fb602SLionel SambucSteps for adding TLS support for a new platform: 2*f14fb602SLionel Sambuc 3*f14fb602SLionel Sambuc(1) Declare TLS variant in machine/types.h by defining either 4*f14fb602SLionel Sambuc__HAVE_TLS_VARIANT_I or __HAVE_TLS_VARIANT_II. 5*f14fb602SLionel Sambuc 6*f14fb602SLionel Sambuc(2) _lwp_makecontext has to set the reserved register or kernel transfer 7*f14fb602SLionel Sambucvariable in uc_mcontext to the provided value of 'private'. See 8*f14fb602SLionel Sambucsrc/lib/libc/arch/$PLATFORM/gen/_lwp.c. 9*f14fb602SLionel Sambuc 10*f14fb602SLionel SambucThis is not possible on the VAX as there is no free space in ucontext_t. 11*f14fb602SLionel SambucThis requires either a special version of _lwp_create or versioning 12*f14fb602SLionel Sambuceverything using ucontext_t. Debug support depends on getting the data from 13*f14fb602SLionel Sambucucontext_t, so the second option is possibly required. 14*f14fb602SLionel Sambuc 15*f14fb602SLionel Sambuc(3) _lwp_setprivate(2) has to update the same register as 16*f14fb602SLionel Sambuc_lwp_makecontext uses for the private area pointer. Normally 17*f14fb602SLionel Sambuccpu_lwp_setprivate is provided by MD to reflect the kernel view and 18*f14fb602SLionel Sambucenabled by defining __HAVE_CPU_LWP_SETPRIVATE in machine/types.h. 19*f14fb602SLionel Sambuccpu_setmcontext is responsible for keeping the MI l_private field 20*f14fb602SLionel Sambucsynchronised by calling lwp_setprivate as needed. 21*f14fb602SLionel Sambuc 22*f14fb602SLionel Sambuccpu_switchto has to update the mapping. 23*f14fb602SLionel Sambuc 24*f14fb602SLionel Sambuc_lwp_setprivate is used for the initial thread, all other threads 25*f14fb602SLionel Sambuccreated by libpthread use _lwp_makecontext for this purpose. 26*f14fb602SLionel Sambuc 27*f14fb602SLionel Sambuc(4) Provide __tls_get_addr and possible other MD functions for dynamic 28*f14fb602SLionel SambucTLS offset computation. If such alternative entry points exist (currently 29*f14fb602SLionel Sambuconly i386), also add a weak reference to 0 in src/lib/libc/tls/tls.c. 30*f14fb602SLionel Sambuc 31*f14fb602SLionel SambucThe generic implementation can be found in tls.c and is used with 32*f14fb602SLionel Sambuc__HAVE_COMMON___TLS_GET_ADDR. It depends on ___lwp_getprivate_fast 33*f14fb602SLionel Sambuc(see below). 34*f14fb602SLionel Sambuc 35*f14fb602SLionel Sambuc(5) Implement the necessary relocation records in mdreloc.c. There are 36*f14fb602SLionel Sambuctypically three relocation types found in dynamic binaries: 37*f14fb602SLionel Sambuc 38*f14fb602SLionel Sambuc(a) R_TYPE(TLS_DTPOFF): Offset inside the module. The common TLS code 39*f14fb602SLionel Sambucensures that the DTV vector points to offset 0 inside the module TLS block. 40*f14fb602SLionel SambucThis is normally def->st_value + rela->r_addend. 41*f14fb602SLionel Sambuc 42*f14fb602SLionel Sambuc(b) R_TYPE(TLS_DTPMOD): Module index. 43*f14fb602SLionel Sambuc 44*f14fb602SLionel Sambuc(c) R_TYPE(TLS_TPOFF): Static TLS offset. The code has to check whether 45*f14fb602SLionel Sambucthe static TLS offset for this module has been allocated 46*f14fb602SLionel Sambuc(defobj->tls_done) and otherwise call _rtld_tls_offset_allocate(). This 47*f14fb602SLionel Sambucmay fail if no static space is available and the object has been pulled 48*f14fb602SLionel Sambucin via dlopen(3). 49*f14fb602SLionel Sambuc 50*f14fb602SLionel SambucFor TLS Variant I, this is typically: 51*f14fb602SLionel Sambuc 52*f14fb602SLionel Sambucdef->st_value + rela->r_addend + defobj->tlsoffset + sizeof(struct tls_tcb) 53*f14fb602SLionel Sambuc 54*f14fb602SLionel Sambuce.g. the relocation doesn't include the fixed TCB. 55*f14fb602SLionel Sambuc 56*f14fb602SLionel SambucFor TLS Variant II, this is typically: 57*f14fb602SLionel Sambuc 58*f14fb602SLionel Sambucdef->st_value - defobj->tlsoffset + rela->r_addend 59*f14fb602SLionel Sambuc 60*f14fb602SLionel Sambuce.g. starting offset is counting down from the TCB. 61*f14fb602SLionel Sambuc 62*f14fb602SLionel Sambuc(6) Implement _lwp_getprivate_fast() in machine/mcontext.h and set 63*f14fb602SLionel Sambuc__HAVE___LWP_GETPRIVATE_FAST in machine/types.h. 64*f14fb602SLionel Sambuc 65*f14fb602SLionel Sambuc(7) Test using src/tests/lib/libc/tls. Make sure with "objdump -R" that 66*f14fb602SLionel Sambuct_tls_dynamic has two TPOFF relocations and h_tls_dlopen.so.1 and 67*f14fb602SLionel Sambuclibh_tls_dynamic.so.1 have both two DTPMOD and DTPOFF relocations. 68