Skip to content

[AArch64] Implement TLS relaxations#1213

Open
Deepak Shirke (deepakshirkem) wants to merge 6 commits into
qualcomm:mainfrom
deepakshirkem:fix/tls-relaxation-aarch64-1004
Open

[AArch64] Implement TLS relaxations#1213
Deepak Shirke (deepakshirkem) wants to merge 6 commits into
qualcomm:mainfrom
deepakshirkem:fix/tls-relaxation-aarch64-1004

Conversation

@deepakshirkem

@deepakshirkem Deepak Shirke (deepakshirkem) commented May 23, 2026

Copy link
Copy Markdown
Contributor

Fixes part of #1004

Implements TLS relaxations for AArch64 covering all TLS access models supported by the AArch64 ABI

Implementation follows LLD's AArch64 TLS relaxation in lld/ELF/Arch/AArch64.cpp (relaxTlsGdToLe, relaxTlsGdToIe,
relaxTlsIeToLe).

CC Parth (@parth-07) Shankar Easwaran (@quic-seaswara) quic-areg

@quic-seaswara

Copy link
Copy Markdown
Contributor

Now you have the ability to also build and run tests. It would be great if you work on a setup to test each relaxation building executables, dynamic executables, PIE, shared libraries to make sure that the behavior is consistent.

See the discussion here : #885

@deepakshirkem Deepak Shirke (deepakshirkem) force-pushed the fix/tls-relaxation-aarch64-1004 branch from d9f8fee to 638c25d Compare June 18, 2026 19:01
@deepakshirkem

Copy link
Copy Markdown
Contributor Author

Hi Shankar Easwaran (@quic-seaswara), I have added a runtime test (TLS_RunTest) that verifies TLS relaxations work correctly by actually executing the linked binary under QEMU.

Extend IE->LE relaxation in tls_gottprel_page and tls_gottprel_lo
to cover global TLS symbols in static executables. Previously only
local symbols (without ReserveGOT) were relaxed.

Fixes part of qualcomm#1004

Signed-off-by: deepakshirkem <deepakshirke509@gmail.com>
Preserve destination register (Rd) when relaxing TLSDESC_LD64_LO12
to movk in static executables. Previously x0 was hardcoded.

Also extend relaxation to cover global symbols in static executables
by adding isCodeStatic() check, matching the IE->LE fix.

Fixes part of qualcomm#1004

Signed-off-by: deepakshirkem <deepakshirke509@gmail.com>
…cutables

Extend TLSDESC->LE relaxation in tls_tlsdesc_page to cover global
TLS symbols in static executables by adding isCodeStatic() check.

Also fix tls_tlsdesc_lo to always use x0 for the relaxed movk
instruction, matching LLD behavior and ensuring the TLS offset
is in the correct register after relaxation.

Add test for TLSDESC->LE relaxation covering both global and
local TLS symbols.

Fixes part of qualcomm#1004

Signed-off-by: deepakshirkem <deepakshirke509@gmail.com>
For PIE executables, relax TLSDESC sequences to Initial Exec (IE)
instead of keeping the expensive TLSDESC runtime resolver call.

Changes:
- scanLocalReloc/scanGlobalReloc: detect isPIE() and create IE GOT
  entry with R_AARCH64_TLS_TPREL64 dynamic relocation instead of
  TLSDESC GOT with R_AARCH64_TLSDESC
- tls_tlsdesc_lo: rewrite ldr to IE GOT load into x0 for PIE
- tls_tlsdesc_add: emit nop for PIE (matching static behavior)
- tls_call: emit nop for PIE (matching static behavior)
- Update DESC.test placeholder with correct CHECK lines
- Add TLS_DESC_PIE.test to verify TLSDESC->IE relaxation

Fixes part of qualcomm#1004

Signed-off-by: deepakshirkem <deepakshirke509@gmail.com>
Add TLS_Relaxation_Verify test using real compiler-generated TLS
relocations to verify end-to-end correctness of relaxations:
- TLSDESC->LE for static executables (movz+movk+nop+nop)
- TLSDESC->IE for PIE executables (adrp+ldr+nop+nop)
- R_AARCH64_TLS_TPREL64 dynamic reloc present in PIE

Add IE_PIE test to verify IE relocations remain as GOT-indirect
adrp+ldr in PIE executables.

Fixes part of qualcomm#1004

Signed-off-by: deepakshirkem <deepakshirke509@gmail.com>
Local TLS symbols in PIE executables were incorrectly getting
R_AARCH64_TLS_TPREL64 dynamic relocations. Since local symbols
have no name in the dynamic symbol table, the dynamic linker
cannot resolve these relocations, causing a segmentation fault
at runtime.

Fix by checking rsym->isLocal() in the PIE path:
- Local symbols: fill GOT at link time (TLSStaticSymbolValue)
- Global symbols: emit R_AARCH64_TLS_TPREL64 dynamic reloc

This bug was caught by adding runtime tests that actually
execute the linked binary under QEMU.

Add TLS_RunTest to verify TLS relaxations work correctly at
runtime for static, PIE, and dynamic executables.

Fixes part of qualcomm#1004

Signed-off-by: deepakshirkem <deepakshirke509@gmail.com>
@deepakshirkem Deepak Shirke (deepakshirkem) force-pushed the fix/tls-relaxation-aarch64-1004 branch from 638c25d to eb7c74d Compare June 19, 2026 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants