Pull up following revision(s) (requested by nakayama in ticket #469):

lib/libc/tls/tls.c: revision 1.10
	lib/libc/tls/tls.c: revision 1.11
	lib/libc/tls/tls.c: revision 1.12
	lib/libc/tls/tls.c: revision 1.13
	libexec/ld.elf_so/tls.c: revision 1.13
	libexec/ld.elf_so/tls.c: revision 1.14
	libexec/ld.elf_so/Makefile: revision 1.142
	lib/libc/tls/Makefile.inc: revision 1.3
	usr.bin/ldd/Makefile.elf: revision 1.6

PR 54093: Align static TLS area to max_align_t.
Use alignof and not size_t for platforms with non-natural base
alignments.

Mirror the ld.elf_so logic for handling aligning the TLS size.
Most noticable, recompute the start of the TLS area for variant I
relative to the TCB. This makes a difference when the segment size and
base alignment don't agree.

Fix PR/54074 and PR/54093 completely.
More similar to the ld.elf_so logic, it is necessary to align with
p_align first.  Also, invert the #ifdef condition for consistency.

Should fix regression for static linking binaries:
http://releng.netbsd.org/b5reports/sparc/commits-2019.11.html#2019.11.10.23.39.03
http://releng.netbsd.org/b5reports/sparc64/commits-2019.11.html#2019.11.16.04.10.33
This commit is contained in:
martin 2019-11-26 08:12:26 +00:00
parent 1deaf2b152
commit 37ebfa176c
5 changed files with 31 additions and 18 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile.inc,v 1.2 2011/03/12 07:55:09 matt Exp $
# $NetBSD: Makefile.inc,v 1.2.46.1 2019/11/26 08:12:26 martin Exp $
.include <bsd.own.mk>
@ -6,4 +6,4 @@
.PATH: ${.PARSEDIR} ${ARCHDIR}/tls
SRCS+= tls.c
CPPFLAGS.tls.c+= -D_LIBC_SOURCE
CPPFLAGS.tls.c+= -D_LIBC_SOURCE -std=gnu11

View File

@ -1,4 +1,4 @@
/* $NetBSD: tls.c,v 1.9 2018/07/13 19:50:21 joerg Exp $ */
/* $NetBSD: tls.c,v 1.9.2.1 2019/11/26 08:12:26 martin Exp $ */
/*-
* Copyright (c) 2011 The NetBSD Foundation, Inc.
@ -30,7 +30,7 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: tls.c,v 1.9 2018/07/13 19:50:21 joerg Exp $");
__RCSID("$NetBSD: tls.c,v 1.9.2.1 2019/11/26 08:12:26 martin Exp $");
#include "namespace.h"
@ -46,6 +46,7 @@ __RCSID("$NetBSD: tls.c,v 1.9 2018/07/13 19:50:21 joerg Exp $");
#include <link_elf.h>
#include <lwp.h>
#include <stdbool.h>
#include <stdalign.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
@ -84,15 +85,17 @@ _rtld_tls_allocate(void)
uint8_t *p;
if (initial_thread_tcb == NULL) {
#ifdef __HAVE_TLS_VARIANT_II
tls_size = roundup2(tls_size, sizeof(void *));
#ifdef __HAVE_TLS_VARIANT_I
tls_allocation = tls_size;
#else
tls_allocation = roundup2(tls_size, alignof(max_align_t));
#endif
tls_allocation = tls_size + sizeof(*tcb);
initial_thread_tcb = p = mmap(NULL, tls_allocation,
PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
initial_thread_tcb = p = mmap(NULL,
tls_allocation + sizeof(*tcb), PROT_READ | PROT_WRITE,
MAP_ANON, -1, 0);
} else {
p = calloc(1, tls_allocation);
p = calloc(1, tls_allocation + sizeof(*tcb));
}
if (p == NULL) {
static const char msg[] = "TLS allocation failed, terminating\n";
@ -105,7 +108,8 @@ _rtld_tls_allocate(void)
p += sizeof(struct tls_tcb);
#else
/* LINTED tls_size is rounded above */
tcb = (struct tls_tcb *)(p + tls_size);
tcb = (struct tls_tcb *)(p + tls_allocation);
p = (uint8_t *)tcb - tls_size;
tcb->tcb_self = tcb;
#endif
memcpy(p, tls_initaddr, tls_initsize);
@ -125,10 +129,10 @@ _rtld_tls_free(struct tls_tcb *tcb)
p = (uint8_t *)tcb;
#else
/* LINTED */
p = (uint8_t *)tcb - tls_size;
p = (uint8_t *)tcb - tls_allocation;
#endif
if (p == initial_thread_tcb)
munmap(p, tls_allocation);
munmap(p, tls_allocation + sizeof(*tcb));
else
free(p);
}
@ -148,7 +152,11 @@ __libc_static_tls_setup_cb(struct dl_phdr_info *data, size_t len, void *cookie)
continue;
tls_initaddr = (void *)(phdr->p_vaddr + data->dlpi_addr);
tls_initsize = phdr->p_filesz;
#ifdef __HAVE_TLS_VARIANT_I
tls_size = phdr->p_memsz;
#else
tls_size = roundup2(phdr->p_memsz, phdr->p_align);
#endif
}
return 0;
}

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.141 2019/04/03 21:37:58 christos Exp $
# $NetBSD: Makefile,v 1.141.2.1 2019/11/26 08:12:26 martin Exp $
#
# NOTE: when changing ld.so, ensure that ldd still compiles.
#
@ -92,6 +92,7 @@ CLEANFILES+= errlist_concat.h ${PROG}.map
BINDIR= ${SHLINKINSTALLDIR}
CPPFLAGS.tls.c+= -std=gnu11
CPPFLAGS+= -DLIBDIR=\"${LIBDIR}\" -D_PATH_RTLD=\"${BINDIR}/${PROG}\"
CPPFLAGS+= -I${.CURDIR} -I. -D_KERNTYPES
CPPFLAGS+= -DRTLD_LOADER

