This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: asm(...) and how to properly align jump labels
- From: Richard Henderson <rth at redhat dot com>
- To: Denys Duchier <duchier at ps dot uni-sb dot de>
- Cc: gcc at gcc dot gnu dot org
- Date: Fri, 28 Nov 2003 14:11:40 -0800
- Subject: Re: asm(...) and how to properly align jump labels
- References: <86oeuxibvb.fsf@speedy.ps.uni-sb.de>
On Fri, Nov 28, 2003 at 01:18:48AM +0100, Denys Duchier wrote:
> The general shape of the trick is something like this:
>
> FAKE_OPCODE3:
> asm(".byte 3");
> asm(".p2align 4,3");
> asm(" TRUE_OPCODE3:");
> execute_opcode3();
>
> The FAKE_OPCODE3 is needed to fool gcc into believing this code is
> reachable (the address of FAKE_OPCODE3 is taken and used somewhere
> else). I use the address of label TRUE_OPCODE3 as the bytecode for
> opcode3. Using this address, I can find the opcode itself in the byte
> that precedes it.
>
> My worry is: what alignment should I use in the .p2align instruction?
> Is there a safe value?
No.
If you're going to do this, I recommend something like
OPCODE3:
asm (" whatever data");
execute_opcode3();
...
void *addr;
addr = labels[next_opcode];
addr += sizeof_data;
goto *addr;
And have the data sized such that it is a multiple of the required
instruction alignment of the platform. E.g. 4 for risc machines.
If you really think you need higher alignment than that, then add
code to force ADDR to be aligned at the time you make the branch.
r~