.comment section shows 3 different compilers?

Viewed 527

I got an external .so file and I ran:

objdump -s --section .comment /opt/lib64/libexternal.so

It printed:

Contents of section .comment:
 0000 4743433a 2028474e 55292034 2e342e37  GCC: (GNU) 4.4.7
 0010 20323031 32303331 33202852 65642048   20120313 (Red H
 0020 61742034 2e342e37 2d342900 4743433a  at 4.4.7-4).GCC:
 0030 2028474e 55292036 2e322e31 20323031   (GNU) 6.2.1 201
 0040 36303931 36202852 65642048 61742036  60916 (Red Hat 6
 0050 2e322e31 2d332900 4743433a 2028474e  .2.1-3).GCC: (GN
 0060 55292034 2e382e35 20323031 35303632  U) 4.8.5 2015062
 0070 33202852 65642048 61742034 2e382e35  3 (Red Hat 4.8.5
 0080 2d342900                             -4).            

Any idea why the .so file has 3 different versions of gcc there? It also sounds like the .so contains object files compiled by 3 different gcc compilers?

I just did a small experiment that I compiled the following code:

#include <iostream>

int main() {
    std::cout << "hello" << std::endl;
}

Compiled with:

$ g++ -v -Wall test_comment_section.cpp -o test_comment_section -O3 -lrt -g3 -ggdb3
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-8/root/usr --mandir=/opt/rh/devtoolset-8/root/usr/share/man --infodir=/opt/rh/devtoolset-8/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-8.2.1-20180905/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 8.2.1 20180905 (Red Hat 8.2.1-3) (GCC) 
COLLECT_GCC_OPTIONS='-v' '-Wall' '-o' 'test_comment_section' '-O3' '-g3' '-ggdb3' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/cc1plus -quiet -v -dD -D_GNU_SOURCE test_comment_section.cpp -quiet -dumpbase test_comment_section.cpp -mtune=generic -march=x86-64 -auxbase test_comment_section -g3 -ggdb3 -O3 -Wall -version -o /tmp/cc5RMnqk.s
GNU C++14 (GCC) version 8.2.1 20180905 (Red Hat 8.2.1-3) (x86_64-redhat-linux)
    compiled by GNU C version 8.2.1 20180905 (Red Hat 8.2.1-3), GMP version 6.0.0, MPFR version 3.1.1, MPC version 1.0.1, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8/include-fixed"
ignoring nonexistent directory "/opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8
 /opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/x86_64-redhat-linux
 /opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8/../../../../include/c++/8/backward
 /opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8/include
 /usr/local/include
 /opt/rh/devtoolset-8/root/usr/include
 /usr/include
End of search list.
GNU C++14 (GCC) version 8.2.1 20180905 (Red Hat 8.2.1-3) (x86_64-redhat-linux)
    compiled by GNU C version 8.2.1 20180905 (Red Hat 8.2.1-3), GMP version 6.0.0, MPFR version 3.1.1, MPC version 1.0.1, isl version isl-0.16.1-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 9b29ab96dbb89418adcb25277f32b267
COLLECT_GCC_OPTIONS='-v' '-Wall' '-o' 'test_comment_section' '-O3' '-g3' '-ggdb3' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/as -v --64 -o /tmp/ccdlEH9g.o /tmp/cc5RMnqk.s
GNU assembler version 2.30 (x86_64-redhat-linux) using BFD version version 2.30-47.el7
COMPILER_PATH=/opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/:/opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/:/opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/:/opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8/:/opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8/:/opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-Wall' '-o' 'test_comment_section' '-O3' '-g3' '-ggdb3' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/collect2 -plugin /opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/liblto_plugin.so -plugin-opt=/opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper -plugin-opt=-fresolution=/tmp/ccLbovWd.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o test_comment_section /lib/../lib64/crt1.o /lib/../lib64/crti.o /opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8/crtbegin.o -L/opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8 -L/opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8/../../.. /tmp/ccdlEH9g.o -lrt -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8/crtend.o /lib/../lib64/crtn.o
COLLECT_GCC_OPTIONS='-v' '-Wall' '-o' 'test_comment_section' '-O3' '-g3' '-ggdb3' '-shared-libgcc' '-mtune=generic' '-march=x86-64'

And then dumped the .comment section:

test_comment_section:     file format elf64-x86-64

Contents of section .comment:
 0000 4743433a 2028474e 55292034 2e382e35  GCC: (GNU) 4.8.5
 0010 20323031 35303632 33202852 65642048   20150623 (Red H
 0020 61742034 2e382e35 2d333629 00474343  at 4.8.5-36).GCC
 0030 3a202847 4e552920 382e322e 31203230  : (GNU) 8.2.1 20
 0040 31383039 30352028 52656420 48617420  180905 (Red Hat 
 0050 382e322e 312d3329 00                 8.2.1-3).       

Thought?

1 Answers

Do not confuse compiling and linking. Different portions of a library are compiled with different compilers here. They are then linked together into one library (.so), and .comment sections of input files get catenated in the process.

As for your test_comment_section example, see .comment sections of all the files present on the linker's command line (same as collect2's), it is visible in gcc -v output you pasted. The names of the linked files are:

/lib/../lib64/crt1.o
/lib/../lib64/crti.o
/opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8/crtbegin.o
/tmp/ccdlEH9g.o # -- that's the assembler output for your file. If you want sane names, supply `-save-temps` to gcc.
/opt/rh/devtoolset-8/root/usr/lib/gcc/x86_64-redhat-linux/8/crtend.o
/lib/../lib64/crtn.o

Note that dynamic libraries are irrelevant as they are not linked into your executable, your executable is just linked against them.

Related