This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] i386: Add _startup_sbrk and _startup_fatal [BZ #21913]
On Mon, Aug 7, 2017 at 6:26 AM, Zack Weinberg <zackw@panix.com> wrote:
> On Mon, Aug 7, 2017 at 9:21 AM, H.J. Lu <hjl.tools@gmail.com> wrote:
>> On Mon, Aug 7, 2017 at 6:11 AM, Zack Weinberg <zackw@panix.com> wrote:
>>> On Mon, Aug 7, 2017 at 8:38 AM, H.J. Lu <hjl.tools@gmail.com> wrote:
>>>> On Mon, Aug 7, 2017 at 4:47 AM, Zack Weinberg <zackw@panix.com> wrote:
>>>>
>>>> To initialize TLS, we need a single syscall, sbrk.
>>>
>>> I presume this is to allocate memory for the TLS block. But why can't
>>> the TLS block - remember, this is for the main thread, in a statically
>>> linked binary - be statically allocated data?
>>
>> I can look into it.
The code is
#if TLS_TCB_AT_TP
/* Align the TCB offset to the maximum alignment, as
_dl_allocate_tls_storage (in elf/dl-tls.c) does using __libc_memalign
and dl_tls_static_align. */
tcb_offset = roundup (memsz + GL(dl_tls_static_size), max_align);
tlsblock = __sbrk (tcb_offset + TLS_INIT_TCB_SIZE + max_align);
#elif TLS_DTV_AT_TP
tcb_offset = roundup (TLS_INIT_TCB_SIZE, align ?: 1);
tlsblock = __sbrk (tcb_offset + memsz + max_align
+ TLS_PRE_TCB_SIZE + GL(dl_tls_static_size));
tlsblock += TLS_PRE_TCB_SIZE;
Are you suggesting that we use a static data with a fixed size and
check the size at run-time?
>> But tunables_strdup also calls sbrk before TLS is initialized. We
>> still need it even if TLS initialization doesn't.
>
> Again, does this _need_ to happen that early, or could it be moved later?
>
We have
__tunables_init (__environ);
ARCH_INIT_CPU_FEATURES ();
/* Perform IREL{,A} relocations. */
ARCH_SETUP_IREL ();
/* The stack guard goes into the TCB, so initialize it early. */
__libc_setup_tls ();
__libc_setup_tls should be called after ARCH_INIT_CPU_FEATURES and
ARCH_SETUP_IREL, which must be called after __tunables_init since
__tunables_init can change CPU features.
Given this, we need a working sbrk before TLS is initialized.
--
H.J.