1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//! Static TLS (local-exec) layout: the thread-pointer-relative offset
//! arithmetic shared by every architecture.
//!
//! A static executable carries its TLS template (`.tdata` for initialised
//! thread-locals, `.tbss` for zero-init ones) inside a `PT_TLS` program
//! header. At load time the runtime allocates one per-thread copy and points
//! the thread pointer (TP) at a fixed position relative to it. A local-exec
//! relocation stores the offset of a variable from the TP (`TPOFF`), so the
//! linker must convert each TLS symbol's position within the template into a
//! TP-relative offset following the per-arch TP convention.
//!
//! The conventions and the formulas below mirror lld's `getTlsTpOffset`
//! (`lld/ELF/InputSection.cpp`), which is the authoritative reference:
//!
//! * Variant 2 (x86-64): the static TLS block sits immediately below the TP, so
//! a variable's `TPOFF` is typically negative. The block is padded so that
//! `TP - p_memsz` is congruent to `p_vaddr` modulo `p_align`.
//! * Variant 1 (`AArch64`): the TP is followed by a two-word gap (16 bytes on
//! `AArch64`), then alignment padding, then the block; offsets are
//! non-negative.
//! * Variant 1 (RISC-V): like `AArch64` but with no gap; the TP is followed by
//! alignment padding then the block.
//!
//! `p_vaddr`, `p_memsz` and `p_align` are the `PT_TLS` fields. Because xold
//! uses an identity map with page-aligned placement, `p_vaddr` is a multiple
//! of `p_align` in practice, but the formulas are applied verbatim so the
//! result matches lld for any placement.
use crateTarget;
/// The laid-out static TLS block, mirroring the fields of a `PT_TLS` program
/// header.
///
/// `offset`/`vaddr` locate the start of `.tdata` (or `.tbss` when no `.tdata`
/// is present); `filesz` is the file-backed size and `memsz` the in-memory size
/// including `.tbss`.
/// The thread-pointer-relative offset (`TPOFF`) for a TLS symbol, following
/// the per-arch TP convention.
///
/// The arithmetic is modular (wrapping) to match lld's unsigned computation;
/// callers that need a signed interpretation (e.g. x86-64 `TPOFF32`)
/// reinterpreting the result when it is stored. `sym_off` is the symbol's
/// offset from the block start (`sym_vaddr - block.vaddr`).
/// The offset of a TLS symbol within its own module's block (`DTPOFF`), given
/// the thread-pointer-relative offset [`tpoff`] produced for it in `block`.
///
/// Both describe the same storage from different origins, so this is exactly
/// the inverse of that function: the general-dynamic model stores a
/// module-relative offset in the GOT, while every direct access uses a
/// thread-pointer-relative one.