public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Get TLS information without readelf
@ 2021-10-30  7:41 Jeff Andrews
  2021-10-30 13:33 ` Alexander Shpilkin
  0 siblings, 1 reply; 2+ messages in thread
From: Jeff Andrews @ 2021-10-30  7:41 UTC (permalink / raw)
  To: gcc-help

Hi all. I'd like to use tls (specifically __thread) with g++ but without a standard library. For the moment all my variables are zero initalized. How should I get the TLS memory size? I can see all the information I need in the TLS header after I build the binary by using "readelf -l". I'll need "MemSiz", possibly "Align" and once I have non zero thread locals I'll need VirtAddr and FileSiz. Is there a way to export these as a symbol/global variable?

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Get TLS information without readelf
  2021-10-30  7:41 Get TLS information without readelf Jeff Andrews
@ 2021-10-30 13:33 ` Alexander Shpilkin
  0 siblings, 0 replies; 2+ messages in thread
From: Alexander Shpilkin @ 2021-10-30 13:33 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 2124 bytes --]

On Sat, 2021-10-30 at 03:41 -0400, Jeff Andrews wrote:
> Hi all. I'd like to use tls (specifically __thread) with g++ but
> without a standard library. For the moment all my variables are zero
> initalized. How should I get the TLS memory size? I can see all the
> information I need in the TLS header after I build the binary by using
> "readelf -l". I'll need "MemSiz", possibly "Align" and once I have non
> zero thread locals I'll need VirtAddr and FileSiz. Is there a way to
> export these as a symbol/global variable?

You can always define whatever symbols you need if you use your own GNU
linker script[1] or amend the default[2], and in fact the default one
already does this for some things (e.g. save the beginning and end of
BSS so that the initialization code can zero it out).  As objects are
linked in command line order unless the linker script says otherwise,
another approach is to include sentinel values in crt{begin,end}.o[3]
or whatever you are replacing them with in your no-libc setup.

Finally, if you are on an ELF system such as Linux or the BSDs, all
this may be unnecessary, because the kernel will actually include a
pointer to your executable’s ELF program header table as the AT_PHDR
entry in the auxiliary vector[4] provided as part of the startup
information, and you’ll find the things you need in the PT_TLS
header[5] (though IMO it makes more sense to do this work at link time
as above—the header seems to be necessary in a dynamic linking
situation, where the loader will have to stitch together the TLS
layouts from multiple ELF images and relocate accordingly).

Actually following the platform ABI on TLS[6] is another matter, of
course, and quite arcane, so I don’t feel qualified to comment on it.

[1]: https://wiki.osdev.org/Linker_Scripts
[2]: https://sourceware.org/binutils/docs/ld/Implicit-Linker-Scripts.html
[3]: https://dev.gentoo.org/~vapier/crt.txt
[4]: https://lwn.net/Articles/519085/
[5]: https://www.akkadia.org/drepper/tls.pdf
[6]: https://maskray.me/blog/2021-02-14-all-about-thread-local-storage

-- 
Cheers,
Alex

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-10-30 13:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-30  7:41 Get TLS information without readelf Jeff Andrews
2021-10-30 13:33 ` Alexander Shpilkin

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 read-only IMAP folder(s) and NNTP newsgroup(s).