Write alignment of `.tls` into `_tls_used.Characteristics`
Updates binutils to correctly encode TLS variable alignment in PE executables.
This patch updates binutils’ BFD library to correctly handle the alignment of Thread Local Storage (TLS) variables in Windows PE executables. Since the Windows 8.1 SDK, the Characteristics field in the TLS directory has a dedicated subfield for Alignment. This patch ensures that when a user defines an over-aligned thread-local variable (e.g., using alignas), its alignment is correctly encoded in the _tls_used.Alignment field, matching the behavior of Clang and MSVC. This allows the system to allocate TLS storage with the proper alignment.
- proposer
Submits a patch to correctly write the alignment of `.tls` sections into the `_tls_used.Characteristics` field of the PE executable's TLS directory.
“Since Windows 8.1 SDK, Microsoft have replaced `IMAGE_TLS_DIRECTORY{32,64}::Characteristics` with union { DWORD Characteristics; struct { DWORD Reserved0 : 20; DWORD Alignment : 4; DWORD Reserved1 : 8; }; }; When a user defines an over-aligned thread-local variable like alignas(128) thread_local int overaligned = 42; the alignment of TLS t…”
In Details
This patch modifies binutils' BFD library, specifically the PE (Portable Executable) object file format generation code (bfd/peXXigen.c). It addresses how the alignment of Thread Local Storage (TLS) variables is recorded in the PE header. Modern Windows systems (since Win 8.1 SDK) use a specific bitfield within the Characteristics member of the IMAGE_TLS_DIRECTORY to specify TLS data alignment. This patch ensures that binutils correctly writes this alignment value when over-aligned TLS variables are used, aligning with compilers like Clang and MSVC.
- TLS
- Thread Local Storage. A mechanism that allows variables to have unique instances for each thread of execution within a process.
- PE
- Portable Executable. The file format used for executables, DLLs, and object files on Windows operating systems.
- BFD
- Binary File Descriptor library, a part of binutils that provides a common interface for reading and writing various executable binary file formats.
- Characteristics
- A field in various PE structures (like section headers or the TLS directory) that contains flags describing the properties or attributes of the associated entity.
- alignas
- A C++ keyword used to specify the alignment of a variable or type.
- over-aligned
- A variable or data structure that requires an alignment greater than the default alignment typically provided for its type.