首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.comment部分显示了3个不同的编译器?

.comment部分显示了3个不同的编译器?
EN

Stack Overflow用户
提问于 2019-06-17 12:43:25
回答 1查看 554关注 0票数 3

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

代码语言:javascript
复制
objdump -s --section .comment /opt/lib64/libexternal.so

它印刷:

代码语言:javascript
复制
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编译器编译的对象文件吗?

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

代码语言:javascript
复制
#include <iostream>

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

编撰:

代码语言:javascript
复制
$ 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部分:

代码语言:javascript
复制
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).       

想过吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-18 14:50:26

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

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

代码语言:javascript
复制
/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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56631575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档