Index: libexec/rtld-elf/amd64/reloc.c =================================================================== --- libexec/rtld-elf/amd64/reloc.c +++ libexec/rtld-elf/amd64/reloc.c @@ -547,30 +547,23 @@ return (tls_get_addr_common(dtvp, ti->ti_module, ti->ti_offset)); } -size_t -calculate_first_tls_offset(size_t size, size_t align, size_t offset) -{ - size_t res; - - res = roundup(size, align); - offset &= align - 1; - if (offset != 0) - res += align - offset; - return (res); -} - size_t calculate_tls_offset(size_t prev_offset, size_t prev_size __unused, size_t size, size_t align, size_t offset) { - size_t res; - - res = roundup(prev_offset + size, align); - offset &= align - 1; - if (offset != 0) - res += align - offset; + // res is the smallest integer satisfying res-prev_offset >= size and (-res)%p_align = + // p_vaddr%p_align (= p_offset%p_align). + size_t res = prev_offset + size + align - 1; + res -= (res + offset) & (align - 1); return (res); } + +size_t +calculate_first_tls_offset(size_t size, size_t align, size_t offset) +{ + return calculate_tls_offset(0, 0, size, align, offset); +} + size_t calculate_tls_end(size_t off, size_t size __unused) { Index: libexec/rtld-elf/i386/reloc.c =================================================================== --- libexec/rtld-elf/i386/reloc.c +++ libexec/rtld-elf/i386/reloc.c @@ -538,30 +538,23 @@ return (tls_get_addr_common(dtvp, ti->ti_module, ti->ti_offset)); } -size_t -calculate_first_tls_offset(size_t size, size_t align, size_t offset) -{ - size_t res; - - res = roundup(size, align); - offset &= align - 1; - if (offset != 0) - res += align - offset; - return (res); -} - size_t calculate_tls_offset(size_t prev_offset, size_t prev_size __unused, size_t size, size_t align, size_t offset) { - size_t res; - - res = roundup(prev_offset + size, align); - offset &= align - 1; - if (offset != 0) - res += align - offset; + // res is the smallest integer satisfying res-prev_offset >= size and (-res)%p_align = + // p_vaddr%p_align (= p_offset%p_align). + size_t res = prev_offset + size + align - 1; + res -= (res + offset) & (align - 1); return (res); } + +size_t +calculate_first_tls_offset(size_t size, size_t align, size_t offset) +{ + return calculate_tls_offset(0, 0, size, align, offset); +} + size_t calculate_tls_end(size_t off, size_t size __unused) {