git: 78b99f369f75 - main - libc: Fix dl_iterate_phdr's dlpi_tls_data for PowerPC and RISC-V

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

URL: https://cgit.FreeBSD.org/src/commit/?id=78b99f369f75f5df49b506ae750659b07ab34362

commit 78b99f369f75f5df49b506ae750659b07ab34362
Author:     Jessica Clarke <jrtc27@FreeBSD.org>
AuthorDate: 2025-05-06 22:14:50 +0000
Commit:     Jessica Clarke <jrtc27@FreeBSD.org>
CommitDate: 2025-05-06 22:14:50 +0000

    libc: Fix dl_iterate_phdr's dlpi_tls_data for PowerPC and RISC-V
    
    The implementation of dl_iterate_phdr for statically-linked binaries
    abuses __tls_get_addr to get to the start of the TLS block. For most
    architectures, tls_index's ti_offset (relocated by DTPOFF/DTPREL for GOT
    entries) is just the offset within that module's TLS block. However, for
    PowerPC and RISC-V, which have a non-zero TLS_DTV_OFFSET and thus are
    designed assuming DTV entries are biased by that value, ti_offset
    normally has TLS_DTV_OFFSET pre-subtracted. By using an offset of zero
    here we end up getting a pointer TLS_DTV_OFFSET past what __tls_get_addr
    would return for the first TLS variable.
    
    Fix this by using -TLS_DTV_OFFSET to mirror what the General Dynamic GOT
    entry for the first TLS variable would be.
    
    (Note this also applies to MIPS on stable/13)
    
    Reviewed by:    kib
    Fixes:          dbd2053026a6 ("libc dl_iterate_phdr(): dlpi_tls_data is wrong")
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D50182
---
 lib/libc/gen/dlfcn.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/libc/gen/dlfcn.c b/lib/libc/gen/dlfcn.c
index bffee3952e0d..ae1c8d83df19 100644
--- a/lib/libc/gen/dlfcn.c
+++ b/lib/libc/gen/dlfcn.c
@@ -229,7 +229,7 @@ _dl_iterate_phdr_locked(
 		return (1);
 	_once(&dl_phdr_info_once, dl_init_phdr_info);
 	ti.ti_module = 1;
-	ti.ti_offset = 0;
+	ti.ti_offset = -TLS_DTV_OFFSET;
 	phdr_info.dlpi_tls_data = __tls_get_addr(&ti);
 	ret = callback(&phdr_info, sizeof(phdr_info), data);
 	return (ret);