Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why gcc is adding .comment and .note.gnu.property section?

Tags:

c

gcc

ld

Can anyone explain why gcc is adding the .comment and .note.gnu.property sections in the object code, and how can I tell to gcc not to add them, is it possible?

Thanks.

like image 519
Alazar Avatar asked Sep 08 '25 13:09

Alazar


2 Answers

why gcc is adding the .comment and .note.gnu.property sections

You can examine the contents of the .comment section with e.g. 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.

Obviously the "why" is to make it easier to understand what compiler produced the object file.

I don't believe there is a GCC flag to suppress this, but objcopy --remove-section .comment foo.o bar.o will get rid of it.

The .note.gnu.property can be removed in similar fashion.

Here is a discussion of what it may contain.

like image 64
Employed Russian Avatar answered Sep 10 '25 03:09

Employed Russian


tks makred it, objcopy -O binary -R .note -R .comment -R .note.gnu.property foo , elf format to binary, size from 129M to 4K

like image 20
zwong Avatar answered Sep 10 '25 04:09

zwong