编译GNU/Linux共享库, 为什么要用PIC编译?( 转)

编译GNU/Linux共享库, 为什么要用PIC编译?

一直以为不管是编译共享库还是静态库,中间生成的目标文件(.o文件)是没有区别的,
区别只在:最后是用-shared编译还是用ar打包; 可是事情的真相并不是这样的:

from <<Binary Hacks ―ハッカー秘伝のテクニック100選>>  <<Binary Hacks:黑客秘笈100选>>

本hack中, 我们来研究编译共享库时,为什么要用PIC(选项)编译?

通常编译GNU/Linux共享库时,把各个.c文件编译编译成PIC(Position Independent Code, 位置无关代码)。但是,实际上不用PIC编译的话也可以编译共享库。那么使用PIC还有意义吗?

让我们来进行一个实验:
    #include <stdio.h>
    void func() {
        printf(" ");
        printf(" ");
        printf(" ");
    }

用PIC编译必须把参数-fpic或-fPIC传给gcc,-fpic可以生成小而高效的代码,但是不同的处理器中-fpic生成的GOT(Global Offset Table, 全局偏移表)的大小有限制,另一方面,使用-fPIC的话,任何处理器都可以放心使用。在这里,使用-fPIC。(在X86中使用-fpic和-fPIC没有任何区别)。
    $ gcc -o fpic-no-pic.s -S fpic.c
    $ gcc -fPIC -o fpic-pic.s -S fpic.c

阅读上述生成的汇编代码,则可以知道PIC版本通过PLT(Procedure Linkage Table)调用printf。
    $ grep printf fpic-no-pic.s
             call printf
             call printf
             call printf
    $ grep printf fpic-pic.s
             call printf@PLT
             call printf@PLT
             call printf@PLT
下面,编译共享库
    $ gcc -shared -o fpic-no-pic.so fpic.c
    $ gcc -shared -fPIC -o fpic-pic.so fpic.c

这些共享库的动态节(dynamic section)用readelf阅读的话,非PIC版本中有TEXTREL输入方法(需要在text内进行再配置),并且RELCOUNT(再配置的数量)为5 -- 比PIC版本的多3个。多出三个是因为printf()的调用进行了3次。
    $ readelf -d fpic-no-pic.so | egrep 'TEXTREL|RELCOUNT'
     0x00000016 (TEXTREL)                  0x0
     0x6ffffffa (RELCOUNT)                 5
    $ readelf -d fpic-pic.so | egrep 'TEXTREL|RELCOUNT'
     0x6ffffffa (RELCOUNT)  2

PIC版本的RELCOUNT非0是由于gcc在缺省时使用的是包含在启动文件里的代码。若加-nostartfiles选项,则RELCOUNT值为0。

PIC和非PIC共享库的性能对比
上面例子阐述了非PIC版本运行时(动态运行时)需要5个地址的再分配。那么,若在配置的数量大增时会出现什么样的情况呢?

运行下面的shell脚本,用非PIC版本和PIC版本编译含有1000万次printf()调用的共享库,和相应的可执行文件fpic-no-pic和fpic-pic。

    #! /bin/sh
    rm -f *.o *.so
    num=1000
    for i in `seq $num`; do
        echo -e "#include <stdio.h>/nvoid func$i() {" >fpic$i.c
        #ruby -e "10000.times { puts 'printf(/" /");' }" >>fpic$i.c
        perl -e 'print("printf(/" /");/n"x10000);' >>fpic$i.c
        echo "}" >> fpic$i.c
        gcc -o fpic-no-pic$i.o -c fpic$i.c
        gcc -o fpic-pic$i.o -fPIC -c fpic$i.c
    done
    gcc -o fpic-no-pic.so -shared fpic-no-pic*.o
    gcc -o fpic-pic.so -shared fpic-pic*.o

    echo "int main() { return 0; }" >fpic-main.c
    gcc -o no-pic-load fpic-main.c ./fpic-no-pic.so
    gcc -o pic-load fpic-main.c ./fpic-pic.so

    echo "int main() {" >main.c
    for i in `seq $num`; do echo "func$i();"; done >>main.c
    echo "}" >>main.c
    gcc -o fpic-no-pic main.c ./fpic-no-pic.so
    gcc -o fpic-pic main.c ./fpic-pic.so

两个版本程序,运行结果如下: 非PIC版本首次运行时间2.15秒,第二次以后大约0.55秒,而PIC版本的首次用了0.02秒,第二次以后用了0.00秒。

    $ repeat 3 time ./no-pic-load
    2.15s total : 0.29s user 0.48s system 35% cpu
    0.56s total : 0.25s user 0.31s system 99% cpu
    0.55s total : 0.30s user 0.25s system 99% cpu
    $ repeat 3 time ./pic-load
    0.02s total : 0.00s user 0.00s system 0% cpu
    0.00s total : 0.00s user 0.01s system 317% cpu
    0.00s total : 0.00s user 0.00s system 0% cpu
main() 本身是空的,可以知道非PIC版本动态链接时的再分配需要2.15~0.55秒。运行环境Xeon-2.8GHz+Debian GNU/Linux sarge+GCC 3.3.5。

