意外的重定位类型 0x03

unexpected reloc type 0x03

我正在尝试执行一个用我自己的库编译的程序,但是当我执行该程序时出现以下错误:

./a.out 
./a.out: error while loading shared libraries: ../../lib-arm/libCustomLibrary.so: unexpected reloc type 0x03

Release 执行时会发生这种情况,Debug 执行时一切正常。

您认为问题出在哪里?

CustomLibrary 库与以下参数链接:

-lSubLibrary -fPIC -Wl,-Bstatic -lboost_system -lboost_filesystem -lboost_thread -lpthread -Wl,-Bdynamic -lrt

我为我的库添加了 lld unix 库命令输出。

ldd ../../lib-arm/libCustomLibrary.so 
/usr/lib/arm-linux-gnueabihf/libcofi_rpi.so (0x76e5d000)
libSubLibrary.so => ../../lib-arm/libSubLibrary.so (0x76e2d000)
librt.so.1 => /lib/arm-linux-gnueabihf/librt.so.1 (0x76e10000)
libstdc++.so.6 => /usr/lib/arm-linux-gnueabihf/libstdc++.so.6 (0x76d3e000)
libm.so.6 => /lib/arm-linux-gnueabihf/libm.so.6 (0x76ccd000)
libgcc_s.so.1 => /lib/arm-linux-gnueabihf/libgcc_s.so.1 (0x76ca5000)
libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0x76b75000)
libdl.so.2 => /lib/arm-linux-gnueabihf/libdl.so.2 (0x76b6a000)
libpthread.so.0 => /lib/arm-linux-gnueabihf/libpthread.so.0 (0x76b4b000)
/lib/ld-linux-armhf.so.3 (0x76f05000)

来自 https://lists.linaro.org/pipermail/linaro-toolchain/2012-November/002939.html :

Relocation type 3 is R_ARM_REL32 which is a static relocation not allowed in shared objects. How did you create the shared lib? Make sure you compile all the code going into it with -fPIC.

换句话说,您在链接程序时使用了 -fPIC,但在构建共享库时可能没有使用。

关于:

./a.out 
./a.out: error while loading shared libraries: ../../lib-arm/libCustomLibrary.so: unexpected reloc type 0x03

我认为您需要显示 libCustomLibrary.so 的相关源代码。您可以通过以下方式查看导致问题的符号:

LD_DEBUG=all ./a.out

详细输出后,最后提到的符号将是问题所在。例如,在共享对象中测试 Cryptogams SHA,这是手写的 asm,结果是:

10419:     relocation processing: /home/test/libcryptopp-8.3.0.so.8 (lazy)
10419:     symbol=CRYPTOGAMS_armcaps;  lookup in file=/home/test/libcryptopp.so.8 [0]
10419:     binding file /home/test/libcryptopp.so.8 [0]: normal symbol `CRYPTOGAMS_armcaps'
/home/test/: error while loading shared libraries: /home/test/libcryptopp.so.8: unexpected reloc type 0x03

所以我知道问题出在 CRYPTOGAMS_armcaps 符号上。您可以用 objdump -r 确认。 R_ARM_REL32 是符号类型 0x03.

$ objdump -r sha1-armv4.o

sha1-armv4.o:     file format elf32-littlearm

RELOCATION RECORDS FOR [.text]:
OFFSET   TYPE              VALUE
000004d0 R_ARM_REL32       CRYPTOGAMS_armcaps

然后您可以返回源文件并修复符号问题。

此外,从 Crypto++ Issue 846,ARM and "unexpected reloc type 0x03" loading shared object,我知道它并不像使用 -fPIC 那样简单。一切都是用 -fPIC 构建的,但问题仍然存在。