user562854
user562854

Reputation:

Why does the compiler version appear in my ELF executable?

I've recently compiled a simple hello world C program under Debian Linux using gcc:

gcc -mtune=native -march=native -m32 -s -Wunused -O2 -o hello hello.c

The file size was 2980 bytes. I opened it in a hex editor and i saw the following lines:

GCC: (Debian 4.4.5-8) 4.4.5 GCC: (Debian 4.4.5-10) 4.4.5 .shstrtab .interp .note.ABI-tag .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rel.dyn .rel.plt .init .text .fini .rodata .eh_frame .ctors .dtors .jcr .dynamic .got .got.plt data.data .bss .comment

Are they really needed? No way to reduce executable size?

Upvotes: 15

Views: 9088

Answers (6)

Simon Perretta
Simon Perretta

Reputation: 73

I had the same issue myself, but using MinGW's GCC implementation - stripping the executable and passing the -Qn option did nothing, and I couldn't remove the ".comment" section as there wasn't one.

In order to stop the compiler including this information, regardless of which sections are in your executable, you can pass the -fno-ident parameter to the compiler and linker:

Without the parameter (strings -a [filename]):

!This program cannot be run in DOS mode.
.text
0`.rdata
[email protected]
GCC: (tdm64-2) 4.8.1

With the parameter:

!This program cannot be run in DOS mode.
.text
0`.idata

Upvotes: 5

user3352637
user3352637

Reputation: 11

You can inform the loader which sections to include in your output with a linker script. You can see what sections are included in the file using the objdump command. As you've noticed there's a good bit of 'junk' in an elf - junk that is until you wish you had it.

Note though, that the size of an elf executable file is not indicative of the memory foot print of the image as realized in memory. A lot of the 'junk' isn't in the memory image and the image can call sbreak and or mmap to acquire more memory, the elf file takes no account of stack usage - essentially all of your automatic variables are unaccounted for. These are only three examples others abound.

Upvotes: 1

swigger
swigger

Reputation: 307

use -Qn to avoid that.

aa$ touch hello.c
aa$ gcc -c hello.c 
aa$ objdump -s hello.o 

hello.o:     file format elf32-i386

Contents of section .comment:
 0000 00474343 3a202844 65626961 6e20342e  .GCC: (Debian 4.
 0010 372e322d 35292034 2e372e32 00        7.2-5) 4.7.2.   
aa$ gcc -Qn -c hello.c 
aa$ objdump -s hello.o 

hello.o:     file format elf32-i386

aa$ gcc -v 
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i486-linux-gnu/4.7/lto-wrapper
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.7.2-5' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --enable-targets=all --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
Thread model: posix
gcc version 4.7.2 (Debian 4.7.2-5) 
aa$ 

Upvotes: 15

Mat
Mat

Reputation: 206659

That's in a comment section in the ELF binary. You can strip it out:

$ gcc -m32 -O2 -s -o t t.c
$ ls -l t
-rwxr-xr-x 1 me users 5488 Jun  7 11:58 t
$ readelf -p .comment t

String dump of section '.comment':
  [     0]  GCC: (Gentoo 4.5.1-r1 p1.4, pie-0.4.5) 4.5.1
  [    2d]  GCC: (Gentoo 4.5.2 p1.1, pie-0.4.5) 4.5.2


$ strip -R .comment t


$ readelf -p .comment t
readelf: Warning: Section '.comment' was not dumped because it does not exist!
$ ls -l t
-rwxr-xr-x 1 me users 5352 Jun  7 11:58 t

The gains are tiny though, not sure it's worth it.

Upvotes: 11

AProgrammer
AProgrammer

Reputation: 52274

This is in a comment section which isn't loaded in memory (and note that ELF files usually use padding so that memory mapping them will keep a correct alignment). If you want to get rid of such unneeded sections, see the various objcopy options and find out:

objcopy --remove-section .comment a.o b.o

Upvotes: 4

sehe
sehe

Reputation: 392833

It appears that you'd be able to 'just' strip that if you don't want it; See this page for a nice run-down.

http://timelessname.com/elfbin/

Note that the page (of course) also resorts to using assembly, which you may not want to do, but the general gist applies

Upvotes: 1

Related Questions