Format:ELF (Executable and Linkable Format)/TLS
Thread-local storage in ELF: the PT_TLS segment (.tdata/.tbss), STT_TLS symbols, the four access models (general dynamic, local dynamic, initial exec, local exec) and how each looks in x86-64 and AArch64 code and relocations.
Where the data is
PT_TLS(7) describes the initial image of one thread's TLS block:p_offset/p_filesz= the initialised bytes (.tdata, section flag SHF_TLS 0x400),p_memsz-p_filesz= the zero-filled tail (.tbss, NOBITS),p_align= required alignment. Every new thread gets a copy; the executable's block is allocated with the thread, other modules' blocks at load time or lazily.STT_TLS(6) symbols:st_valueis an offset inside the module's TLS block, not an address. A debugger or disassembler that treats it as an address shows nonsense.DF_STATIC_TLS(0x10) in DT_FLAGS: the module uses the initial-exec/local-exec model and cannot be dlopen'ed late.
Thread pointer
x86-64 uses %fs (TLS block just below the thread pointer: offsets from it are negative, 'variant II'). AArch64 uses system register tpidr_el0 (block just above a 16-byte TCB: offsets positive, 'variant I'). 32-bit x86 uses %gs.
The four models (x86-64)
| Model | Used for | Code | Relocation |
|---|---|---|---|
| General dynamic | variable in another shared object, -fPIC | leaq x@tlsgd(%rip),%rdi; call __tls_get_addr@PLT (prefixed 66, 66 48 padding so the linker can rewrite it) |
R_X86_64_TLSGD (19), plus DTPMOD64/DTPOFF64 dynamic relocs in the GOT |
| Local dynamic | several variables of the same module, -fPIC | leaq x@tlsld(%rip),%rdi; call __tls_get_addr@PLT; leaq x@dtpoff(%rax),%rdx |
R_X86_64_TLSLD (20), DTPOFF32 (21) |
| Initial exec | variable in a library loaded at startup | movq x@gottpoff(%rip),%rax; movq %fs:(%rax),%rcx |
R_X86_64_GOTTPOFF (22) with R_X86_64_TPOFF64 (18) in the GOT |
| Local exec | executable's own variable | movq %fs:x@tpoff,%rax or movq %fs:0,%rax; leaq x@tpoff(%rax),... (64 48 8b 04 25 00 00 00 00) |
R_X86_64_TPOFF32 (23), resolved at link time |
The linker relaxes the slow models to faster ones when it can (GD to LE or IE, LD to LE, IE to LE), rewriting the instruction bytes, so an executable usually contains only %fs:-based accesses and __tls_get_addr calls survive only in shared objects. __tls_get_addr takes a pointer to a {module id, offset} pair (the tls_index) in the GOT.
TLS descriptors (R_X86_64_TLSDESC 36, R_AARCH64_TLSDESC 1031; selected with -mtls-dialect=gnu2 on x86-64 and -mtls-dialect=desc on AArch64): the GOT holds a two-word descriptor {resolver function, argument}; the code calls through it (marker relocs R_X86_64_TLSDESC_CALL 35, R_AARCH64_TLSDESC_CALL 569) and gets the offset from the thread pointer.
AArch64
General dynamic: adrp x0, :tlsgd:x; add x0,x0,:tlsgd_lo12:x; bl __tls_get_addr (relocs 513, 514), local exec: mrs x0, tpidr_el0; add x0,x0,#:tprel_hi12:x; add x0,x0,#:tprel_lo12_nc:x (549, 551), initial exec via adrp+ldr :gottprel_lo12: (541, 542), descriptor sequence adrp/ldr/add/blr with 562, 563, 564, 569. Dynamic relocation types: 1028 TLS_DTPMOD, 1029 TLS_DTPREL, 1030 TLS_TPREL, 1031 TLSDESC.
Reversing hints
- In a stripped x86-64 executable,
mov %fs:0x28,%raxat function entry and exit is the stack protector canary read, not a TLS variable of the program.%fs:0x0holds the thread pointer itself. errnois*__errno_location()on glibc and musl,__errno()on bionic; they are not direct%fsaccesses.- Rust and C++
thread_localobjects add a guard/destructor registration via__cxa_thread_atexit.
Sources: LLVM lld/ELF/Arch/X86_64.cpp (relaxation code and comments), glibc elf.h (relocation numbers). Unverified here: the byte-exact padding prefixes for the GD sequences; check with a compiled sample.