Index: libexec/rtld-elf/map_object.c =================================================================== --- libexec/rtld-elf/map_object.c +++ libexec/rtld-elf/map_object.c @@ -312,6 +312,7 @@ obj->tlsindex = ++tls_max_index; obj->tlssize = phtls->p_memsz; obj->tlsalign = phtls->p_align; + obj->tlspoffset = phtls->p_offset; obj->tlsinitsize = phtls->p_filesz; obj->tlsinit = mapbase + phtls->p_vaddr; } Index: libexec/rtld-elf/rtld.h =================================================================== --- libexec/rtld-elf/rtld.h +++ libexec/rtld-elf/rtld.h @@ -167,6 +167,7 @@ size_t tlssize; /* Size of TLS block for this module */ size_t tlsoffset; /* Offset of static TLS block for this module */ size_t tlsalign; /* Alignment of static TLS block */ + size_t tlspoffset; /* p_offset of the static TLS block */ caddr_t relro_page; size_t relro_size; @@ -364,7 +365,7 @@ void *xcalloc(size_t, size_t); void *xmalloc(size_t); char *xstrdup(const char *); -void *malloc_aligned(size_t size, size_t align); +void *malloc_aligned(size_t size, size_t align, size_t offset); void free_aligned(void *ptr); extern Elf_Addr _GLOBAL_OFFSET_TABLE_[]; extern Elf_Sym sym_zero; /* For resolving undefined weak refs. */ Index: libexec/rtld-elf/rtld.c =================================================================== --- libexec/rtld-elf/rtld.c +++ libexec/rtld-elf/rtld.c @@ -4819,7 +4819,7 @@ tls_block_size += pre_size + tls_static_space - TLS_TCB_SIZE - post_size; /* Allocate whole TLS block */ - tls_block = malloc_aligned(tls_block_size, maxalign); + tls_block = malloc_aligned(tls_block_size, maxalign, 0); tcb = (Elf_Addr **)(tls_block + pre_size + extra_size); if (oldtcb != NULL) { @@ -4909,7 +4909,7 @@ size = round(tls_static_space, ralign) + round(tcbsize, ralign); assert(tcbsize >= 2*sizeof(Elf_Addr)); - tls = malloc_aligned(size, ralign); + tls = malloc_aligned(size, ralign, 0 /* XXX */); dtv = xcalloc(tls_max_index + 2, sizeof(Elf_Addr)); segbase = (Elf_Addr)(tls + round(tls_static_space, ralign)); @@ -5002,25 +5002,24 @@ void * allocate_module_tls(int index) { - Obj_Entry* obj; - char* p; - - TAILQ_FOREACH(obj, &obj_list, next) { - if (obj->marker) - continue; - if (obj->tlsindex == index) - break; - } - if (!obj) { - _rtld_error("Can't find module with TLS index %d", index); - rtld_die(); - } + Obj_Entry *obj; + char *p; - p = malloc_aligned(obj->tlssize, obj->tlsalign); - memcpy(p, obj->tlsinit, obj->tlsinitsize); - memset(p + obj->tlsinitsize, 0, obj->tlssize - obj->tlsinitsize); + TAILQ_FOREACH(obj, &obj_list, next) { + if (obj->marker) + continue; + if (obj->tlsindex == index) + break; + } + if (obj == NULL) { + _rtld_error("Can't find module with TLS index %d", index); + rtld_die(); + } - return p; + p = malloc_aligned(obj->tlssize, obj->tlsalign, obj->tlspoffset); + memcpy(p, obj->tlsinit, obj->tlsinitsize); + memset(p + obj->tlsinitsize, 0, obj->tlssize - obj->tlsinitsize); + return (p); } bool Index: libexec/rtld-elf/xmalloc.c =================================================================== --- libexec/rtld-elf/xmalloc.c +++ libexec/rtld-elf/xmalloc.c @@ -27,6 +27,7 @@ * $FreeBSD$ */ +#include #include #include #include @@ -76,16 +77,22 @@ } void * -malloc_aligned(size_t size, size_t align) +malloc_aligned(size_t size, size_t align, size_t offset) { - void *mem, *res; + char *mem, *res; + uintptr_t x; + offset &= align - 1; if (align < sizeof(void *)) align = sizeof(void *); - mem = xmalloc(size + sizeof(void *) + align - 1); - res = (void *)round((uintptr_t)mem + sizeof(void *), align); - *(void **)((uintptr_t)res - sizeof(void *)) = mem; + mem = xmalloc(roundup(size, align) + roundup(sizeof(void *), + align) + offset); + x = roundup((uintptr_t)mem + sizeof(void *), align); + x += offset; + res = (void *)x; + x -= sizeof(void *); + memcpy((void *)x, &mem, sizeof(mem)); return (res); } @@ -99,6 +106,6 @@ return; x = (uintptr_t)ptr; x -= sizeof(void *); - mem = *(void **)x; + memcpy(&mem, (void *)x, sizeof(mem)); free(mem); }