I try to create a shared library on x86-64 but fail. The problem boils down to the following code (please don't mind, that it does not make a lot of sense):
.section .data
newline:
.ascii "\n"
.section .text
.globl write_newline
.type write_newline, @function
write_newline:
mov $newline, %rax
ret
building as follows:
as minimal.s -o minimal.o
ld -shared minimal.o -o libmin.so
leads to the following error:
ld: minimal.o: relocation R_X86_64_32 against `.data' can not be used when making a shared object; recompile with -fPIC
minimal.o: error adding symbols: Bad value
But gas doesn't know option -fPIC, so I cannot recompile with it:
as -fPIC minimal.s -o minimal.o
as: unrecognized option '-PIC'
What can be done instead?
mov newline@GOTPCREL(%rip),%rax
. Check what output your C compiler generates to get an idea for how to write PIC.