非PIC版本的缺点不仅是在运行时再配置上花时间。为了更新再配置部分的代码,将会发生这样的情况(下载text segment里需要再配置的页->更新->进行copy on write -> 不能与其他路径和text共享)。

另外比较非PIC版本的fpic-no-pic.so和PIC版本的fpic-pic.so的大小,前者268M,后者134M,差别很明显。用readelf -S查看节头,会有以下区别:
                 .rel.dyn      .text
非PIC            152MB         114MB
PIC              0MB           133MB

非PIC版本的代码(.text)比PIC版本的小,但再配置所需要的信息占很大的空间。
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
make for plat=atlas310 cross=aarch64-ascend310-linux-gnu- LDFLAGS=-Wl,--gc-sections -static -L/home/257916/server/test/v1.1/Trunk/build/../src/thirdpart/libs/atlas310 -L/home/257916/server/test/v1.1/Trunk/build/../lib/atlas310 -ldw -lbs -lpthread -lm EXTRA_CFLAGS=-DSVN_VERSION="\"64670"\" -I /net -DOSA_MODULE_NAME=Spectrum-Convert@64670 -DBUILD_DATE="\"Mon, 24 Jul 2023 19:48:54 +0800"\" CFLAGS=-D_GNU_SOURCE -I/home/257916/server/test/v1.1/Trunk/build/../include -I/home/257916/server/test/v1.1/Trunk/build/../src/base/include -I/home/257916/server/test/v1.1/Trunk/build/../src/base/test/include -I/home/257916/server/test/v1.1/Trunk/build/../src/decode/include -I/home/257916/server/test/v1.1/Trunk/build/../src/decode/test/include -I/home/257916/server/test/v1.1/Trunk/build/../src/fourier/include -I/home/257916/server/test/v1.1/Trunk/build/../src/fourier/test/include -I/home/257916/server/test/v1.1/Trunk/build/../src/include -I/home/257916/server/test/v1.1/Trunk/build/../src/service/include -I/home/257916/server/test/v1.1/Trunk/build/../src/service/test/include -I/home/257916/server/test/v1.1/Trunk/build/../src/draw/include -I/home/257916/server/test/v1.1/Trunk/build/../src/draw/test/include -I/home/257916/server/test/v1.1/Trunk/build/../tools/include -fPIC -ffunction-sections -fdata-sections -g -Wall -O1 -c -o ############################################################ mkdir -p /home/257916/server/test/v1.1/Trunk/build/../bin/atlas310 aarch64-ascend310-linux-gnu-g++ -o draw test/src/draw_pic_test.o -Wl,--gc-sections -static -L/home/257916/server/test/v1.1/Trunk/build/../src/thirdpart/libs/atlas310 -L/home/257916/server/test/v1.1/Trunk/build/../lib/atlas310 -ldw -lbs -lpthread -lm /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: Relocations in generic ELF (EM: 62) /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: Relocations in generic ELF (EM: 62) /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: Relocations in generic ELF (EM: 62) /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: Relocations in generic ELF (EM: 62) /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: Relocations in generic ELF (EM: 62) /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: Relocations in generic ELF (EM: 62) /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: Relocations in generic ELF (EM: 62) /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: Relocations in generic ELF (EM: 62) /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: error adding symbols: file in wrong format collect2: error: ld returned 1 exit status make[1]: *** [draw] Error 1 make[1]: Leaving directory `/home/257916/server/test/v1.1/Trunk/src/draw' make: *** [draw] Error 2
07-25
根你提供的信息,编译过程中出现了一个错误。错误信息显示 ld (链接器) 试图处理一个格式错误的文件。 具体来说,ld 报告了以下错误: ``` /opt/Atlas310-ascend/toolkit/toolchain/hcc/bin/../lib64/gcc/aarch64-target-linux-gnu/7.3.0/../../../../aarch64-target-linux-gnu/bin/ld: test/src/draw_pic_test.o: error adding symbols: file in wrong format collect2: error: ld returned 1 exit status ``` 这个错误通常发生在尝试链接不兼容的目标文件时。可能有以下几个原因导致这个错误: 1. 目标文件 `test/src/draw_pic_test.o` 的格式不正确,不符合链接器的预期。你可能需要检查这个目标文件是否由正确的编译器生成,并且是否与你的交叉编译目标匹配。 2. 链接器正在尝试将不同架构的目标文件链接在一起。请确保你的交叉编译环境配置正确,并且编译器和链接器的目标架构与你的目标平台一致。 3. 缺少必要的文件。请确保你的链接命令中指定的文件存在,并且位于正确的位置。 针对这个错误,你可以尝试以下几个步骤来解决问题: 1. 检查目标文件的生成方式和来源。确保使用正确的编译器和编译选项生成目标文件,并确认目标文件的格式正确。 2. 检查你的交叉编译环境配置,确保交叉编译器和链接器的目标架构与你的目标平台一致。 3. 检查链接命令中指定的文件路径是否正确,并确保文件存在。 如果问题仍然存在,可能需要更多的上下文信息来进行进一步的诊断和解决。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值