在已经使用fPIC的情况下,在制作共享对象时不能使用针对符号的重定位R_X86_PC32



我看过很多关于解决这种类型的链接器错误的帖子,在大多数情况下,人们只是忘记了使用-fPIC编译,有时人们在内联函数等方面遇到了问题。我正在尝试使用Pybind11为python包装一个c++库。在这个过程中,我想将一些静态库(其中一个是newmat11(链接到.so文件中。

我使用带有-fPIC的automake系统构建了newmat11库(这里有一些输出…(

...
g++ -DHAVE_CONFIG_H -I. -g -O2 -MT newmat8.lo -MD -MP -MF .deps/newmat8.Tpo -c newmat8.cpp  -fPIC -DPIC -o newmat8.o
...
ar cru libnewmat11.a  bandmat.o cholesky.o evalue.o fft.o jacobi.o hholder.o myexcept.o newfft.o newmat1.o newmat2.o newmat3.o newmat4.o newmat5.o newmat6.o newmat7.o newmat8.o newmat9.o newmatex.o newmatrm.o solution.o sort.o submat.o svd.o

事实上,当我运行readelf --relocs libnewmat11.a时,我看到了大量重新定位的符号。这是一个给我带来麻烦的:

$readelf --relocs libnewmat11.a | grep ZTIN6NEWMAT17Sing
000000001d20  013c00000002 R_X86_64_PC32     0000000000000000 _ZTIN6NEWMAT17Singular - 4
Relocation section '.rela.data.rel.ro._ZTIN6NEWMAT17SingularExceptionE' at offset 0x21280 contains 3 entries:
000000005b0b  013c00000001 R_X86_64_64       0000000000000000 _ZTIN6NEWMAT17Singular + 0
0000000009fc  008d00000002 R_X86_64_PC32     0000000000000000 _ZTIN6NEWMAT17Singular - 4
Relocation section '.rela.data.rel.ro._ZTIN6NEWMAT17SingularExceptionE' at offset 0x20b38 contains 3 entries:
000000008280  008d00000001 R_X86_64_64       0000000000000000 _ZTIN6NEWMAT17Singular + 0
...

到目前为止,一切似乎都很好,但当我运行python3 setup.py build时,我得到了这个错误:

running build
running build_py
running build_ext
building 'mytest' extension
x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I../.. -I../../mhfe -I/usr/include/python3.8 -I/usr/local/include/newmat11/include -c src/my_python_binding.cpp -o build/temp.linux-x86_64-3.8/src/my_python_binding.o -std=c++11 -DPYBIND11_PYTHON_VERSION=3.8
...
x86_64-linux-gnu-g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fwrapv -O2 -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fwrapv -O2 -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.8/src/my_python_binding.o -L/usr/lib64python3.8 -o build/lib.linux-x86_64-3.8/mytest.cpython-38-x86_64-linux-gnu.so -fPIC -Wl,--whole-archive /usr/local/lib/newmat11/lib/libnewmat11.a -Wl,--no-whole-archive
/usr/bin/ld: /usr/local/lib/newmat11/lib/libnewmat11.a(newmat8.o): relocation R_X86_64_PC32 against symbol `_ZTIN6NEWMAT17SingularExceptionE' can not be used when making a shared object; recompile with -fPIC

如上所述,我正在使用-fPIC构建静态库,并且可以在.a文件中看到重定位符号。R_X86_64_PC32是否存在与共享对象不兼容的问题?我想我需要让它生成R_X86_64_PLT32重定位符号,但我真的不确定。我可以在库中看到其他R_X86_64_PLT32重定位符号,但不适用于有问题的符号。此外,我还不熟悉setuptools添加到构建中的所有标志,也许其中一个给我带来了麻烦?

感谢所有的帮助。

我找到了一个解决方案。

尽管我的构建使用-fPIC作为依赖库,但我注意到我在configure中没有使用--with-pic进行automake。我添加它只是为了看看会有什么不同。

readelf --relocs现在显示:

000000001a90  01390000002a R_X86_64_REX_GOTP 0000000000000000 _ZTIN6NEWMAT17Singular - 4
Relocation section '.rela.data.rel.ro._ZTIN6NEWMAT17SingularExceptionE' at offset 0x20f30 contains 3 entries:
000000005a54  013900000001 R_X86_64_64       0000000000000000 _ZTIN6NEWMAT17Singular + 0
0000000009fc  008c0000002a R_X86_64_REX_GOTP 0000000000000000 _ZTIN6NEWMAT17Singular - 4
Relocation section '.rela.data.rel.ro._ZTIN6NEWMAT17SingularExceptionE' at offset 0x20830 contains 3 entries:
0000000082b6  008c00000001 R_X86_64_64       0000000000000000 _ZTIN6NEWMAT17Singular + 0

这具有R_X86_64_REX_GOTP重定位符号类型。我还发现我的链接器错误已经解决。

添加--with-pic后,我看不到编译标志有任何区别,所以在编译器级别,我不确定区别是什么。无论如何,希望这能帮助有类似问题的人。

最新更新