28

So I've been coding something on 32-bit and yesterday I needed to build a dll and I had a couple of problems with that. Anyway I solved them here.

Unfortunately even if I thought that everything was working after all I found that wasn't the case when I moved my program and makefile on other computer what runs on 64bit, as you can guess what happened...

So my problem is related to relocation because of 64bit

/usr/bin/ld: MyClass.o: relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC
MyClass.o: could not read symbols: Bad value

and here is my makefile

MyProgram: main.o chkopts
    -${CLINKER} -o $@ $< ${MYLIB} ${PETSC_MAT_LIB}
    ${RM} main.o

    export LD_LIBRARY_PATH=${LIBADD}:$LD_LIBRARY_PATH

LibMyProgram.so: MyClass.o  chkopts
    -${CLINKER}  -shared -Wl,-soname,${SONAME} -o ${VERS}   *.o  ${PETSC_MAT_LIB}

    mv ${VERS} ${LIBADD}
    ln -sf ${LIBADD}${VERS} ${LIBADD}${SOWOV}
    ln -sf ${LIBADD}${VERS} ${LIBADD}${SONAME}

I've tried to add -fPIC in CFLAGS, CPPFLAGS and even LDFLAGS. I've also tried add -fPIC before and after -shared flag.

-${CLINKER} -shared -fPIC -Wl,-soname,${SONAME} -o ${VERS}   *.o  ${PETSC_MAT_LIB}

But I'll just get a same error as previously.

If I use CFLAGS = -fPIC I'll get a bit same kind of error which is:

.../petsc/petsc-3.2-p6/arch-linux2-cxx-debug/lib/libpetsc.a(err.o): relocation R_X86_64_32 against `ompi_mpi_comm_self' can not be used when making a shared object; recompile with -fPIC.

I've read about all the topics what are even remotely similar with my problem but I've been unable to figure this out.

Community
  • 1
  • 1
Mare
  • 943
  • 3
  • 11
  • 18
  • 5
    Thank you so much for asking and answering the question. Now if only somebody could explain why adding "-shared" resolved it... – cmo Jan 18 '13 at 21:49
  • Possible duplicate of [What do R\_X86\_64\_32S and R\_X86\_64\_64 relocation mean?](https://stackoverflow.com/questions/6093547/what-do-r-x86-64-32s-and-r-x86-64-64-relocation-mean) – jww Oct 23 '17 at 19:22

8 Answers8

10

I encountered the same problem when I try to create a shared library which need to link a static library.

I solved the problem by adding -fPIC to CXXFLAGS to compile .o files which are archived in the static library.

user2391685
  • 1,006
  • 12
  • 16
7

The solution was to compile everything with -fPIC, and link shared objects with -shared.

Add -fPIC to CFLAGS or CXXFLAGS for make-based projects.

jww
  • 97,681
  • 90
  • 411
  • 885
Mare
  • 943
  • 3
  • 11
  • 18
4

Trying to compile xmlrpc-c-1.06.41 in CentOS 6.5, I have encountered same linking problem, which was solved by the following: In ./src/cpp, I have modified Makefile: line 142 to

CXXFLAGS = $(CXXFLAGS_COMMON) $(CFLAGS_PERSONAL) $(CADD) -shared -fPIC

More info regarding the flags can be found link

Saeed
  • 550
  • 1
  • 7
  • 12
  • The problem I had was very similar, `/usr/bin/ld: XmlRpcCpp.o: relocation R_X86_64_32 against '.rodata' can not be used when making a shared object; recompile with -fPIC XmlRpcCpp.o: could not read symbols: Bad value` – Saeed May 26 '14 at 18:20
  • Sometimes what is happening is when you are running -fPIC, it will throw this error again for some sub object file. Just take backup of that object file and run make again. Example. you are running for TotalSum.cpp and it has reference to another file CalculateSum.o. Now, the error comes on CalculateSum.o. So, take backup of CalculateSum.o as well and run -fPIC. the error will go away. – bgth Jul 18 '15 at 11:09
4

If this problem still exist after adding "-fPIC",try clean all the .o files,and run again

Xin
  • 69
  • 4
1

I also meet this problem. As I try using @Mare and @user2391685 said, it can work well :

Using -fPIC when comepile to .o file : For example:

gcc -Wall -fPIC -c hello.c -I./ -I/usr/lib/jvm/java/include/ -I/usr/lib/jvm/java/include/linux/

Then you can build a .so file :

gcc -Wall -rdynamic -shared -o libhello.so hello.o Main.h -I/usr/lib/jvm/java/include/ -I/usr/lib/jvm/java/include/linux/
Hexaholic
  • 3,299
  • 7
  • 30
  • 39
Strong
  • 99
  • 8
0

this work as a charm. for who not know yet this easy used

an open file called Makefile.am or Makefile. Just up to your config.

look the code at this _a_CXXFLAGS = or just CXXFLAGS =

add after that files -shared -fPIC

this example

before

crypto_libmubdi_crypto_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIC_FLAGS) $(CXXFLAGS_COMMON) $(CFLAGS_PERSONAL) $(CADD)

after

crypto_libmubdi_crypto_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIC_FLAGS) $(CXXFLAGS_COMMON) $(CFLAGS_PERSONAL) $(CADD) -shared -fPIC

these bugs cause we not put shared for the files or need -fPIC strings/tags.

Note: I experience on to build my blockchain. and this cause added this crypto/sph_sha2big.c

Nur1Labs
  • 9
  • 6
0

In the command line:

cmake -DCMAKE_EXE_LINKER_FLAGS="-no-pie"

Or in the CMakeList.txt:

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -no-pie")
0

Relocation R_X86_64_PC32 against undefined symbol , usually happens when LDFLAGS are set with hardening and CFLAGS not . An usually happens when configure.ac override all system flags with CFLAGS="something" you need change it to CFLAGS+="something"

https://github.com/rpmfusion/lxdream/blob/master/lxdream-0.9.1-implicit.patch https://bugzilla.redhat.com/show_bug.cgi?id=1304277#c3

Sérgio
  • 6,966
  • 1
  • 48
  • 53