Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20241221062248.GV10433@brightrain.aerifal.cx>
Date: Sat, 21 Dec 2024 01:22:48 -0500
From: Rich Felker <dalias@...c.org>
To: Markus Wichmann <nullplan@....net>
Cc: musl@...ts.openwall.com, " ." <yedexi1996@...com>
Subject: Re: Question about the TLS

On Tue, Dec 17, 2024 at 05:25:37PM +0100, Markus Wichmann wrote:
> Am Tue, Dec 17, 2024 at 03:11:06PM +0800 schrieb  .:
> > Hi
> > I have one question in TLS.
> > I intend to incorporate a Thread-Local Storage (TLS) variable into the musl library.&nbsp; However, the musl library does not currently support the&nbsp;
> > handling of TLS variables within the ldso library.&nbsp; Could you please advise if there are any potential risks or issues associated with adding TLS
> > &nbsp;variables and the corresponding processing logic?
> > Best
> > ye
> >
> > &nbsp;
> 
> Main problem that I see is the bootstrapping of TLS relocations inside
> of LDSO at this time. LDSO bootstrapping at this point works like this:
> 
> - Stage 1 (dlstart.c): Process relative ldso relocations.
> - Stage 2:
>     - Initialize dynamic linking structures
>     - Temporarily process symbolic ldso relocations
> - Stage 2b: Set thread pointer to builtin TLS initially.
> - Stage 3:
>     - Load LD_PRELOAD + possibly app + dependencies
>     - Process all relocations (including symbolic ldso relocs again)
>     - Properly initialize TLS and thread pointer.
> 
> The initialization of TLS requires that the allocator (malloc et al.)
> work already. This requires that all dependencies be loaded and symbolic
> relocations be done. At this point, the only way I can think of to do
> this would be to create a stage 4 for all the TLS stuff. You'd have to
> shuffle some code around, and ensure that stage 3 never calls any stuff
> that depends on TLS until it is set up in stage 4.
> 
> What do you want TLS in libc for, anyway? Mayhaps a simpler thing would
> be to add a field to struct __pthread?

The existing musl-internal way to do TLS is indeed putting the data in
struct __pthread. As Markus noted, there are complications doing TLS
relocations for libc/ldso itself, as they would have to be performed
twice, since it's not known whether libc can use TLS offset/slot zero
until the main executable has been processed, and it's not necessarily
even loaded yet.

Since eventually we probably want to support TLS in libc/ldso (for
example, the linked-in parts of libgcc might need it if we ever
support fenv for softfloat, even if musl code itself never does), I
think it makes sense to be on the lookout for clean solutions. One
that comes to mind is using negative offset for libc TLS (same
positioning as struct __pthread) so that it doesn't clash with
nonnegative offsets for other modules, and thereby doesn't necessitate
performing the relocations twice.

But in the short term if someone custmizing musl wants TLS, the way
that works is stuffing it in struct __pthread.

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.