From 5bf7eba213cacc4c1220627c91c28deff2ffecda Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Tue, 20 Dec 2016 14:19:32 -0500 Subject: [PATCH] fix support for initialized TLS in static PIE binaries the static-linked version of __init_tls needs to locate the TLS initialization image via the ELF program headers, which requires determining the base address at which the program was loaded. the existing code attempted to do this by comparing the actual address of the program headers (obtained via auxv) with the virtual address for the PT_PHDR record in the program headers. however, the linker seems to produce a PT_PHDR record only when a program interpreter (dynamic linker) is used. thus the computation failed and used the default base address of 0, leading to a crash when trying to access the TLS image at the wrong address. the dynamic linker entry point and static-PIE rcrt1.o startup code compute the base address instead by taking the difference between the run-time address of _DYNAMIC and the virtual address in the PT_DYNAMIC record. this patch copies the approach they use, but with a weak symbolic reference to _DYNAMIC instead of obtaining the address from the crt_arch.h asm. this works because relocations have already been performed at the time __init_tls is called. --- src/env/__init_tls.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/env/__init_tls.c b/src/env/__init_tls.c index 0107a545..b125eb1f 100644 --- a/src/env/__init_tls.c +++ b/src/env/__init_tls.c @@ -71,6 +71,9 @@ typedef Elf32_Phdr Phdr; typedef Elf64_Phdr Phdr; #endif +__attribute__((__weak__, __visibility__("hidden"))) +extern const size_t _DYNAMIC[]; + static void static_init_tls(size_t *aux) { unsigned char *p; @@ -83,6 +86,8 @@ static void static_init_tls(size_t *aux) phdr = (void *)p; if (phdr->p_type == PT_PHDR) base = aux[AT_PHDR] - phdr->p_vaddr; + if (phdr->p_type == PT_DYNAMIC && _DYNAMIC) + base = (size_t)_DYNAMIC - phdr->p_vaddr; if (phdr->p_type == PT_TLS) tls_phdr = phdr; } -- GitLab