[RFC] Document the %fs segment register as the thread pointer

12 views
Skip to first unread message

H.J. Lu

unread,
Aug 29, 2020, 11:38:55 AM8/29/20
to x86-64-abi
The %fs segment register is designated as the thread pointer whose value
should be stored at the absolute address 0 from the %fs segment register.

https://gitlab.com/x86-psABIs/x86-64-ABI/-/merge_requests/14

--
H.J.

Jan Beulich

unread,
Aug 31, 2020, 2:47:27 AM8/31/20
to H.J. Lu, x86-64-abi
On 29.08.2020 17:38, H.J. Lu wrote:
> The %fs segment register is designated as the thread pointer whose value
> should be stored at the absolute address 0 from the %fs segment register.

Isn't this a per-OS choice rather than something to be mandated by
the psABI?

Jan

Florian Weimer

unread,
Aug 31, 2020, 4:27:25 AM8/31/20
to Jan Beulich, H.J. Lu, x86-64-abi
* Jan Beulich:
No, it's a required part of the TLS ABI. This:

__thread int x __attribute__ ((tls_model ("initial-exec")));

int *
f (void)
{
return &x;
}

Compiles to:

f:
movq x@gottpoff(%rip), %rax
addq %fs:0, %rax
ret

It assumes that %fs:0 has this special value. I don't think there is
any other way to get the address of a thread-local variable. This is
already present in the psABI manual, which has a couple of mentions of
%fs:0 in TLS-related examples.

There is also the GCC option -mno-tls-direct-seg-refs, which hard-codes
this assumption, too, even for TLS variables whose address is not taken.

We also have a couple of more GCC assumptions about TCB layout, e.g. for
the stack protector, split stacks, and libitm. Those might be specific
to GNU/Linux.

Thanks,
Florian

Reply all
Reply to author
Forward
0 new messages