GCC 减少二进制膨胀——奇怪的副作用

发布于 2024-09-17 15:55:34 字数 3827 浏览 9 评论 0原文

怪异

我已经编译了 Google Protocol Buffers,没有使用额外的参数进行“膨胀”编译,并使用以下命令进行编译 ./configure CXXFLAGS="-ffunction-sections -fdata-sections"。 a du-h 揭示:

120K ./bloat/bin
124K ./bloat/include/google/protobuf/io
8.0K ./bloat/include/google/protobuf/compiler/java
12K ./bloat/include/google/protobuf/compiler/python
8.0K ./bloat/include/google/protobuf/compiler/cpp
128K ./bloat/include/google/protobuf/compiler
52K ./bloat/include/google/protobuf/stubs
848K ./bloat/include/google/protobuf
852K ./bloat/include/google
856K ./bloat/include
12K ./bloat/lib/pkgconfig
37M ./bloat/lib
38M ./bloat
20K ./unbloat/bin
124K ./unbloat/include/google/protobuf/io
8.0K ./unbloat/include/google/protobuf/compiler/java
12K ./unbloat/include/google/protobuf/compiler/python
8.0K ./unbloat/include/google/protobuf/compiler/cpp
128K ./unbloat/include/google/protobuf/compiler
52K ./unbloat/include/google/protobuf/stubs
848K ./unbloat/include/google/protobuf
852K ./unbloat/include/google
856K ./unbloat/include
12K ./unbloat/lib/pkgconfig
15M ./unbloat/lib
16M ./unbloat
53M .

深入研究:

 ls -gGh bloat/lib/
    total 37M
    -rw-r--r-- 1  13M 2010-09-07 13:57 libprotobuf.a
    -rwxr-xr-x 1  986 2010-09-07 13:57 libprotobuf.la
    -rw-r--r-- 1 1.6M 2010-09-07 13:57 libprotobuf-lite.a
    -rwxr-xr-x 1 1021 2010-09-07 13:57 libprotobuf-lite.la
    lrwxrwxrwx 1   25 2010-09-07 13:57 libprotobuf-lite.so -> libprotobuf-lite.so.6.0.0
    lrwxrwxrwx 1   25 2010-09-07 13:57 libprotobuf-lite.so.6 -> libprotobuf-lite.so.6.0.0
    -rwxr-xr-x 1 771K 2010-09-07 13:57 libprotobuf-lite.so.6.0.0
    lrwxrwxrwx 1   20 2010-09-07 13:57 libprotobuf.so -> libprotobuf.so.6.0.0
    lrwxrwxrwx 1   20 2010-09-07 13:57 libprotobuf.so.6 -> libprotobuf.so.6.0.0
    -rwxr-xr-x 1 5.5M 2010-09-07 13:57 libprotobuf.so.6.0.0
    -rw-r--r-- 1  12M 2010-09-07 13:57 libprotoc.a
    -rwxr-xr-x 1 1.1K 2010-09-07 13:57 libprotoc.la
    lrwxrwxrwx 1   18 2010-09-07 13:57 libprotoc.so -> libprotoc.so.6.0.0
    lrwxrwxrwx 1   18 2010-09-07 13:57 libprotoc.so.6 -> libprotoc.so.6.0.0
    -rwxr-xr-x 1 4.6M 2010-09-07 13:57 libprotoc.so.6.0.0
    drwxr-xr-x 2 4.0K 2010-09-07 13:57 pkgconfig
    ls -gGh unbloat/lib/
    total 15M
    -rw-r--r-- 1 5.8M 2010-09-07 14:03 libprotobuf.a
    -rwxr-xr-x 1  988 2010-09-07 14:03 libprotobuf.la
    -rw-r--r-- 1 764K 2010-09-07 14:03 libprotobuf-lite.a
    -rwxr-xr-x 1 1023 2010-09-07 14:03 libprotobuf-lite.la
    lrwxrwxrwx 1   25 2010-09-07 14:03 libprotobuf-lite.so -> libprotobuf-lite.so.6.0.0
    lrwxrwxrwx 1   25 2010-09-07 14:03 libprotobuf-lite.so.6 -> libprotobuf-lite.so.6.0.0
    -rwxr-xr-x 1 393K 2010-09-07 14:03 libprotobuf-lite.so.6.0.0
    lrwxrwxrwx 1   20 2010-09-07 14:03 libprotobuf.so -> libprotobuf.so.6.0.0
    lrwxrwxrwx 1   20 2010-09-07 14:03 libprotobuf.so.6 -> libprotobuf.so.6.0.0
    -rwxr-xr-x 1 2.7M 2010-09-07 14:03 libprotobuf.so.6.0.0
    -rw-r--r-- 1 3.7M 2010-09-07 14:04 libprotoc.a
    -rwxr-xr-x 1 1.1K 2010-09-07 14:04 libprotoc.la
    lrwxrwxrwx 1   18 2010-09-07 14:04 libprotoc.so -> libprotoc.so.6.0.0
    lrwxrwxrwx 1   18 2010-09-07 14:04 libprotoc.so.6 -> libprotoc.so.6.0.0
    -rwxr-xr-x 1 1.3M 2010-09-07 14:04 libprotoc.so.6.0.0
    drwxr-xr-x 2 4.0K 2010-09-07 14:03 pkgconfig

