This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
Re: Confused about TLS handling
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Nathan Sidwell <nathan at codesourcery dot com>
- Cc: binutils at sources dot redhat dot com
- Date: Tue, 17 Feb 2004 19:48:32 +0100
- Subject: Re: Confused about TLS handling
- References: <403274F6.9010104@codesourcery.com>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Tue, Feb 17, 2004 at 08:09:26PM +0000, Nathan Sidwell wrote:
> Hi,
> I'm doing a binutils port to an embedded system where we want some
> kind of thread-local storage mechanism. I'm using the elf TLS stuff
> for that, as it seems appropriate, but I'm getting confused because
> both bfd and ld have some strange handling of .tbss like sections.
> See the attached partial diff for the hacks I've done to get things
> to work. AFAICT, binutils is trying to treat .tbss sections
> as if they are initialized data, and take up space in the executable.
Nope. binutils treats .tbss specially, so that it occupies no
space in the program's virtual memory nor in the file (unlike say .bss),
but occupies space only in the PT_TLS segment.
Say for
__thread int a[16];
__thread int b[16] = { 24 };
int main (void) {}
you want:
...
[16] .data PROGBITS 08049418 000418 00000c 00 WA 0 0 4
[17] .tdata PROGBITS 08049440 000440 000040 00 WAT 0 0 32
[18] .tbss NOBITS 08049480 000480 000040 00 WAT 0 0 32
[19] .dynamic DYNAMIC 08049480 000480 0000c8 08 WA 5 0 4
[20] .ctors PROGBITS 08049548 000548 000008 00 WA 0 0 4
[21] .dtors PROGBITS 08049550 000550 000008 00 WA 0 0 4
[22] .jcr PROGBITS 08049558 000558 000004 00 WA 0 0 4
[23] .got PROGBITS 0804955c 00055c 000014 04 WA 0 0 4
[24] .bss NOBITS 08049570 000570 000004 00 WA 0 0 4
[25] .comment PROGBITS 00000000 000570 000132 00 0 0 1
...
and:
...
LOAD 0x000418 0x08049418 0x08049418 0x00158 0x0015c RW 0x1000
...
TLS 0x000440 0x08049440 0x08049440 0x00040 0x00080 R 0x20
If bfd/ld skipped the special handling, .tbss would occupy 0x40 bytes in VA,
so .dynamic would need to start at 080494c0 in the above example.
The PT_TLS segment tells the threading library to initialize first
64 bytes of thread local storage with 64 bytes at 0x08049440 (i.e. the .tdata
section after relocation) and to clear the remaining 64 bytes, in every thread.
Jakub