Symbol refs in inline asm

Andrew Haley aph@redhat.com
Thu Aug 20 17:15:00 GMT 2009


Amir Gonnen wrote:
> Hi.
> 
> (GCC 4.2.2)
> I would like to emit addresses relative to symbols using inline asm.
> The following code:
> asm volatile("... %0":: "i" (&symbol));
> works well. However:
> asm volatile("... %0":: "i" (&symbol+1));
> 
> fails with these errors:
> test.c:24: warning: asm operand 0 probably doesn't match constraints
> test.c:24: error: impossible constraint in 'asm'
> 
> According to the documentations, the "i" constraint:
> "An immediate integer operand (one with constant value) is allowed. This
> includes symbolic constants whose values will be known only at assembly time
> or later."
> Doesn't it include an offset from a symbolic constant?

This really depends on the assembly language. It doesn't generally make
sense to emit the address of an object as an immediate, because that
object may be pc-relative, addressed via the GOT, etc, etc.

> I'd like to avoid constraints such as "g" because I need to guarantee a
> constant is emitted, never a register.
> Is there some way to do that?

I don't know exactly what you want, but I would have thought a memory
operand was more appropriate.

int NN[] = { 0, 1, 2 };

  asm
    (
     "lea %1, %0\n"
     : "=r" (val) : "m"(NN[1]));

on x86 generates

        lea NN+4(%rip), %eax

Andrew.



More information about the Gcc-help mailing list