问题

我没有更改构建脚本以在链接期间执行“--gc-sections”,因此不应unbloat 构建是否相同(如果不是更大)?是什么导致尺寸减小?

背景

我现在正在用 gcc 编译一个低级库,该库未剥离的大小为 2.5MB,剥离后的大小为 970KB。这是不可接受的,我需要删除死代码——我依赖 OpenSSL、Protocol Buffers 和 Boost 的 3 个库,并且我会将最后 2 个静态链接到我的库中。这两个静态链接库必须使用“-ffunction-sections -fdata-sections”进行编译,以便我删除死代码。

相关问题

我的下一个问题是关于如何指定用于消除死代码的根。

The Weirdness

I have compiled Google Protocol Buffers using no extra parameters for a "bloat" compile, and compile with the following command ./configure CXXFLAGS="-ffunction-sections -fdata-sections". a du-h reveals:

120K ./bloat/bin
124K ./bloat/include/google/protobuf/io
8.0K ./bloat/include/google/protobuf/compiler/java
12K ./bloat/include/google/protobuf/compiler/python
8.0K ./bloat/include/google/protobuf/compiler/cpp
128K ./bloat/include/google/protobuf/compiler
52K ./bloat/include/google/protobuf/stubs
848K ./bloat/include/google/protobuf
852K ./bloat/include/google
856K ./bloat/include
12K ./bloat/lib/pkgconfig
37M ./bloat/lib
38M ./bloat
20K ./unbloat/bin
124K ./unbloat/include/google/protobuf/io
8.0K ./unbloat/include/google/protobuf/compiler/java
12K ./unbloat/include/google/protobuf/compiler/python
8.0K ./unbloat/include/google/protobuf/compiler/cpp
128K ./unbloat/include/google/protobuf/compiler
52K ./unbloat/include/google/protobuf/stubs
848K ./unbloat/include/google/protobuf
852K ./unbloat/include/google
856K ./unbloat/include
12K ./unbloat/lib/pkgconfig
15M ./unbloat/lib
16M ./unbloat
53M .

