why gcc is adding .comment and section?

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



  • why gcc is adding the .comment and 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 can be removed in similar fashion.

    Here is a discussion of what it may contain.