This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: relocation problem


Dzianis Barzakouski <barzakouski@googlemail.com> writes:

> I am trying to compile following code:
>
> ===lib.c====
> extern int foo();
>
> int bar()
> {
> ÂÂÂ int j = foo();
> ÂÂÂ return j;
> }
> ==========
>
> ===add.c===
> static int i = 0;
>
> int foo()
> {
> ÂÂÂ __asm__("mov 42, %ax");
> ÂÂÂ __asm__("mov %ax, i");
> ÂÂÂ return i;
> }
> ==========
>
> gcc -fPIC -c -o lib.o lib.c
> gcc -fPIC -c -o add.o add.c
> gcc -shared lib.o add.o -o lib.so
> /usr/bin/ld: add.o: relocation R_X86_64_32S against `a local symbol'
> can not be used when making a shared object; recompile with -fPIC
>
> I thought that is due static declaration but it removing don't solves
> the problem. Is this expected behavior? If yes, can somebody explain?

Compiling with -fPIC changes the assembler output to use different
instructions.  You have written your asm statement using a non-PIC
instruction sequence.  If you want this to work in a shared library, you
need to use a PIC instruction sequence.

You can find the PIC instruction sequence by compiling some code with
--save-temps and looking at the .s file.  In this case it should be
something like
    mov %ax, i(%rip)

Ian


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]