relocation R_X86_64_PC32 against symbol can not be used when making a shared object when fPIC was already used

4.1k views Asked by At

I have looked at numerous posts regarding resolving this type of linker error, in most cases, people just forgot to compile with -fPIC, sometimes people had trouble with inline functions, etc. that is not the case here. I am trying to wrap a c++ library for python using Pybind11. In this process, I want to link some static libraries (one of them is newmat11) into a .so file.

I build the newmat11 library using an automake system with -fPIC (here's some output...)

...
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

Indeed, when I run readelf --relocs libnewmat11.a, I see plenty of relocated symbols. Here's one that's giving me trouble:

$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
...

Everything seems ok so far, but when I run python3 setup.py build I get this error:

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

As noted above, I'm building the static lib with -fPIC and I can see the relocation symbols in the .a file. Is there something about R_X86_64_PC32 that makes it incompatible with shared objects? I think I would need to have it produce R_X86_64_PLT32 relocation symbols instead, but I'm really not sure. I can see other R_X86_64_PLT32 relocation symbols in the library, but not for the symbol in question. Also, for the record, I'm not familiar with all of the flags that setuptools is adding to the build, perhaps one of them is giving me trouble?

All help appreciated.

1

There are 1 answers

0
thayne On

I found a solution.

Although my build was using -fPIC for the dependent library, I noticed that I wasn't using --with-pic in my configure for automake. I added it just to see what the difference would be.

readelf --relocs now shows:

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

This has R_X86_64_REX_GOTP relocation symbol types. I also found that my linker error has been resolved.

I can't see any difference in the compile flags after adding --with-pic, so at the compiler level, I'm not sure what the difference is. In any case, hopefully this will help somebody with a similar problem.