我有一个外部.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).
有想法吗?
答案 0 :(得分:2)
不要混淆编译和链接。库的不同部分在这里使用不同的编译器进行编译。然后将它们链接到一个库(.so
)中,并在此过程中链接输入文件的.comment
部分。
对于您的test_comment_section
示例,请参阅链接器命令行中存在的所有文件(与.comment
相同)的collect2
部分,该文件在{{1}中可见}输出您粘贴的内容。链接文件的名称为:
gcc -v
请注意,动态库无关紧要,因为它们没有链接到您的可执行文件,您的可执行文件只是链接了它们。