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.

1

2 Answers 2

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.

2
  • 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? Jan 4, 2017 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 4, 2017 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.