为什么 gcc 添加 .comment 和 .note.gnu.property 部分?

问题描述 投票:0回答:3

任何人都可以解释为什么 gcc 在目标代码中添加 .comment 和 .note.gnu.property 部分,以及我如何告诉 gcc 不要添加它们,可能吗?

谢谢。

c gcc ld
3个回答
4
投票

为什么 gcc 添加 .comment 和 .note.gnu.property 部分

您可以使用以下命令检查

.comment
部分的内容:
readelf -x.comment

echo "int foo() { return 42; }" | gcc -xc - -c -o foo.o
readelf -x.comment foo.o

Hex dump of section '.comment':
  0x00000000 00474343 3a202844 65626961 6e203131 .GCC: (Debian 11
  0x00000010 2e322e30 2d313029 2031312e 322e3000 .2.0-10) 11.2.0.

显然“为什么”是为了更容易理解编译器生成的目标文件。

我不相信有 GCC 标志可以抑制这一点,但

objcopy --remove-section .comment foo.o bar.o
将摆脱它。

.note.gnu.property
可以以类似的方式移除

这里是对其可能包含的内容的讨论


1
投票

tks makred it,

objcopy -O binary -R .note -R .comment -R .note.gnu.property  foo
,elf格式转二进制,大小从129M到4K


0
投票

不要删除 .note.gnu.property,它包含传递给链接器的数据,例如“堆栈大小、堆栈权限等...”,如果 .note.gnu.property 不存在,则不会加载二进制文件

© www.soinside.com 2019 - 2024. All rights reserved.