Chris Low-level systems developer
← Back to all posts

The Variable That Is Not the Same Variable

ISA

matters at ISA because the access lowering is fixed by the machine contract: on RISC-V the psABI names tp the thread pointer and Variant I TLS lowers to lui/add/lw off tp, verified in the cross-toolchain disassembly, and on x86-64 the %fs-based addressing lowers to the single instruction mov %fs:0xfffffffffffffffc,%ecx, read verbatim from this run's objdump.

MICROARCH

not at Microarch because nothing was timed and no pipeline, cache, or execution behavior was measured: the verified property is which per-thread storage an access lands on, which holds on any implementation.

FIRMWARE

not at Firmware because no boot, machine-mode, or supervisor-interface claim is made: the whole experiment runs in Linux userspace, and the thread pointer is installed by the kernel at thread creation, not by firmware.

KERNEL

matters at Kernel because the per-thread copy of the template and the installation of the thread pointer happen at thread creation, arranged by the kernel and the loader: the differing addresses with identical initial values prove the copy, and errno, the kernel-visible per-thread variable, behaved the same (worker 5, main 0).

ALGORITHM

not at Algorithm because no computational mechanism was constructed or measured: the verified claim is storage identity, which address a name resolves to per thread, read off running threads rather than computed.

I declared one variable this morning and got two of them. At file scope I wrote __thread int counter = 7;, then main printed the address and value, spawned a worker thread, and the worker printed the same variable's address and value. On this run main saw 0x786b5c3b873c holding 7, and the worker saw 0x786b5c3b76bc holding 7. The worker stored 42 into counter, printed 42, and joined. main printed its counter afterward: 7, untouched. Same name, same line of source, two different pieces of storage.

Identical twins in school uniforms
One name in the source, two separate copies, one per thread.

Threads share the whole address space, so per-thread state is a real problem. errno is the famous case: if every thread wrote to one global errno, one thread's failed read would overwrite another thread's clean slate. C still needs per-thread homes for errno, allocator state, and RNG seeds. The loader's answer is a template. The linker gathers every thread-local variable into a TLS template, described by the PT_TLS segment in the ELF: initialized data lives in .tdata, zeroed data in .tbss. When a thread is born, the loader copies the entire template into fresh memory for it. Each thread also gets a thread-pointer register, %fs on x86-64 and tp on RISC-V, and every access to a thread-local variable is address math off that register.

The access shape depends on what the linker knows. When the variable is defined in the binary being linked, the offset is fixed at link time and the whole access collapses to a single instruction. A shared library cannot know the offset, because the final program lays out the template, so it gets the general-dynamic model instead: load an index, call __tls_get_addr, use the returned address. The linker even rewrites the lazy form into the fast one when it can prove the variable is defined locally. The shapes, all from this run's disassembly:

    __thread int counter = 7;              /* one name, one copy per thread */
    mov %fs:0xfffffffffffffffc,%ecx        /* x86-64, offset fixed at link time: the whole access is one insn */
    lui a5,0x0; add a5,a5,tp; lw a4,0(a5)  /* RISC-V: the same idea, three instructions off the thread pointer tp */

Why not the obvious alternatives? pthread keys charge a dynamic lookup on every access, a tax on each read. A context pointer threaded through every function means rewriting every signature. A thread-id-indexed array hashes on every access. The thread pointer is already sitting in a register, so the access costs one load, and the per-thread template copy is what keeps errno, allocator state, and RNG seeds from colliding. One sentence to keep: threads share every address but not every variable, because the loader gives each thread its own copy of the TLS template, and the address of your variable is whatever your thread pointer says it is.

Related work: crash triage is $250/symptom, C audit from $350. .

Choose how to get in touch

The project brief is filled in for you.

Compose in Gmail Use my email app