Paths in Pieces stoppels.ch · 14 points · 0 comments · HN thread TL;DR The autho...

@best

Paths in Pieces stoppels.ch · 14 points · 0 comments · HN thread

TL;DR The author shows that GCC (12 and later, targeting x86-64) often does not store string literals contiguously in binaries when they are copied into buffers, because it splits them into overlapping 16-byte or wider vector constants. As a result, grep and strings can miss absolute build paths that post-build relocatability audits and binary patching rely on. Clang keeps the literal intact but also copies it with overlapping vector moves, and the author argues that keeping strings literal still helps binary interpretability.

  • A std::string constructed from a string literal often does not contain the literal in the binary: GCC stores two overlapping 16-byte pieces as constants, so grep for the path finds 0 matches.
  • strings can show a plausible but wrong path (e.g. /home/linuxbrew/xbrew/.linuxbrew) because the pieces happen to be adjacent in .rodata.
  • For Conda-style padded paths, GCC exploits the cyclic _placehold repeat to store only 5 distinct 16-byte constants, but the path still breaks up in the binary.
  • Clang keeps the literal in .rodata.str1.1 and copies it with overlapping ymm moves, so the speed and size difference is small; GCC's pieces can even be larger than the string.
  • Only -Os or -mstore-max=128 reduce the effect, and the author found no clean way to opt out in GCC; a gcc-patches thread proposes an opt-out.
  • Many distributions and package managers (RPM, Arch, Homebrew, conda-build and others) audit or patch absolute paths, so this optimization can silently defeat those checks.

Discussion No comments yet, so there is no discussion to summarize at the time of this digest.

#GCC #compilers #binaries #relocatability #linux #optimization