Paths in pieces: how GCC splits string constants in binaries
Many Linux distributions and package managers check built binaries for absolute paths that are not relocatable[1], which may stop them from running on other machines. The author shows that a grep or strings search can miss such a path. When compiling with GCC 12 or later for x86-64, a string copied into a std::string is split into two overlapping 16-byte pieces that are stored as constants, so the path never appears whole in the file, and strings prints a similar but different path.
The same splitting applies to paths with padding[2] used by Conda and Spack, where a 256-byte path made of the repeated string _placehold is stored as only 5 distinct 16-byte constants. Clang does not split strings. It copies them with two overlapping 32-byte loads, and the author sees almost no difference in speed between the two compilers. Splitting can take more space than the string itself, and it pays off only when strings share a prefix of at least 16 bytes for SSE2, 32 bytes for AVX2[3] or 64 bytes for AVX-512.
Only -Os avoids the splitting in practice. The option -mstore-max=128 restores older defaults, but strings up to 256 bytes are still split. The author considers Debian's bit-for-bit rebuild checks a more reliable way to keep machine-specific details out of binaries. He still argues that keeping strings whole is useful for inspecting binaries, much as Fedora and Ubuntu re-enabled frame pointers[4] for profiling.
Read the original stoppels.chDiscussion
Sources
-
stoppels.ch
Paths in Pieces


