【问题标题】:relocation R_X86_64_PC32 against symbol can not be used when making a shared object when fPIC was already used当已使用 fPIC 时制作共享对象时,不能使用针对符号的重定位 R_X86_64_PC32
【发布时间】:2021-01-17 14:22:52
【问题描述】:

我看过很多关于解决这种类型的链接器错误的帖子,在大多数情况下,人们只是忘记使用-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 添加到构建中的所有标志,也许其中一个给我带来了麻烦?

感谢所有帮助。

【问题讨论】:

    标签: g++ linker-errors setuptools pybind11 fpic


    【解决方案1】:

    我找到了解决办法。

    虽然我的构建使用 -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 后我看不到编译标志有任何区别,所以在编译器级别,我不确定有什么区别。无论如何,希望这能帮助遇到类似问题的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-17
      • 2020-01-17
      • 1970-01-01
      • 2017-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多