This is the mail archive of the gcc@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: asm(...) and how to properly align jump labels


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~


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