.comment 部分显示了 3 种不同的编译器

.comment section shows 3 different compilers?

本文关键字:编译器 显示 comment      更新时间:2023-10-16

我得到了一个外部 .so 文件,我运行了:

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

它打印了:

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).            

知道为什么 .so 文件有 3 个不同版本的 gcc 吗? 听起来 .so 包含由 3 个不同的 gcc 编译器编译的目标文件?

刚刚做了一个小实验,我编译了以下代码:

#include <iostream>
int main() {
    std::cout << "hello" << std::endl;
}

编译方式:

$ 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'

然后转储了 .comment 部分:

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).       

思想?

不要混淆编译和链接。库的不同部分在这里使用不同的编译器进行编译。然后它们被链接到一个库(.so(,输入文件的.comment部分在这个过程中被连接起来。

至于您的test_comment_section示例,请参阅链接器命令行上存在的所有文件.comment部分(与collect2相同(,它在您粘贴gcc -v输出中可见。链接文件的名称为:

/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

请注意,动态库是无关紧要的,因为它们没有链接到可执行文件中,您的可执行文件只是链接到它们。