Having this:
.text
str:
.string "string"
.globl main
main:
mov $0, %eax
mov $str, %rdi #cannot use $str
call printf
call exit
compiler gives err:
relocation R_X86_64_32S against `.text' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/ld: final link failed: nonrepresentable section on output
I have read somewhere, the mov $str, %rdi
translates to movabsq $str, %rdi
, since the size of first operand $str
, is address and is greater then 32bits and needs to be passed "full" (absolute value). But that would only work on x32 architecture as told me someone here Why does not XORing %eax causes segfault?. So now the only way to use address of that string and pass it to the printf
is use lea str(%rip), %rdi
. But that is one more instruction added, instead of use immediate. So why cannot I use immediate?