Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

relocation R_X86_64_32S against `.data' can not be used when making a shared object; recompile with -fPIC with gcc

Tags:

gcc

When I compile print.s using gcc I get the following error:

/usr/bin/ld: /tmp/cc45uyZj.o: relocation R_X86_64_32S against `.data' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
  1. I tried the same in another linux distribution and it worked perfectly.

  2. Adding -fPIC doesn't help. Nor does apt-get update.

  3. if you want to see the code: https://github.com/NEGU93/Compilation.git inside the Practice > Part1. Of course is the file print.s (btw. all the .s files have similar errors when I do gcc).

I guess something is missing in the Kali distro but I don't know what can it be.

like image 836
Agustin Barrachina Avatar asked Jan 04 '17 18:01

Agustin Barrachina


2 Answers

It seems that your distro enables -pie by default (check gcc -v output) but your assembly is not position-independent. Try compiling a sample .c file under -fPIC and see how it generates calls. In your case it should be

call    printf@PLT

or you can try compiling with gcc -no-pie.

like image 96
yugr Avatar answered Sep 26 '22 01:09

yugr


I get the same error as you when trying to link some object files compiled with clang and some with gcc. I guess these two compilers have different default settings regarding whether to produce position independent code.

So make sure you're not accidentally using the wrong compiler on part of your project.

like image 43
OmarL Avatar answered Sep 24 '22 01:09

OmarL