git: 8ad9cec3a2cc - main - rtld-elf: Fix dlsym(3) for TLS symbols on PowerPC and RISC-V

From: Jessica Clarke <jrtc27_at_FreeBSD.org>
Date: Tue, 06 May 2025 22:15:31 UTC
The branch main has been updated by jrtc27:

URL: https://cgit.FreeBSD.org/src/commit/?id=8ad9cec3a2cc643020a286ee68f70eb01225fbdd

commit 8ad9cec3a2cc643020a286ee68f70eb01225fbdd
Author:     Jessica Clarke <jrtc27@FreeBSD.org>
AuthorDate: 2025-05-06 22:14:51 +0000
Commit:     Jessica Clarke <jrtc27@FreeBSD.org>
CommitDate: 2025-05-06 22:14:51 +0000

    rtld-elf: Fix dlsym(3) for TLS symbols on PowerPC and RISC-V
    
    The implementation here is meant to mirror what a GOT entry for the
    given symbol would use for ti_offset. However, on PowerPC and RISC-V,
    TLS_DTV_OFFSET is non-zero, and so the GOT entries are normally biased
    by this, but we fail to do so here. As a result we end up getting a
    pointer TLS_DTV_OFFSET past where the variable actually is.
    
    (Note this also applies to MIPS on stable/13)
    
    Reviewed by:    kib
    Fixes:          5ceeeba90c6c ("Import DragonFly BSD commit")
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D50183
---
 libexec/rtld-elf/rtld.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libexec/rtld-elf/rtld.c b/libexec/rtld-elf/rtld.c
index 12aee444fccf..29d32e54e690 100644
--- a/libexec/rtld-elf/rtld.c
+++ b/libexec/rtld-elf/rtld.c
@@ -4135,7 +4135,7 @@ do_dlsym(void *handle, const char *name, void *retaddr, const Ver_Entry *ve,
 			sym = rtld_resolve_ifunc(defobj, def);
 		else if (ELF_ST_TYPE(def->st_info) == STT_TLS) {
 			ti.ti_module = defobj->tlsindex;
-			ti.ti_offset = def->st_value;
+			ti.ti_offset = def->st_value - TLS_DTV_OFFSET;
 			sym = __tls_get_addr(&ti);
 		} else
 			sym = defobj->relocbase + def->st_value;