Why does the compiler version appear in my ELF executable?

后端 未结 6 781
说谎
说谎 2021-01-04 04:43

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


        
相关标签:
6条回答
  • 2021-01-04 04:54

    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

    0 讨论(0)
  • 2021-01-04 05:02

    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.

    0 讨论(0)
  • 2021-01-04 05:03

    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
    0@.idata
    GCC: (tdm64-2) 4.8.1
    

    With the parameter:

    !This program cannot be run in DOS mode.
    .text
    0`.idata
    
    0 讨论(0)
  • 2021-01-04 05:12

    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.

    0 讨论(0)
  • 2021-01-04 05:13

    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
    
    0 讨论(0)
  • 2021-01-04 05:14

    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$ 
    
    0 讨论(0)
提交回复
热议问题