7

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.

Neuron
  • 5,141
  • 5
  • 38
  • 59
J Agustin Barrachina
  • 3,501
  • 1
  • 32
  • 52
  • 1
    Possible duplicate of [32-bit absolute addresses no longer allowed in x86-64 Linux?](https://stackoverflow.com/questions/43367427/32-bit-absolute-addresses-no-longer-allowed-in-x86-64-linux) – Braiam Oct 06 '18 at 14:25

2 Answers2

14

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.

yugr
  • 19,769
  • 3
  • 51
  • 96
  • 2
    Solved with that! I added the "-no-pie" line and worked! Thanks a lot! Is there a way of making it permanent though? not to have to write that line each time? – J Agustin Barrachina Jan 04 '17 at 20:17
  • Unfortunately it seems to be enabled by default for all modern distros. `-no-pie` is fine, just throw it into your `CFLAGS`. – yugr Jan 04 '17 at 21:09
2

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.

Lorraine
  • 1,189
  • 14
  • 30