mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: Markus Wichmann <nullplan@gmx.net>
Cc: musl@lists.openwall.com, " ." <yedexi1996@qq.com>
Subject: Re: [musl] Question about the TLS
Date: Sat, 21 Dec 2024 01:22:48 -0500	[thread overview]
Message-ID: <20241221062248.GV10433@brightrain.aerifal.cx> (raw)
In-Reply-To: <Z2GmAZyr3-bOw74z@voyager>

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

      reply	other threads:[~2024-12-21  6:23 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-17  7:11  .
2024-12-17 16:25 ` Markus Wichmann
2024-12-21  6:22   ` Rich Felker [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241221062248.GV10433@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    --cc=nullplan@gmx.net \
    --cc=yedexi1996@qq.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).