I am making some assembly code (intel) and I don't understand why this code does not work when I try to create a shared library :
BITS 64
SECTION .text
GLOBAL test
test:
push rbp
mov rbp, rsp
mov rax, 3
mov al, BYTE [rel array + rax]
pop rbp
ret
SECTION .data
array times 256 db 0
Whereas if you modify the line with the "mov" by changing the register with a number, it works :
mov al, BYTE [rel array + 3]
I do not have any error with nasm, but when i try to link and create a shared library with ld :
relocation R_X86_64_32S against `.data' can not be used when making a shared object; recompile with -fPIC
I found this answer for the "R_X86_64_32S" error : How does C++ linking work in practice?
But what I do not understand why I can not use "rax" as an offset, whereas I can with a number.
Is there a way to browse thought the array ?
This is the commands I use to create the shared library :
nasm -f elf64 test.s
ld -shared test.o -o test.so