Skip to content

Commit 01cd716

Browse files
committed
Reword derivation of TLS padding to make it easier to understand
Use integers modulo m to avoid excess use of (modulo m). Explain the congruence symbol. Put expression first so derivation is optional.
1 parent 638243e commit 01cd716

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

sysvabi64/sysvabi64.rst

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,12 @@ AArch64 TLS SystemV design choices:
20712071

20722072
* AArch64 uses variant 1 TLS as described in ELFTLS_.
20732073

2074+
* There are two dialects of TLS supported by the relocations defined
2075+
in AAELF64_, the traditional dialect described by ELFTLS_ and the
2076+
descriptor dialect described by TLSDESC_. This document describes
2077+
only the descriptor dialect as this is the default dialect for GCC
2078+
and the only dialect supported by clang.
2079+
20742080
* The thread pointer (TP) is always accessible via the ``TPIDR_EL0``
20752081
system register. This can be accessed via inlining an ``mrs``
20762082
instruction to read the thread pointer.
@@ -2079,39 +2085,48 @@ AArch64 TLS SystemV design choices:
20792085
KiB, 16 MiB, 4GiB or 16EiB, depending on the addressing mode. The
20802086
default is 16 MiB for all addressing modes.
20812087

2082-
Recall that the Thread Pointer ``TP`` points to the start of the ``TCB``.
2083-
2084-
The static and dynamic linker must agree on the size of the padding
2085-
between the TCB and the executables TLS Block. Using ``TCBsize`` as the
2086-
size of the TCB (16 bytes), ``PADsize`` as the size of the padding bytes,
2087-
and ``PT_TLS`` as the program header with type PT_TLS.
2088-
2089-
The Thread Pointer ``TP`` and also the address of the start of the
2090-
``TCB``, must satisfy the requirement:
2091-
2092-
``TP ≡ 0 (modulo PT_TLS.p_align)``.
2088+
* The TLS for an executable or shared-library is described by the
2089+
``PT_TLS`` program header.
20932090

2094-
``PADsize`` must be the smallest positive integer that satisfies the
2095-
following congruence:
2091+
Recall from the diagram in `SystemV AArch64 TLS addressing
2092+
architecture`_ that the Thread Pointer ``TP`` points to the start of
2093+
the ``TCB``, which is followed by 0 or more bytes of alignment
2094+
padding, then the executable's TLS block.
20962095

2097-
``TCBsize + PADsize ≡ PT_TLS.p_vaddr (modulo PT_TLS.p_align)``.
2096+
The ``TP``, and hence the start of the ``TCB`` must be aligned to a
2097+
``PT_TLS.p_align`` boundary. This can be expressed as ``TP ≡ 0 (modulo
2098+
PT_TLS.p_align)`` where ```` means congruent to.
20982099

2099-
An expression for ``PADsize`` is therefore:
2100+
The static and dynamic linker must agree on the size of the padding
2101+
(``PADsize``) between the TCB and the executable's TLS Block. Using
2102+
``TCBsize`` as the size of the TCB (16 bytes), the following expression can be used to calcluate ``PADsize`` from the ``PT_TLS`` program header.
21002103

21012104
``PADsize = (PT_TLS.p_vaddr - TCBsize) mod PT_TLS.p_align``.
21022105

21032106
A number of dynamic linkers use a different calculation that requires
21042107
``PT_TLS.p_vaddr ≡ 0 (modulo PT_TLS.p_align)`` to correctly align the
2105-
executables TLS block, for either static or dynamic TLS. For maximum
2108+
executables TLS block. In this case the expression above simplifies to
2109+
``PADsize = Max(0, PT_TLS.p_align - TCBsize``). For maximum
21062110
compatibility, static linkers and any linker scripts including TLS,
2107-
are recommended to align the TLS block so that `PT_TLS.p_vaddr ≡ 0
2108-
(modulo p_align)`.
2109-
2110-
There are two dialects of TLS supported by the relocations defined in
2111-
AAELF64_, the traditional dialect described by ELFTLS_ and the
2112-
descriptor dialect described by TLSDESC_. This document describes only
2113-
the descriptor dialect as this is the default dialect for GCC and the
2114-
only dialect supported by clang.
2111+
are recommended to align the TLS block so that ``PT_TLS.p_vaddr ≡ 0
2112+
(modulo p_align)``. This requires the start of the TLS to be aligned
2113+
to the maximum of the .tdata and .tbss sections.
2114+
2115+
The expression for ``PADsize`` above can be derived from the
2116+
requirement that ``PADsize`` must be the smallest positive integer
2117+
that satisfies the following congruence:
2118+
2119+
``TP`` + ``TCBsize + PADsize ≡ PT_TLS.p_vaddr (modulo PT_TLS.p_align)``.
2120+
2121+
Using Integers modulo m where (``PT_TLS.p_align``).
2122+
``TP:sub:m + TCBsize:sub:m + PADsize:sub:m = PT_TLS.p_vaddr:sub:m``
2123+
2124+
As ``TP:sub:m`` is 0 as ``TP ≡ 0 (modulo PT_TLS.p_align)`` rearranging
2125+
we get:
2126+
2127+
``PADsize:sub:m = PT_TLS.p_vaddr:sub:m - TCBsize:sub:m``
2128+
which is equivalent to
2129+
``PADsize:sub:m = (PT_TLS.p_vaddr - TCBsize):sub:m``.
21152130

21162131
Code sequences for accessing TLS variables
21172132
------------------------------------------

0 commit comments

Comments
 (0)