From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by sourceware.org (Postfix) with ESMTPS id D7331385840E for ; Sat, 30 Oct 2021 13:33:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D7331385840E Received: by mail-wr1-x42a.google.com with SMTP id d5so5810270wrc.1 for ; Sat, 30 Oct 2021 06:33:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version; bh=MSvJMiVNaqtUBkxN+nZbFisXpdoNN5RdyZY2OVtxnQA=; b=2tnQuKVJgwub7Q8f7USYlXhAx25u2waE7Rz739OW5oGWBWbPksEdCYem7kUSliNTh2 rqnIzDmQq0mvJVkzhagE2soEOuEYZYl0YZAq0jjCXciEEk0MXTntpa/05qvMauIlaHl/ 7hGr+kjgtBKbzEABfRCAud/OIPIeA8WPi0kysXbWHEkqDuyt2apOE9ntDDbNrnKfYFRO hDywNMthfBVTk1R5ZazilJZnIg+8BKTfj13iyAGNh41uxveY8fDAEmM7+oivleCfE3cJ nH4iJHPr40saaNGy20A4Au6FCthp1rXZuVMgilri/0W1Ud4y5Sxy2507uebr6wvIkdeN +B3w== X-Gm-Message-State: AOAM531vpxVDMywJIN/7mbICXTGccIPm4jw1G7wL5gmKqdjbN5WQ7fu4 cEv6mx8AQTfk3gkiiPF1nz4Lng2P4kurMt5i X-Google-Smtp-Source: ABdhPJyrOgC3ygYIvI3SPscMewcQv28xNGV2Ljqtj5S52E5WJYg6tvhj2AcmXGBpZefHzo72RQ0rGA== X-Received: by 2002:a5d:4882:: with SMTP id g2mr21717487wrq.399.1635600815826; Sat, 30 Oct 2021 06:33:35 -0700 (PDT) Received: from [192.168.58.42] (sheaf.site. [172.104.132.207]) by smtp.gmail.com with ESMTPSA id w1sm11122773wmc.19.2021.10.30.06.33.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Oct 2021 06:33:34 -0700 (PDT) Message-ID: <4f183af5fbf3fc739edd06eda289bdcffc855cba.camel@gmail.com> Subject: Re: Get TLS information without readelf From: Alexander Shpilkin To: gcc-help@gcc.gnu.org Date: Sat, 30 Oct 2021 16:33:29 +0300 In-Reply-To: <20211030074112.D9FC7140175@vultr155> References: <20211030074112.D9FC7140175@vultr155> Content-Type: multipart/signed; micalg="pgp-sha512"; protocol="application/pgp-signature"; boundary="=-BiygO0HRjuKe9DzoeAXO" User-Agent: Evolution 3.40.4 MIME-Version: 1.0 X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, KAM_INFOUSMEBIZ, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 30 Oct 2021 13:33:38 -0000 --=-BiygO0HRjuKe9DzoeAXO Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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=E2=80=99s ELF program header table as the AT_PHD= R entry in the auxiliary vector[4] provided as part of the startup information, and you=E2=80=99ll 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=E2=80=94the 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=E2=80=99t feel qualified to comment on i= t. [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 --=20 Cheers, Alex --=-BiygO0HRjuKe9DzoeAXO Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEE1Wb4bTIs+DIofPlVVfb1t1Y3vTgFAmF9SakACgkQVfb1t1Y3 vTiYvAf/ZGU4nFX0xCvq0TtANhuim1Je3UaWI1IsU2BSjgYVRe0PmjCu2XD6qJb+ /cRf3MS7k0j/ZU93naEp76axNJmBW5VnrwmKIuKez++WuXMZPfW1TV4ve+DhLx9x hrd8oL17no6CGYpGBpA5cxvG2QM38amYKVhxgc94Ywk63JJAr8V8dkTJdv0nFFEG eTjO246E1fVU4dAN6DO4vBg6rHG+2D4U/OpOl0yGSwXd87hqVfeinNRlNIPzFtMv Q0AGPxzyewo9kDdeKTgkC8+aN4TMhOX5V4LBXAbQfzXVJv5vP3uvjSiPUEvYzyoZ MGzTtwQfMYaP48wNoG2DKx4NzxsPlw== =Qy3s -----END PGP SIGNATURE----- --=-BiygO0HRjuKe9DzoeAXO--