Don't Recompile With -fPIC

X86-64 NASM Assembly and PIE

If you get the following error when attempting to compile a 64-bit C application that links with a 64-bit NASM assembly object on Linux:
/usr/bin/ld: foo.o: relocation R_X86_64_32S against `.data' can not be used when making a PIE object; recompile with -fPIC
Don't do what the error message suggests and recompile with -fPIC. From the NASM Manual:
In 64-bit mode, NASM will by default generate absolute addresses. The REL keyword makes it produce RIP-relative addresses
When you have a .data section defined in the NASM 64-bit assembly object:
section .data               ; DX directives
 align 16
 ten:                    dd    10.0
 error_code:             dd    -1.0
All this means is that in order to be PIE-compatible under Linux, change:
 movss      xmm2, [ten]
To:
 movss      xmm2, [rel ten]
Then recompile everything and the linker will no longer complain about not being able to make a PIE object.

Set REL as the DEFAULT

Again, according to the NASM Manual:
Since this is frequently the desired behavior, see the DEFAULT directive (section 6.2). The keyword ABS overrides REL.
This can be useful depending on how many memory accesses are performed against the .data section within the NASM x86-64 assembly code.

Comments