c - 如何减少 ELF section padding?

标签 c gcc elf crosstool-ng

我使用 crosstool-NG 创建了一个包含 gcc 6.3.0 和 glibc 2.25 的 PowerPC 工具链。我有以下测试程序,test.c:

int main() { return 0; }

我是用命令编译的:

powerpc-unknown-linux-gnu-gcc -s -Os -o test test.c

最终的二进制文件是 66904 字节,比预期的要大得多。节标题如下所示:

$ powerpc-unknown-linux-gnu-readelf -S test
There are 27 section headers, starting at offset 0x10120:

Section Headers:
[Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
[ 0]                   NULL            00000000 000000 000000 00      0   0  0
[ 1] .interp           PROGBITS        10000154 000154 00000d 00   A  0   0  1
[ 2] .note.ABI-tag     NOTE            10000164 000164 000020 00   A  0   0  4
[ 3] .hash             HASH            10000184 000184 000024 04   A  4   0  4
[ 4] .dynsym           DYNSYM          100001a8 0001a8 000040 10   A  5   1  4
[ 5] .dynstr           STRTAB          100001e8 0001e8 000045 00   A  0   0  1
[ 6] .gnu.version      VERSYM          1000022e 00022e 000008 02   A  4   0  2
[ 7] .gnu.version_r    VERNEED         10000238 000238 000020 00   A  5   1  4
[ 8] .rela.dyn         RELA            10000258 000258 00000c 0c   A  4   0  4
[ 9] .rela.plt         RELA            10000264 000264 000018 0c  AI  4  23  4
[10] .init             PROGBITS        1000027c 00027c 00004c 00  AX  0   0  4
[11] .text             PROGBITS        100002c8 0002c8 00031c 00  AX  0   0  4
[12] .fini             PROGBITS        100005e4 0005e4 000030 00  AX  0   0  4
[13] .rodata           PROGBITS        10000614 000614 000014 00   A  0   0  4
[14] .eh_frame_hdr     PROGBITS        10000628 000628 000014 00   A  0   0  4
[15] .eh_frame         PROGBITS        1000063c 00063c 000080 00   A  0   0  4
[16] .ctors            PROGBITS        1001ff1c 00ff1c 000008 00  WA  0   0  4
[17] .dtors            PROGBITS        1001ff24 00ff24 000008 00  WA  0   0  4
[18] .jcr              PROGBITS        1001ff2c 00ff2c 000004 00  WA  0   0  4
[19] .got2             PROGBITS        1001ff30 00ff30 000008 00  WA  0   0  1
[20] .dynamic          DYNAMIC         1001ff38 00ff38 0000c8 08  WA  5   0  4
[21] .data             PROGBITS        10020000 010000 000008 00  WA  0   0  4
[22] .got              PROGBITS        10020008 010008 000014 04 WAX  0   0  4
[23] .plt              NOBITS          1002001c 01001c 000060 00 WAX  0   0  4
[24] .bss              NOBITS          1002007c 01001c 000008 00  WA  0   0  4
[25] .comment          PROGBITS        00000000 01001c 00002e 01  MS  0   0  1
[26] .shstrtab         STRTAB          00000000 01004a 0000d4 00      0   0  1

您可以看到 .eh_frame 和 .ctors 部分之间有很大的跳跃。如果我使用“hd”查看文件,我可以看到 .eh_frame 和 .ctors 之间的空间完全被空字节填充。

为什么 gcc 在各部分之间添加如此巨大的空间,有什么方法可以改变它的行为吗?

最佳答案

看起来这是由于 binutils 2.27 将 PowerPC 目标的默认页面大小增加到 64k,导致嵌入式平台上的二进制文件膨胀。

在 crosstool-NG github 上有一个讨论 here .

使用 --disable-relro 配置 binutils 应该会有所改善。

也可以在gcc编译时加入-Wl,-z,max-page-size=0x1000

关于c - 如何减少 ELF section padding?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47244572/

相关文章:

c - UDP Echo 服务器未在客户端上接收

c++ - OpenCV 网格区域

c - 在 nm 报告中验证符号更改的简便方法

gcc - 什么是计算机科学中的目标架构?

c - 全局 const 优化和符号插入

c - 超快舍入函数(PBC)

python - 调用 PyEval_CallObject 时程序关闭

c - 在源代码级别使用 GCC __builtin_object_size 是否有任何动机?

创建 TCP 服务器来托管 ELF 文件

c - 当我想要一个共享库时,GCC 输出一个可执行的 ELF 文件