View File

@ -1,4 +1,4 @@
/* $NetBSD: tls.c,v 1.12 2019/04/13 00:23:32 rin Exp $ */
/* $NetBSD: tls.c,v 1.12.2.1 2019/11/26 08:12:26 martin Exp $ */
/*-
* Copyright (c) 2011 The NetBSD Foundation, Inc.
* All rights reserved.
@ -29,11 +29,13 @@
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: tls.c,v 1.12 2019/04/13 00:23:32 rin Exp $");
__RCSID("$NetBSD: tls.c,v 1.12.2.1 2019/11/26 08:12:26 martin Exp $");
#include <sys/param.h>
#include <sys/ucontext.h>
#include <lwp.h>
#include <stdalign.h>
#include <stddef.h>
#include <string.h>
#include "debug.h"
#include "rtld.h"
@ -99,7 +101,7 @@ _rtld_tls_initial_allocation(void)
#ifndef __HAVE_TLS_VARIANT_I
_rtld_tls_static_space = roundup2(_rtld_tls_static_space,
sizeof(void *));
alignof(max_align_t));
#endif
dbg(("_rtld_tls_static_space %zu", _rtld_tls_static_space));

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile.elf,v 1.5 2013/05/07 13:00:35 christos Exp $
# $NetBSD: Makefile.elf,v 1.5.30.1 2019/11/26 08:12:27 martin Exp $
# Makefile fragment to build a (32 or 64 bit) libldd_elfxx.a.
# Expects CPPFLAGS to have ELFSIZE set, and LIB to be set.
@ -7,4 +7,6 @@ SRCS= ldd_elfxx.c
SRCS+= xmalloc.c debug.c expand.c map_object.c load.c search.c \
headers.c paths.c tls.c symver.c
CPPFLAGS.tls.c+= -std=gnu11
.include "Makefile.common"