diff --git a/libc/bionic/bionic_elf_tls.cpp b/libc/bionic/bionic_elf_tls.cpp index 6de51eb389b5e16728563450e98e34fece255c91..15d383b61f3a9fdb14cfda3d8d4b9f9709b6c4b4 100644 --- a/libc/bionic/bionic_elf_tls.cpp +++ b/libc/bionic/bionic_elf_tls.cpp @@ -323,11 +323,7 @@ __attribute__((noinline)) static void* tls_get_addr_slow_path(const TlsIndex* ti } } -#if (defined(__riscv) && (__riscv_xlen == 64)) return static_cast(mod_ptr) + ti->offset + TLS_DTV_OFFSET; -#else - return static_cast(mod_ptr) + ti->offset; -#endif } // Returns the address of a thread's TLS memory given a module ID and an offset @@ -347,11 +343,7 @@ extern "C" void* TLS_GET_ADDR(const TlsIndex* ti) TLS_GET_ADDR_CCONV { if (__predict_true(generation == dtv->generation)) { void* mod_ptr = dtv->modules[__tls_module_id_to_idx(ti->module_id)]; if (__predict_true(mod_ptr != nullptr)) { -#if (defined(__riscv) && (__riscv_xlen == 64)) return static_cast(mod_ptr) + ti->offset + TLS_DTV_OFFSET; -#else - return static_cast(mod_ptr) + ti->offset; -#endif } } diff --git a/libc/private/bionic_elf_tls.h b/libc/private/bionic_elf_tls.h index 57db3425a364867dfab95f5584e3dbc9f2ee2597..04e353c9c5de8a59bd8bbd0f7108004563d972c9 100644 --- a/libc/private/bionic_elf_tls.h +++ b/libc/private/bionic_elf_tls.h @@ -213,4 +213,6 @@ void __notify_thread_exit_callbacks(); // [2]: Documentation of TLS data structures // https://github.com/riscv-non-isa/riscv-elf-psabi-doc/issues/53 #define TLS_DTV_OFFSET 0x800 +#else +#define TLS_DTV_OFFSET 0 #endif diff --git a/linker/linker.cpp b/linker/linker.cpp index 3488f5cc716eaef3c7ead6cdff7f0390e7ed9e56..6af2a95400755522ed26c37224ad23e5d2732737 100644 --- a/linker/linker.cpp +++ b/linker/linker.cpp @@ -371,7 +371,7 @@ static inline void* get_tls_block_for_this_thread(const soinfo_tls* si_tls, bool char* static_tls = reinterpret_cast(__get_bionic_tcb()) - layout.offset_bionic_tcb(); return static_tls + tls_mod.static_offset; } else if (should_alloc) { - const TlsIndex ti { si_tls->module_id, 0 }; + const TlsIndex ti { si_tls->module_id, static_cast(0 - TLS_DTV_OFFSET) }; return TLS_GET_ADDR(&ti); } else { TlsDtv* dtv = __get_tcb_dtv(__get_bionic_tcb()); diff --git a/linker/linker_relocate.cpp b/linker/linker_relocate.cpp index 72be5f74c8dc932b472b6be0a6e7f285d568d092..0198e9221c8cab88454c399b3ee7514042954dea 100644 --- a/linker/linker_relocate.cpp +++ b/linker/linker_relocate.cpp @@ -413,7 +413,7 @@ static bool process_relocation_impl(Relocator& relocator, const rel_t& reloc) { case R_GENERIC_TLS_DTPREL: count_relocation_if(kRelocRelative); { - const ElfW(Addr) result = sym_addr + get_addend_rel(); + const ElfW(Addr) result = sym_addr + get_addend_rel() - TLS_DTV_OFFSET; trace_reloc("RELO TLS_DTPREL %16p <- %16p %s", rel_target, reinterpret_cast(result), sym_name); *static_cast(rel_target) = result; diff --git a/linker/linker_relocs.h b/linker/linker_relocs.h index 375a0764e35af7ea2f0811d084f9d624ccffc231..e53499ce4ca6d320eca9f4c5d771c16b3afd8abb 100644 --- a/linker/linker_relocs.h +++ b/linker/linker_relocs.h @@ -97,6 +97,7 @@ #define R_GENERIC_TLS_DTPMOD R_RISCV_TLS_DTPMOD64 #define R_GENERIC_TLS_DTPREL R_RISCV_TLS_DTPREL64 #define R_GENERIC_TLS_TPREL R_RISCV_TLS_TPREL64 +// TBD: riscv has not supported TLSDESC actually. #define R_GENERIC_TLSDESC R_RISCV_TLS_GOT_HI20 #endif