The following code compiles into an executable with no problems:
static const char *foo = "bar";
void main(void)
{
__asm__ ("mov %0,%%rax"
:
: "i"(&foo)
: "%rax");
}
But as a shared lib, I get an error:
static const char *foo = "bar";
void zot(void)
{
__asm__ ("mov %0,%%rax"
:
: "i"(&foo)
: "%rax");
}
Compilation result:
hacker@lab$ gcc -shared -o mini-lib mini-lib.c
/usr/bin/ld: /tmp/ccwume3d.o: relocation R_X86_64_32S against `.data'
can not be used when making a shared object; recompile with -fPIC
/tmp/ccwume3d.o: error adding symbols: Bad value
Compiling with -fPIC
makes no difference. How can I tweak this in such a way that the linker will relocate a reference to the address of foo
? It needs to be an immediate integer operand in the asm.
Update: I ended up using a different instruction that takes a memory operand, since there is apparently no way to do this with immediate operands.