diff options
author | Kamil Rytarowski <n54@gmx.com> | 2017-12-09 02:23:33 +0000 |
---|---|---|
committer | Kamil Rytarowski <n54@gmx.com> | 2017-12-09 02:23:33 +0000 |
commit | 917606ab37d00d8877ceda6b23bde60a79f09bc4 (patch) | |
tree | f6c7fd6b29777197f58dd7d4e3453e0ba019a6f0 | |
parent | 32e20e4417a0dd8870d8ec5c9be21f5c2d1ebc63 (diff) |
Correct handling of the TLS/NetBSD block of the main program
Summary:
Include <sys/tls.h> for:
- struct tls_tcb - thread control block structure
- __HAVE___LWP_GETTCB_FAST - __lwp_gettcb_fast() is available
- __HAVE___LWP_GETPRIVATE_FAST - __lwp_getprivate_fast() is available
- __HAVE_TLS_VARIANT_I - TLS Variant I for this architecture
- __HAVE_TLS_VARIANT_II - TLS Variant II for this architecture
Rename ThreadSelfSegbase() to ThreadSelfTlsTcb and switch it
to retrieve in a portable way TCB.
Switch ThreadSelf() to retrieve pthread from struct tcb_tls.
Use dl_iterate_phdr() to find out the size of TLS block of
the main program.
Correct the index of the TLS block of the main program
(dlpi_tls_modid); it's 1, not 2.
New NetBSD code is now CPU (NetBSD port) agnostic.
Stop sharing the same code with FreeBSD.
Sponsored by <The NetBSD Foundation>
Reviewers: dvyukov, joerg, vitalybuka
Reviewed By: vitalybuka
Subscribers: emaste, kubamracek, llvm-commits, #sanitizers
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D40159
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@320234 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/sanitizer_common/sanitizer_linux_libcdep.cc | 50 |
1 files changed, 47 insertions, 3 deletions
diff --git a/lib/sanitizer_common/sanitizer_linux_libcdep.cc b/lib/sanitizer_common/sanitizer_linux_libcdep.cc index f89c93a9d..c68f2146d 100644 --- a/lib/sanitizer_common/sanitizer_linux_libcdep.cc +++ b/lib/sanitizer_common/sanitizer_linux_libcdep.cc @@ -43,6 +43,7 @@ #if SANITIZER_NETBSD #include <sys/sysctl.h> +#include <sys/tls.h> #endif #if SANITIZER_LINUX @@ -327,7 +328,7 @@ uptr ThreadSelf() { } #endif // (x86_64 || i386 || MIPS) && SANITIZER_LINUX -#if SANITIZER_FREEBSD || SANITIZER_NETBSD +#if SANITIZER_FREEBSD static void **ThreadSelfSegbase() { void **segbase = 0; # if defined(__i386__) @@ -345,7 +346,36 @@ static void **ThreadSelfSegbase() { uptr ThreadSelf() { return (uptr)ThreadSelfSegbase()[2]; } -#endif // SANITIZER_FREEBSD || SANITIZER_NETBSD +#endif // SANITIZER_FREEBSD + +#if SANITIZER_NETBSD +static struct tls_tcb * ThreadSelfTlsTcb() { + struct tls_tcb * tcb; +# ifdef __HAVE___LWP_GETTCB_FAST + tcb = (struct tls_tcb *)__lwp_gettcb_fast(); +# elif defined(__HAVE___LWP_GETPRIVATE_FAST) + tcb = (struct tls_tcb *)__lwp_getprivate_fast(); +# endif + return tcb; +} + +uptr ThreadSelf() { + return (uptr)ThreadSelfTlsTcb()->tcb_pthread; +} + +int GetSizeFromHdr(struct dl_phdr_info *info, size_t size, void *data) { + const Elf_Phdr *hdr = info->dlpi_phdr; + const Elf_Phdr *last_hdr = hdr + info->dlpi_phnum; + + for (; hdr != last_hdr; ++hdr) { + if (hdr->p_type == PT_TLS && info->dlpi_tls_modid == 1) { + *(uptr*)data = hdr->p_memsz; + break; + } + } + return 0; +} +#endif // SANITIZER_NETBSD #if !SANITIZER_GO static void GetTls(uptr *addr, uptr *size) { @@ -363,7 +393,7 @@ static void GetTls(uptr *addr, uptr *size) { *addr = 0; *size = 0; # endif -#elif SANITIZER_FREEBSD || SANITIZER_NETBSD +#elif SANITIZER_FREEBSD void** segbase = ThreadSelfSegbase(); *addr = 0; *size = 0; @@ -376,6 +406,20 @@ static void GetTls(uptr *addr, uptr *size) { *addr = (uptr) dtv[2]; *size = (*addr == 0) ? 0 : ((uptr) segbase[0] - (uptr) dtv[2]); } +#elif SANITIZER_NETBSD + struct tls_tcb * const tcb = ThreadSelfTlsTcb(); + *addr = 0; + *size = 0; + if (tcb != 0) { + // Find size (p_memsz) of dlpi_tls_modid 1 (TLS block of the main program). + // ld.elf_so hardcodes the index 1. + dl_iterate_phdr(GetSizeFromHdr, size); + + if (*size != 0) { + // The block has been found and tcb_dtv[1] contains the base address + *addr = (uptr)tcb->tcb_dtv[1]; + } + } #elif SANITIZER_ANDROID *addr = 0; *size = 0; |