Drill Down:

 ls -gGh bloat/lib/
    total 37M
    -rw-r--r-- 1  13M 2010-09-07 13:57 libprotobuf.a
    -rwxr-xr-x 1  986 2010-09-07 13:57 libprotobuf.la
    -rw-r--r-- 1 1.6M 2010-09-07 13:57 libprotobuf-lite.a
    -rwxr-xr-x 1 1021 2010-09-07 13:57 libprotobuf-lite.la
    lrwxrwxrwx 1   25 2010-09-07 13:57 libprotobuf-lite.so -> libprotobuf-lite.so.6.0.0
    lrwxrwxrwx 1   25 2010-09-07 13:57 libprotobuf-lite.so.6 -> libprotobuf-lite.so.6.0.0
    -rwxr-xr-x 1 771K 2010-09-07 13:57 libprotobuf-lite.so.6.0.0
    lrwxrwxrwx 1   20 2010-09-07 13:57 libprotobuf.so -> libprotobuf.so.6.0.0
    lrwxrwxrwx 1   20 2010-09-07 13:57 libprotobuf.so.6 -> libprotobuf.so.6.0.0
    -rwxr-xr-x 1 5.5M 2010-09-07 13:57 libprotobuf.so.6.0.0
    -rw-r--r-- 1  12M 2010-09-07 13:57 libprotoc.a
    -rwxr-xr-x 1 1.1K 2010-09-07 13:57 libprotoc.la
    lrwxrwxrwx 1   18 2010-09-07 13:57 libprotoc.so -> libprotoc.so.6.0.0
    lrwxrwxrwx 1   18 2010-09-07 13:57 libprotoc.so.6 -> libprotoc.so.6.0.0
    -rwxr-xr-x 1 4.6M 2010-09-07 13:57 libprotoc.so.6.0.0
    drwxr-xr-x 2 4.0K 2010-09-07 13:57 pkgconfig
    ls -gGh unbloat/lib/
    total 15M
    -rw-r--r-- 1 5.8M 2010-09-07 14:03 libprotobuf.a
    -rwxr-xr-x 1  988 2010-09-07 14:03 libprotobuf.la
    -rw-r--r-- 1 764K 2010-09-07 14:03 libprotobuf-lite.a
    -rwxr-xr-x 1 1023 2010-09-07 14:03 libprotobuf-lite.la
    lrwxrwxrwx 1   25 2010-09-07 14:03 libprotobuf-lite.so -> libprotobuf-lite.so.6.0.0
    lrwxrwxrwx 1   25 2010-09-07 14:03 libprotobuf-lite.so.6 -> libprotobuf-lite.so.6.0.0
    -rwxr-xr-x 1 393K 2010-09-07 14:03 libprotobuf-lite.so.6.0.0
    lrwxrwxrwx 1   20 2010-09-07 14:03 libprotobuf.so -> libprotobuf.so.6.0.0
    lrwxrwxrwx 1   20 2010-09-07 14:03 libprotobuf.so.6 -> libprotobuf.so.6.0.0
    -rwxr-xr-x 1 2.7M 2010-09-07 14:03 libprotobuf.so.6.0.0
    -rw-r--r-- 1 3.7M 2010-09-07 14:04 libprotoc.a
    -rwxr-xr-x 1 1.1K 2010-09-07 14:04 libprotoc.la
    lrwxrwxrwx 1   18 2010-09-07 14:04 libprotoc.so -> libprotoc.so.6.0.0
    lrwxrwxrwx 1   18 2010-09-07 14:04 libprotoc.so.6 -> libprotoc.so.6.0.0
    -rwxr-xr-x 1 1.3M 2010-09-07 14:04 libprotoc.so.6.0.0
    drwxr-xr-x 2 4.0K 2010-09-07 14:03 pkgconfig

The Question

I have not altered the build scripts to perform a "--gc-sections" during linking, therefore shouldn't the unbloat build be the same if not bigger ? What caused the reduction in size ?

Background

I am compiling a low-level library with gcc at the moment and the library is a ginormous 2.5MB unstriped and 970KB stripped. This is unacceptable, and I need to remove dead-code -- I depend on OpenSSL, Protocol Buffers and 3 Libraries from Boost, and I will static link the last 2 into my library. The two statically linked libraries will have to be compiled them with the "-ffunction-sections -fdata-sections" for me to remove dead code.

Related Question

My next question is about how to specify the root used to eliminate dead code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡紫姑娘! 2024-09-24 15:56:47

使用 -ffunction-sections 进行编译会导致每个函数被发送到自己的部分,这会导致每个目标文件变得更大(而不仅仅是 .text 部分,您现在有 .text.foo.text.bar 等)。 -fdata-sections 相同。所以你得到的结果正是预期的。

但您不应该关心您的构建面积有多大。您应该关心的是最终的可执行文件(或共享库)有多大。

使用您指定的标志,“膨胀”可执行文件可能仍然更大(但可能不会太大)。现在添加 -Wl,--gc-sections,您的“膨胀”可执行文件将明显变得更小。

Compiling with -ffunction-sections causes every function to be emitted into its own section, and that causes every object file to become larger (instead of just .text section, you now have .text.foo, .text.bar, etc.). Same for -fdata-sections. So the result you've got is exactly what's expected.

But you shouldn't care how big your build area is. What you should care about is how big your final executable (or shared library) is.

With the flags you've specified, the "bloat" executable may still be bigger (but probably not by much). Now add -Wl,--gc-sections, and your "bloat" executable will become markedly smaller.

请帮我爱他 2024-09-24 15:56:27

恐怕增益与 -ffunction-sections -fdata-sections 无关:当您在配置上指定 CXXFLAGS="-ffunction-sections -fdata-sections" 时命令行中,您覆盖了默认标志 ​​-O2 -g -DNDEBUG。因此,您的代码是在没有优化的情况下编译的。

您应该使用 CXXFLAGS="-ffunction-sections -fdata-sections -O2 -g -DNDEBUG" 重新进行测试,您将获得预期(即相同)的结果。

I am afraid the gain has nothing to do with -ffunction-sections -fdata-sections: when you specified CXXFLAGS="-ffunction-sections -fdata-sections" on configure command line, you overwrote the default flags which were -O2 -g -DNDEBUG. As a consequence, your code was compiled with no optimizations.

You should redo your test with CXXFLAGS="-ffunction-sections -fdata-sections -O2 -g -DNDEBUG" and you will get the expected (i.e., identical) results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文