Paths in pieces
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Paths in pieces
Many Linux distributions and package managers audit just-built binaries for non-relocatable absolute paths. If a binary contains a path /path/from/build/machine, it might not run on the end-user’s machine. Now, a basic grep on the binary, or a slightly more sophisticated $ strings bin/app | grep /path/from/build/machine using GNU binutils’s strings will detect these paths, right? The answer is no, at least when using GCC and targeting x86-64: you should not assume that string constants end up literally in the binary.
许多 Linux 发行版和软件包管理器会审计刚构建的二进制文件,以查找不可重定位的绝对路径。如果二进制文件包含 /path/from/build/machine 这样的路径,它可能无法在最终用户的机器上运行。那么,通过对二进制文件进行基本的 grep 操作,或者使用 GNU binutils 的 strings 工具执行 $ strings bin/app | grep /path/from/build/machine 这样稍微复杂一点的操作就能检测到这些路径了吗?答案是否定的,至少在使用 GCC 并针对 x86-64 架构时是这样:你不应假设字符串常量会以字面形式完整地出现在二进制文件中。
Basic example
In this C++ source we create a std::string from a string constant:
#include <iostream>
#include <string>
#define PREFIX "/home/linuxbrew/.linuxbrew"
int main() {
std::string prefix{PREFIX};
std::cout << prefix << '\n';
}
Compile with GCC 12 or later and run, which prints the path:
$ g++ -O2 main.cpp -o main
$ ./main
/home/linuxbrew/.linuxbrew
But the path occurs precisely 0 times in the binary itself.
基本示例
在以下 C++ 源代码中,我们从一个字符串常量创建了一个 std::string:
#include <iostream>
#include <string>
#define PREFIX "/home/linuxbrew/.linuxbrew"
int main() {
std::string prefix{PREFIX};
std::cout << prefix << '\n';
}
使用 GCC 12 或更高版本编译并运行,它会打印出该路径:
$ g++ -O2 main.cpp -o main
$ ./main
/home/linuxbrew/.linuxbrew
但该路径在二进制文件本身中出现的次数恰好为 0。
Store by pieces
The answer is that GCC optimizes our std::string{PREFIX}. The fact that we see something that looks like the prefix is merely a coincidence. GCC splits our string up into two (overlapping) pieces of 16 bytes, which fit exactly in xmm registers, so that it can copy our string to the std::string buffer using vectorized move instructions.
分段存储
答案是 GCC 对我们的 std::string{PREFIX} 进行了优化。我们看到看起来像前缀的内容仅仅是一个巧合。GCC 将我们的字符串拆分为两个(重叠的)16 字节片段,这些片段正好可以放入 xmm 寄存器中,以便它可以使用向量化移动指令将字符串复制到 std::string 缓冲区中。
Deduplication
Some package managers like Conda and Spack install packages into a padded prefix during their builds, with the idea that they can be “relocated” to a shorter prefix of the user’s choice: the binaries are patched in-place. Conda uses a repeated string _placehold in a directory name to make the prefix long enough. Now, even if we pick a 256-byte path and keep our fingers crossed that GCC puts the pieces of the path contiguously in the binary, we do not see our prefix back in the binary.
去重
一些软件包管理器(如 Conda 和 Spack)在构建过程中将软件包安装到带有填充的前缀中,其理念是它们可以被“重定位”到用户选择的更短的前缀:二进制文件会被原地修补。Conda 在目录名中使用重复的字符串 _placehold 来使前缀足够长。现在,即使我们选择了一个 256 字节的路径,并祈祷 GCC 将路径片段连续地放置在二进制文件中,我们也无法在二进制文件中看到我们的前缀。
Is this optimization helpful?
For string literals, I don’t think so. Clang does not split strings into constants, yet it generates essentially the same instructions. So what does GCC gain? In terms of performance, probably nothing measurable: both do the same loads and stores. In terms of size, GCC’s pieces can take more space than the string itself: overlapping bytes are stored twice, and the pieces are aligned to 16, 32 or 64 bytes. Presumably deduplication does not save much in practice, especially now that some distributions…
这种优化有帮助吗?
对于字符串字面量,我认为没有。Clang 不会将字符串拆分为常量,但它生成的指令本质上是相同的。那么 GCC 获得了什么?在性能方面,可能没有任何可衡量的提升:两者执行相同的加载和存储操作。在大小方面,GCC 的片段所占用的空间可能比字符串本身还要大:重叠的字节被存储了两次,并且片段被对齐到 16、32 或 64 字节。据推测,去重在实践中并不能节省太多空间,尤其是现在一些发行版……